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