]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/src/helpers.cpp
Added more makefiles
[wxWidgets.git] / utils / 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
21f4bf45
RD
13#ifdef __WXMSW__
14#include <windows.h>
15#undef FindWindow
16#undef GetCharWidth
17#undef LoadAccelerators
18#endif
19
08127323
RD
20#ifdef __WXGTK__
21#include "gtk/gtk.h"
22#endif
23
7bf85405
RD
24#undef DEBUG
25#include <Python.h>
26#include "helpers.h"
dd9a3de8 27#include <wx/module.h>
7bf85405 28
7bf85405
RD
29//---------------------------------------------------------------------------
30
31//wxHashTable* wxPyWindows = NULL;
32
33
34wxPoint wxPyDefaultPosition; //wxDefaultPosition);
35wxSize wxPyDefaultSize; //wxDefaultSize);
36wxString wxPyEmptyStr("");
37
38
39
21f4bf45
RD
40#ifdef __WXMSW__ // If building for win32...
41extern HINSTANCE wxhInstance;
42
43//----------------------------------------------------------------------
44// This gets run when the DLL is loaded. We just need to save a handle.
45//----------------------------------------------------------------------
9c039d08 46
21f4bf45
RD
47BOOL WINAPI DllMain(
48 HINSTANCE hinstDLL, // handle to DLL module
49 DWORD fdwReason, // reason for calling function
50 LPVOID lpvReserved // reserved
51 )
52{
53 wxhInstance = hinstDLL;
54 return 1;
55}
56#endif
57
7bf85405
RD
58//----------------------------------------------------------------------
59// Class for implementing the wxp main application shell.
60//----------------------------------------------------------------------
61
62wxPyApp *wxPythonApp = NULL; // Global instance of application object
63
64
65// This one isn't acutally called... See __wxStart()
66bool wxPyApp::OnInit(void) {
67 return false;
68}
69
70int wxPyApp::MainLoop(void) {
71 int retval = wxApp::MainLoop();
72 AfterMainLoop();
73 return retval;
74}
75
76void wxPyApp::AfterMainLoop(void) {
77 // more stuff from wxEntry...
0d6f9504 78
7bf85405
RD
79 if (wxPythonApp->GetTopWindow()) {
80 // Forcibly delete the window.
81 if (wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame)) ||
82 wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxDialog))) {
83
84 wxPythonApp->GetTopWindow()->Close(TRUE);
85 wxPythonApp->DeletePendingObjects();
86 }
87 else {
88 delete wxPythonApp->GetTopWindow();
89 wxPythonApp->SetTopWindow(NULL);
90 }
91 }
0d6f9504
RD
92#ifdef __WXGTK__
93 wxPythonApp->DeletePendingObjects();
94#endif
7bf85405
RD
95
96 wxPythonApp->OnExit();
7bf85405 97 wxApp::CleanUp();
b8b8dda7 98// delete wxPythonApp;
7bf85405
RD
99}
100
101
fb5e0af0 102//---------------------------------------------------------------------
7bf85405
RD
103// a few native methods to add to the module
104//----------------------------------------------------------------------
105
106
0d6f9504 107// This is where we pick up the first part of the wxEntry functionality...
4464bbee 108// The rest is in __wxStart and AfterMainLoop. This function is called when
0d6f9504
RD
109// wxpc is imported. (Before there is a wxApp object.)
110void __wxPreStart()
7bf85405 111{
0d6f9504
RD
112 // Bail out if there is already windows created. This means that the
113 // toolkit has already been initialized, as in embedding wxPython in
114 // a C++ wxWindows app.
115 if (wxTopLevelWindows.Number() > 0)
116 return;
7bf85405 117
7bf85405 118#ifdef __WXMSW__
9c039d08 119 wxApp::Initialize();
7bf85405 120#endif
21f4bf45 121
08127323 122#ifdef __WXGTK__
fb5e0af0
RD
123 PyObject* sysargv = PySys_GetObject("argv");
124 int argc = PyList_Size(sysargv);
125 char** argv = new char*[argc+1];
126 int x;
127 for(x=0; x<argc; x++)
128 argv[x] = PyString_AsString(PyList_GetItem(sysargv, x));
129 argv[argc] = NULL;
130
08127323 131 gtk_set_locale();
0d6f9504
RD
132 gtk_init( &argc, &argv );
133 delete [] argv;
7bf85405 134
08127323 135 wxApp::Initialize(); // may return FALSE. Should we check?
7bf85405
RD
136#endif
137
0d6f9504
RD
138}
139
140
141
142static char* __nullArgv[1] = { 0 };
143
144// Start the user application, user App's OnInit method is a parameter here
145PyObject* __wxStart(PyObject* /* self */, PyObject* args)
146{
147 PyObject* onInitFunc = NULL;
148 PyObject* arglist;
149 PyObject* result;
150 long bResult;
151
152
153 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
154 return NULL;
155
156 if (wxTopLevelWindows.Number() > 0) {
157 PyErr_SetString(PyExc_TypeError, "Only 1 wxApp per process!");
158 return NULL;
159 }
160
161
162 // This is the next part of the wxEntry functionality...
163 wxPythonApp->argc = 0;
164 wxPythonApp->argv = __nullArgv;
165 wxPythonApp->OnInitGui();
166
7bf85405
RD
167
168 // Call the Python App's OnInit function
169 arglist = PyTuple_New(0);
170 result = PyEval_CallObject(onInitFunc, arglist);
171 if (!result) {
172 PyErr_Print();
173 exit(1);
174 }
175
176 if (! PyInt_Check(result)) {
177 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
178 return NULL;
179 }
180 bResult = PyInt_AS_LONG(result);
181 if (! bResult) {
182 wxPythonApp->DeletePendingObjects();
183 wxPythonApp->OnExit();
fb5e0af0 184 wxApp::CleanUp();
7bf85405
RD
185 PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting...");
186 return NULL;
187 }
188
13dfc243 189#ifdef __WXGTK__
fb5e0af0 190 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
13dfc243 191#endif
fb5e0af0 192
7bf85405
RD
193 Py_INCREF(Py_None);
194 return Py_None;
195}
196
197
7bf85405
RD
198
199
200
201PyObject* wxPython_dict;
202PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
203{
204
205 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
206 return NULL;
207
208 if (!PyDict_Check(wxPython_dict)) {
209 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
210 return NULL;
211 }
212#ifdef __WXMOTIF__
21f4bf45
RD
213#define wxPlatform "__WXMOTIF__"
214#endif
215#ifdef __WXQT__
216#define wxPlatform "__WXQT__"
7bf85405
RD
217#endif
218#ifdef __WXGTK__
21f4bf45 219#define wxPlatform "__WXGTK__"
7bf85405
RD
220#endif
221#if defined(__WIN32__) || defined(__WXMSW__)
fb5e0af0 222#define wxPlatform "__WXMSW__"
7bf85405
RD
223#endif
224#ifdef __WXMAC__
21f4bf45 225#define wxPlatform "__WXMAC__"
7bf85405
RD
226#endif
227
228 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
229
230 Py_INCREF(Py_None);
231 return Py_None;
232}
233
234
235//---------------------------------------------------------------------------
236
237
238static
239PyObject* wxPyConstructObject(void* ptr, char* className)
240{
9c039d08 241 char buff[64]; // should always be big enough...
7bf85405
RD
242 char swigptr[64];
243
244 sprintf(buff, "_%s_p", className);
245 SWIG_MakePtr(swigptr, ptr, buff);
246
247 sprintf(buff, "%sPtr", className);
248 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
249 if (! classobj) {
250 Py_INCREF(Py_None);
251 return Py_None;
252 }
253
254 PyObject* arg = Py_BuildValue("(s)", swigptr);
255 PyObject* obj = PyInstance_New(classobj, arg, NULL);
256 Py_DECREF(arg);
257
258 return obj;
259}
260
261
262// This function is used for all events destined for Python event handlers.
263void wxPyCallback::EventThunker(wxEvent& event) {
264 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
265 PyObject* func = cb->m_func;
266 PyObject* result;
267 PyObject* arg;
268 PyObject* tuple;
269
270 arg = wxPyConstructObject((void*)&event, event.GetClassInfo()->GetClassName());
271
272 tuple = PyTuple_New(1);
273 PyTuple_SET_ITEM(tuple, 0, arg);
274 result = PyEval_CallObject(func, tuple);
7bf85405
RD
275 Py_DECREF(tuple);
276 if (result) {
277 Py_DECREF(result);
278 PyErr_Clear();
279 } else {
280 PyErr_Print();
281 }
282}
283
284
285//---------------------------------------------------------------------------
286
287wxPyMenu::wxPyMenu(const wxString& title, PyObject* _func)
288 : wxMenu(title, (wxFunction)(func ? MenuCallback : NULL)), func(0) {
289
290 if (_func) {
291 func = _func;
292 Py_INCREF(func);
293 }
294}
295
296wxPyMenu::~wxPyMenu() {
297 if (func)
298 Py_DECREF(func);
299}
300
301
302void wxPyMenu::MenuCallback(wxMenu& menu, wxCommandEvent& evt) {
303 PyObject* evtobj = wxPyConstructObject((void*)&evt, "wxCommandEvent");
304 PyObject* menuobj = wxPyConstructObject((void*)&menu, "wxMenu");
305 if (PyErr_Occurred()) {
306 // bail out if a problem
307 PyErr_Print();
308 return;
309 }
310 // Now call the callback...
311 PyObject* func = ((wxPyMenu*)&menu)->func;
d5c9047a
RD
312 PyObject* args = PyTuple_New(2);
313 PyTuple_SET_ITEM(args, 0, menuobj);
314 PyTuple_SET_ITEM(args, 1, evtobj);
7bf85405
RD
315 PyObject* res = PyEval_CallObject(func, args);
316 Py_DECREF(args);
d5c9047a 317 Py_XDECREF(res); /* In case res is a NULL pointer */
7bf85405 318}
714e6a9e 319
7bf85405
RD
320
321//---------------------------------------------------------------------------
322
323wxPyTimer::wxPyTimer(PyObject* callback) {
324 func = callback;
325 Py_INCREF(func);
326}
327
328wxPyTimer::~wxPyTimer() {
329 Py_DECREF(func);
330}
331
332void wxPyTimer::Notify() {
333 PyObject* result;
334 PyObject* args = Py_BuildValue("()");
335
336 result = PyEval_CallObject(func, args);
337 Py_DECREF(args);
338 if (result) {
339 Py_DECREF(result);
340 PyErr_Clear();
341 } else {
342 PyErr_Print();
343 }
344}
345
346
347
7bf85405
RD
348//----------------------------------------------------------------------
349//----------------------------------------------------------------------
350// Some helper functions for typemaps in my_typemaps.i, so they won't be
351// imcluded in every file...
352
353
b639c3c5
RD
354byte* byte_LIST_helper(PyObject* source) {
355 if (!PyList_Check(source)) {
356 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
357 return NULL;
358 }
359 int count = PyList_Size(source);
360 byte* temp = new byte[count];
361 if (! temp) {
362 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
363 return NULL;
364 }
365 for (int x=0; x<count; x++) {
366 PyObject* o = PyList_GetItem(source, x);
367 if (! PyInt_Check(o)) {
368 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
369 return NULL;
370 }
371 temp[x] = (byte)PyInt_AsLong(o);
372 }
373 return temp;
374}
375
376
7bf85405
RD
377int* int_LIST_helper(PyObject* source) {
378 if (!PyList_Check(source)) {
379 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
380 return NULL;
381 }
382 int count = PyList_Size(source);
383 int* temp = new int[count];
384 if (! temp) {
385 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
386 return NULL;
387 }
388 for (int x=0; x<count; x++) {
389 PyObject* o = PyList_GetItem(source, x);
390 if (! PyInt_Check(o)) {
391 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
392 return NULL;
393 }
394 temp[x] = PyInt_AsLong(o);
395 }
396 return temp;
397}
398
399
400long* long_LIST_helper(PyObject* source) {
401 if (!PyList_Check(source)) {
402 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
403 return NULL;
404 }
405 int count = PyList_Size(source);
406 long* temp = new long[count];
407 if (! temp) {
408 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
409 return NULL;
410 }
411 for (int x=0; x<count; x++) {
412 PyObject* o = PyList_GetItem(source, x);
413 if (! PyInt_Check(o)) {
414 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
415 return NULL;
416 }
417 temp[x] = PyInt_AsLong(o);
418 }
419 return temp;
420}
421
422
423char** string_LIST_helper(PyObject* source) {
424 if (!PyList_Check(source)) {
425 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
426 return NULL;
427 }
428 int count = PyList_Size(source);
429 char** temp = new char*[count];
430 if (! temp) {
431 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
432 return NULL;
433 }
434 for (int x=0; x<count; x++) {
435 PyObject* o = PyList_GetItem(source, x);
436 if (! PyString_Check(o)) {
437 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
438 return NULL;
439 }
440 temp[x] = PyString_AsString(o);
441 }
442 return temp;
443}
444
445
446
447wxPoint* wxPoint_LIST_helper(PyObject* source) {
448 if (!PyList_Check(source)) {
449 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
450 return NULL;
451 }
452 int count = PyList_Size(source);
453 wxPoint* temp = new wxPoint[count];
454 if (! temp) {
455 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
456 return NULL;
457 }
458 for (int x=0; x<count; x++) {
459 PyObject* o = PyList_GetItem(source, x);
460 if (PyString_Check(o)) {
461 char* st = PyString_AsString(o);
462 wxPoint* pt;
463 if (SWIG_GetPtr(st,(void **) &pt,"_wxPoint_p")) {
464 PyErr_SetString(PyExc_TypeError,"Expected _wxPoint_p.");
465 return NULL;
466 }
467 temp[x] = *pt;
468 }
469 else if (PyTuple_Check(o)) {
470 PyObject* o1 = PyTuple_GetItem(o, 0);
471 PyObject* o2 = PyTuple_GetItem(o, 1);
472
473 temp[x].x = PyInt_AsLong(o1);
474 temp[x].y = PyInt_AsLong(o2);
475 }
476 else {
477 PyErr_SetString(PyExc_TypeError, "Expected a list of 2-tuples or wxPoints.");
478 return NULL;
479 }
480 }
481 return temp;
482}
483
484
485wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
486 if (!PyList_Check(source)) {
487 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
488 return NULL;
489 }
490 int count = PyList_Size(source);
491 wxBitmap** temp = new wxBitmap*[count];
492 if (! temp) {
493 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
494 return NULL;
495 }
496 for (int x=0; x<count; x++) {
497 PyObject* o = PyList_GetItem(source, x);
498 if (PyString_Check(o)) {
499 char* st = PyString_AsString(o);
500 wxBitmap* pt;
501 if (SWIG_GetPtr(st,(void **) &pt,"_wxBitmap_p")) {
502 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
503 return NULL;
504 }
505 temp[x] = pt;
506 }
507 else {
508 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
509 return NULL;
510 }
511 }
512 return temp;
513}
514
515
516
517wxString* wxString_LIST_helper(PyObject* source) {
518 if (!PyList_Check(source)) {
519 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
520 return NULL;
521 }
522 int count = PyList_Size(source);
523 wxString* temp = new wxString[count];
524 if (! temp) {
525 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
526 return NULL;
527 }
528 for (int x=0; x<count; x++) {
529 PyObject* o = PyList_GetItem(source, x);
530 if (! PyString_Check(o)) {
531 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
532 return NULL;
533 }
534 temp[x] = PyString_AsString(o);
535 }
536 return temp;
537}
538
539
540wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
541 if (!PyList_Check(source)) {
542 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
543 return NULL;
544 }
545 int count = PyList_Size(source);
546 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
547 if (! temp) {
548 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
549 return NULL;
550 }
551 for (int x=0; x<count; x++) {
552 PyObject* o = PyList_GetItem(source, x);
553 if (PyString_Check(o)) {
554 char* st = PyString_AsString(o);
555 wxAcceleratorEntry* ae;
556 if (SWIG_GetPtr(st,(void **) &ae,"_wxAcceleratorEntry_p")) {
557 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
558 return NULL;
559 }
560 temp[x] = *ae;
561 }
562 else if (PyTuple_Check(o)) {
563 PyObject* o1 = PyTuple_GetItem(o, 0);
564 PyObject* o2 = PyTuple_GetItem(o, 1);
565 PyObject* o3 = PyTuple_GetItem(o, 2);
566
567 temp[x].m_flags = PyInt_AsLong(o1);
568 temp[x].m_keyCode = PyInt_AsLong(o2);
569 temp[x].m_command = PyInt_AsLong(o3);
570 }
571 else {
572 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
573 return NULL;
574 }
575 }
576 return temp;
577}
578
7bf85405 579//----------------------------------------------------------------------
7bf85405 580
7bf85405 581
7bf85405
RD
582
583/////////////////////////////////////////////////////////////////////////////
584//
585// $Log$
dd9a3de8
RD
586// Revision 1.18 1999/01/30 08:17:27 RD
587// Added wxSashWindow, wxSashEvent, wxLayoutAlgorithm, etc.
588//
589// Various cleanup, tweaks, minor additions, etc. to maintain
590// compatibility with the current wxWindows.
591//
08127323 592// Revision 1.17 1999/01/30 07:30:12 RD
dd9a3de8 593//
08127323
RD
594// Added wxSashWindow, wxSashEvent, wxLayoutAlgorithm, etc.
595//
596// Various cleanup, tweaks, minor additions, etc. to maintain
597// compatibility with the current wxWindows.
598//
4f22cf8d 599// Revision 1.16 1998/12/17 14:07:39 RR
08127323 600//
4f22cf8d
RR
601// Removed minor differences between wxMSW and wxGTK
602//
b8b8dda7
RD
603// Revision 1.15 1998/12/15 20:41:19 RD
604// Changed the import semantics from "from wxPython import *" to "from
605// wxPython.wx import *" This is for people who are worried about
606// namespace pollution, they can use "from wxPython import wx" and then
607// prefix all the wxPython identifiers with "wx."
608//
609// Added wxTaskbarIcon for wxMSW.
610//
611// Made the events work for wxGrid.
612//
613// Added wxConfig.
614//
615// Added wxMiniFrame for wxGTK, (untested.)
616//
617// Changed many of the args and return values that were pointers to gdi
618// objects to references to reflect changes in the wxWindows API.
619//
620// Other assorted fixes and additions.
621//
b639c3c5 622// Revision 1.14 1998/11/25 08:45:25 RD
b8b8dda7 623//
b639c3c5
RD
624// Added wxPalette, wxRegion, wxRegionIterator, wxTaskbarIcon
625// Added events for wxGrid
626// Other various fixes and additions
627//
faf3cb35
RD
628// Revision 1.13 1998/11/15 23:03:45 RD
629// Removing some ifdef's for wxGTK
630//
4464bbee
RD
631// Revision 1.12 1998/11/03 09:21:08 RD
632// fixed a typo
633//
d5c9047a
RD
634// Revision 1.11 1998/10/20 06:43:58 RD
635// New wxTreeCtrl wrappers (untested)
636// some changes in helpers
637// etc.
638//
9c039d08 639// Revision 1.10 1998/10/02 06:40:39 RD
d5c9047a 640//
9c039d08
RD
641// Version 0.4 of wxPython for MSW.
642//
47d67540 643// Revision 1.9 1998/09/25 13:28:52 VZ
9c039d08 644//
47d67540
VZ
645// USE_xxx constants renamed to wxUSE_xxx. This is an incompatible change, you
646// must recompile everything after upgrading!
647//
0d6f9504
RD
648// Revision 1.8 1998/08/27 21:59:08 RD
649// Some chicken-and-egg problems solved for wxPython on wxGTK
650//
21f4bf45
RD
651// Revision 1.7 1998/08/27 00:00:26 RD
652// - more tweaks
653// - have discovered some problems but not yet discovered solutions...
654//
13dfc243 655// Revision 1.6 1998/08/18 21:54:12 RD
21f4bf45 656//
13dfc243
RD
657// ifdef out some wxGTK specific code
658//
fb5e0af0
RD
659// Revision 1.5 1998/08/18 19:48:17 RD
660// more wxGTK compatibility things.
661//
662// It builds now but there are serious runtime problems...
663//
714e6a9e
RD
664// Revision 1.4 1998/08/16 04:31:06 RD
665// More wxGTK work.
666//
03e9bead
RD
667// Revision 1.3 1998/08/15 07:36:36 RD
668// - Moved the header in the .i files out of the code that gets put into
669// the .cpp files. It caused CVS conflicts because of the RCS ID being
670// different each time.
671//
672// - A few minor fixes.
673//
853b255a
RD
674// Revision 1.2 1998/08/14 23:36:36 RD
675// Beginings of wxGTK compatibility
676//
7bf85405
RD
677// Revision 1.1 1998/08/09 08:25:51 RD
678// Initial version
679//
680//