]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/src/helpers.cpp
Added missing | symbol
[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 #ifdef __WXMSW__
14 #include <windows.h>
15 #undef FindWindow
16 #undef GetCharWidth
17 #undef LoadAccelerators
18 #endif
19
20 #ifdef __WXGTK__
21 #include "gtk/gtk.h"
22 #endif
23
24 #undef DEBUG
25 #include <Python.h>
26 #include "helpers.h"
27 #include <wx/module.h>
28
29 //---------------------------------------------------------------------------
30
31 //wxHashTable* wxPyWindows = NULL;
32
33
34 wxPoint wxPyDefaultPosition; //wxDefaultPosition);
35 wxSize wxPyDefaultSize; //wxDefaultSize);
36 wxString wxPyEmptyStr("");
37
38
39
40 #ifdef __WXMSW__ // If building for win32...
41 extern HINSTANCE wxhInstance;
42
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 wxhInstance = 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 void wxPyApp::AfterMainLoop(void) {
77 // more stuff from wxEntry...
78
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 }
92 #ifdef __WXGTK__
93 wxPythonApp->DeletePendingObjects();
94 #endif
95
96 wxPythonApp->OnExit();
97 wxApp::CleanUp();
98 // delete wxPythonApp;
99 }
100
101
102 //---------------------------------------------------------------------
103 // a few native methods to add to the module
104 //----------------------------------------------------------------------
105
106
107 // This is where we pick up the first part of the wxEntry functionality...
108 // The rest is in __wxStart and AfterMainLoop. This function is called when
109 // wxpc is imported. (Before there is a wxApp object.)
110 void __wxPreStart()
111 {
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;
117
118 #ifdef __WXMSW__
119 wxApp::Initialize();
120 #endif
121
122 #ifdef __WXGTK__
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
131 gtk_set_locale();
132 gtk_init( &argc, &argv );
133 delete [] argv;
134
135 wxApp::Initialize(); // may return FALSE. Should we check?
136 #endif
137
138 }
139
140
141
142 static char* __nullArgv[1] = { 0 };
143
144 // Start the user application, user App's OnInit method is a parameter here
145 PyObject* __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
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();
184 wxApp::CleanUp();
185 PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting...");
186 return NULL;
187 }
188
189 #ifdef __WXGTK__
190 wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0);
191 #endif
192
193 Py_INCREF(Py_None);
194 return Py_None;
195 }
196
197
198
199
200
201 PyObject* wxPython_dict;
202 PyObject* __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__
213 #define wxPlatform "__WXMOTIF__"
214 #endif
215 #ifdef __WXQT__
216 #define wxPlatform "__WXQT__"
217 #endif
218 #ifdef __WXGTK__
219 #define wxPlatform "__WXGTK__"
220 #endif
221 #if defined(__WIN32__) || defined(__WXMSW__)
222 #define wxPlatform "__WXMSW__"
223 #endif
224 #ifdef __WXMAC__
225 #define wxPlatform "__WXMAC__"
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
238 static
239 PyObject* wxPyConstructObject(void* ptr, char* className)
240 {
241 char buff[64]; // should always be big enough...
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.
263 void 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);
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
287 wxPyMenu::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
296 wxPyMenu::~wxPyMenu() {
297 if (func)
298 Py_DECREF(func);
299 }
300
301
302 void 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;
312 PyObject* args = PyTuple_New(2);
313 PyTuple_SET_ITEM(args, 0, menuobj);
314 PyTuple_SET_ITEM(args, 1, evtobj);
315 PyObject* res = PyEval_CallObject(func, args);
316 Py_DECREF(args);
317 Py_XDECREF(res); /* In case res is a NULL pointer */
318 }
319
320
321 //---------------------------------------------------------------------------
322
323 wxPyTimer::wxPyTimer(PyObject* callback) {
324 func = callback;
325 Py_INCREF(func);
326 }
327
328 wxPyTimer::~wxPyTimer() {
329 Py_DECREF(func);
330 }
331
332 void 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
348 //----------------------------------------------------------------------
349 //----------------------------------------------------------------------
350 // Some helper functions for typemaps in my_typemaps.i, so they won't be
351 // imcluded in every file...
352
353
354 byte* 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
377 int* 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
400 long* 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
423 char** 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
447 wxPoint* 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
485 wxBitmap** 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
517 wxString* 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
540 wxAcceleratorEntry* 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
579 //----------------------------------------------------------------------
580
581
582
583 /////////////////////////////////////////////////////////////////////////////
584 //
585 // $Log$
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 //
592 // Revision 1.17 1999/01/30 07:30:12 RD
593 //
594 // Added wxSashWindow, wxSashEvent, wxLayoutAlgorithm, etc.
595 //
596 // Various cleanup, tweaks, minor additions, etc. to maintain
597 // compatibility with the current wxWindows.
598 //
599 // Revision 1.16 1998/12/17 14:07:39 RR
600 //
601 // Removed minor differences between wxMSW and wxGTK
602 //
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 //
622 // Revision 1.14 1998/11/25 08:45:25 RD
623 //
624 // Added wxPalette, wxRegion, wxRegionIterator, wxTaskbarIcon
625 // Added events for wxGrid
626 // Other various fixes and additions
627 //
628 // Revision 1.13 1998/11/15 23:03:45 RD
629 // Removing some ifdef's for wxGTK
630 //
631 // Revision 1.12 1998/11/03 09:21:08 RD
632 // fixed a typo
633 //
634 // Revision 1.11 1998/10/20 06:43:58 RD
635 // New wxTreeCtrl wrappers (untested)
636 // some changes in helpers
637 // etc.
638 //
639 // Revision 1.10 1998/10/02 06:40:39 RD
640 //
641 // Version 0.4 of wxPython for MSW.
642 //
643 // Revision 1.9 1998/09/25 13:28:52 VZ
644 //
645 // USE_xxx constants renamed to wxUSE_xxx. This is an incompatible change, you
646 // must recompile everything after upgrading!
647 //
648 // Revision 1.8 1998/08/27 21:59:08 RD
649 // Some chicken-and-egg problems solved for wxPython on wxGTK
650 //
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 //
655 // Revision 1.6 1998/08/18 21:54:12 RD
656 //
657 // ifdef out some wxGTK specific code
658 //
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 //
664 // Revision 1.4 1998/08/16 04:31:06 RD
665 // More wxGTK work.
666 //
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 //
674 // Revision 1.2 1998/08/14 23:36:36 RD
675 // Beginings of wxGTK compatibility
676 //
677 // Revision 1.1 1998/08/09 08:25:51 RD
678 // Initial version
679 //
680 //