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