]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/src/helpers.cpp
Initial version
[wxWidgets.git] / utils / wxPython / src / helpers.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpers.cpp
3 // Purpose: Helper functions/classes for the wxPython extenaion 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 #undef DEBUG
14 #include <Python.h>
15 #include "helpers.h"
16
17
18 //---------------------------------------------------------------------------
19
20 //wxHashTable* wxPyWindows = NULL;
21
22
23 wxPoint wxPyDefaultPosition; //wxDefaultPosition);
24 wxSize wxPyDefaultSize; //wxDefaultSize);
25 wxString wxPyEmptyStr("");
26
27
28
29 //----------------------------------------------------------------------
30 // Class for implementing the wxp main application shell.
31 //----------------------------------------------------------------------
32
33 wxPyApp *wxPythonApp = NULL; // Global instance of application object
34
35
36 // This one isn't acutally called... See __wxStart()
37 bool wxPyApp::OnInit(void) {
38 return false;
39 }
40
41 int wxPyApp::MainLoop(void) {
42 int retval = wxApp::MainLoop();
43 AfterMainLoop();
44 return retval;
45 }
46
47 void wxPyApp::AfterMainLoop(void) {
48 // more stuff from wxEntry...
49 if (wxPythonApp->GetTopWindow()) {
50 // Forcibly delete the window.
51 if (wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxFrame)) ||
52 wxPythonApp->GetTopWindow()->IsKindOf(CLASSINFO(wxDialog))) {
53
54 wxPythonApp->GetTopWindow()->Close(TRUE);
55 wxPythonApp->DeletePendingObjects();
56 }
57 else {
58 delete wxPythonApp->GetTopWindow();
59 wxPythonApp->SetTopWindow(NULL);
60 }
61 }
62
63 wxPythonApp->OnExit();
64 #ifdef __WXMSW__
65 wxApp::CleanUp();
66 #endif
67 #ifdef __WXGTK__
68 wxApp::CommonCleanup();
69 #endif
70 delete wxPythonApp;
71 }
72
73
74 //----------------------------------------------------------------------
75 // a few native methods to add to the module
76 //----------------------------------------------------------------------
77
78
79 // Start the user application, user App's OnInit method is a parameter here
80 PyObject* __wxStart(PyObject* /* self */, PyObject* args)
81 {
82 PyObject* onInitFunc = NULL;
83 PyObject* arglist;
84 PyObject* result;
85 long bResult;
86
87 if (!PyArg_ParseTuple(args, "O", &onInitFunc))
88 return NULL;
89
90 // This is where we pick up one part of the wxEntry functionality...
91 // the rest is in AfterMainLoop.
92 #ifdef __WXMSW__
93 wxPythonApp->argc = 0;
94 wxPythonApp->argv = NULL;
95 wxPythonApp->OnInitGui();
96 #endif
97 #ifdef __WXGTK__
98 wxTheApp->argc = 0;
99 wxTheApp->argv = NULL;
100
101 gtk_init( &wxTheApp->argc, &wxTheApp->argv );
102
103 #ifdef USE_GDK_IMLIB
104
105 gdk_imlib_init();
106
107 gtk_widget_push_visual(gdk_imlib_get_visual());
108
109 gtk_widget_push_colormap(gdk_imlib_get_colormap());
110
111 #endif
112
113 wxApp::CommonInit();
114
115 wxTheApp->OnInitGui();
116
117 #endif
118
119
120 // Call the Python App's OnInit function
121 arglist = PyTuple_New(0);
122 result = PyEval_CallObject(onInitFunc, arglist);
123 if (!result) {
124 PyErr_Print();
125 exit(1);
126 }
127
128 if (! PyInt_Check(result)) {
129 PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
130 return NULL;
131 }
132 bResult = PyInt_AS_LONG(result);
133 if (! bResult) {
134 wxPythonApp->DeletePendingObjects();
135 wxPythonApp->OnExit();
136 #ifdef __WXMSW__
137 wxApp::CleanUp();
138 #endif
139 #ifdef __WXGTK__
140 wxApp::CommonCleanup();
141 #endif
142 PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting...");
143 return NULL;
144 }
145
146 Py_INCREF(Py_None);
147 return Py_None;
148 }
149
150
151 //PyObject* __wxMainLoop(PyObject* /* self */, PyObject* /* args */)
152 //{
153 // wxPythonApp->MainLoop();
154 // if (wxPythonApp->wx_frame) {
155 // wxPythonApp->wx_frame->GetEventHandler()->OnClose();
156 // delete wxPythonApp->wx_frame;
157 // }
158 // wxCleanUp();
159
160 // Py_INCREF(Py_None);
161 // return Py_None;
162 //}
163
164
165 //PyObject* __wxExitMainLoop(PyObject* /* self */, PyObject* /* args */)
166 //{
167 // wxPythonApp->ExitMainLoop();
168 // Py_INCREF(Py_None);
169 // return Py_None;
170 //}
171
172
173
174 //PyObject* __wxAddCallback(PyObject* /* self */, PyObject* args)
175 //{
176 // char* swigPtr;
177 // char* name;
178 // PyObject* callback;
179 // wxWindow* win;
180 // wxPyEvtHandlers* evtHdlr;
181
182 // if (!PyArg_ParseTuple(args, "ssO", &swigPtr, &name, &callback))
183 // return NULL;
184
185 // if (!PyCallable_Check(callback)) {
186 // PyErr_SetString(PyExc_TypeError, "Expected a callable object.");
187 // return NULL;
188 // }
189
190 // if (SWIG_GetPtr(swigPtr, (void **)&win, "_wxWindow_p")) {
191 // PyErr_SetString(PyExc_TypeError, "Expected class derived from wxWindow.");
192 // return NULL;
193 // }
194
195 // evtHdlr = (wxPyEvtHandlers*)win->GetClientData();
196 // if (! evtHdlr->addCallback(name, callback)) {
197 // PyErr_SetString(PyExc_TypeError, "Unknown callback name.");
198 // return NULL;
199 // }
200
201 // Py_INCREF(Py_None);
202 // return Py_None;
203 //}
204
205
206
207 //PyObject* __wxSetWinEvtHdlr(PyObject* /* self */, PyObject* args)
208 //{
209 // char* swigPtr;
210 // wxWindow* win;
211 // wxPyEvtHandlers* evtHdlr;
212
213 // if (!PyArg_ParseTuple(args, "s", &swigPtr))
214 // return NULL;
215
216 // if (SWIG_GetPtr(swigPtr, (void **)&win, "_wxWindow_p")) {
217 // PyErr_SetString(PyExc_TypeError, "Expected class derived from wxWindow.");
218 // return NULL;
219 // }
220
221 // evtHdlr = new wxPyEvtHandlers;
222 // win->SetClientData((char*)evtHdlr);
223
224 // Py_INCREF(Py_None);
225 // return Py_None;
226 //}
227
228
229
230 //PyObject* __wxDelWinEvtHdlr(PyObject* /* self */, PyObject* args)
231 //{
232 // char* swigPtr;
233 // wxWindow* win;
234 // wxPyEvtHandlers* evtHdlr;
235
236 // if (!PyArg_ParseTuple(args, "s", &swigPtr))
237 // return NULL;
238
239 // if (SWIG_GetPtr(swigPtr, (void **)&win, "_wxWindow_p")) {
240 // PyErr_SetString(PyExc_TypeError, "Expected class derived from wxWindow.");
241 // return NULL;
242 // }
243
244 // evtHdlr = (wxPyEvtHandlers*)win->GetClientData();
245 // printf("__wxDelWinEvtHdlr: %p\n", evtHdlr);
246 // delete evtHdlr;
247
248 // Py_INCREF(Py_None);
249 // return Py_None;
250 //}
251
252
253
254 PyObject* wxPython_dict;
255 PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args)
256 {
257
258 if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
259 return NULL;
260
261 if (!PyDict_Check(wxPython_dict)) {
262 PyErr_SetString(PyExc_TypeError, "_wxSetDictionary must have dictionary object!");
263 return NULL;
264 }
265 #ifdef __WXMOTIF__
266 #define wxPlatform "__MOTIF__"
267 #endif
268 #ifdef __WXGTK__
269 #define wxPlatform "__GTK__"
270 #endif
271 #if defined(__WIN32__) || defined(__WXMSW__)
272 #define wxPlatform "__WIN32__"
273 #endif
274 #ifdef __WXMAC__
275 #define wxPlatform "__MAC__"
276 #endif
277
278 PyDict_SetItemString(wxPython_dict, "wxPlatform", PyString_FromString(wxPlatform));
279
280 Py_INCREF(Py_None);
281 return Py_None;
282 }
283
284
285 //---------------------------------------------------------------------------
286
287
288 static
289 PyObject* wxPyConstructObject(void* ptr, char* className)
290 {
291 char buff[64]; // should be big enough...
292 char swigptr[64];
293
294 sprintf(buff, "_%s_p", className);
295 SWIG_MakePtr(swigptr, ptr, buff);
296
297 sprintf(buff, "%sPtr", className);
298 PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
299 if (! classobj) {
300 Py_INCREF(Py_None);
301 return Py_None;
302 }
303
304 PyObject* arg = Py_BuildValue("(s)", swigptr);
305 PyObject* obj = PyInstance_New(classobj, arg, NULL);
306 Py_DECREF(arg);
307
308 return obj;
309 }
310
311
312 // This function is used for all events destined for Python event handlers.
313 void wxPyCallback::EventThunker(wxEvent& event) {
314 wxPyCallback* cb = (wxPyCallback*)event.m_callbackUserData;
315 PyObject* func = cb->m_func;
316 PyObject* result;
317 PyObject* arg;
318 PyObject* tuple;
319
320 arg = wxPyConstructObject((void*)&event, event.GetClassInfo()->GetClassName());
321
322 tuple = PyTuple_New(1);
323 PyTuple_SET_ITEM(tuple, 0, arg);
324 result = PyEval_CallObject(func, tuple);
325 Py_DECREF(arg);
326 Py_DECREF(tuple);
327 if (result) {
328 Py_DECREF(result);
329 PyErr_Clear();
330 } else {
331 PyErr_Print();
332 }
333 }
334
335
336 //---------------------------------------------------------------------------
337
338 wxPyMenu::wxPyMenu(const wxString& title, PyObject* _func)
339 : wxMenu(title, (wxFunction)(func ? MenuCallback : NULL)), func(0) {
340
341 if (_func) {
342 func = _func;
343 Py_INCREF(func);
344 }
345 }
346
347 wxPyMenu::~wxPyMenu() {
348 if (func)
349 Py_DECREF(func);
350 }
351
352
353 void wxPyMenu::MenuCallback(wxMenu& menu, wxCommandEvent& evt) {
354 PyObject* evtobj = wxPyConstructObject((void*)&evt, "wxCommandEvent");
355 PyObject* menuobj = wxPyConstructObject((void*)&menu, "wxMenu");
356 if (PyErr_Occurred()) {
357 // bail out if a problem
358 PyErr_Print();
359 return;
360 }
361 // Now call the callback...
362 PyObject* func = ((wxPyMenu*)&menu)->func;
363 PyObject* args = Py_BuildValue("(OO)", menuobj, evtobj);
364 PyObject* res = PyEval_CallObject(func, args);
365 Py_DECREF(args);
366 Py_DECREF(res);
367 Py_DECREF(evtobj);
368 Py_DECREF(menuobj);
369 }
370
371 //---------------------------------------------------------------------------
372
373 wxPyTimer::wxPyTimer(PyObject* callback) {
374 func = callback;
375 Py_INCREF(func);
376 }
377
378 wxPyTimer::~wxPyTimer() {
379 Py_DECREF(func);
380 }
381
382 void wxPyTimer::Notify() {
383 PyObject* result;
384 PyObject* args = Py_BuildValue("()");
385
386 result = PyEval_CallObject(func, args);
387 Py_DECREF(args);
388 if (result) {
389 Py_DECREF(result);
390 PyErr_Clear();
391 } else {
392 PyErr_Print();
393 }
394 }
395
396
397
398 //----------------------------------------------------------------------
399 // wxPyEvtHandlers class
400 //----------------------------------------------------------------------
401
402 //wxPyEvtHandlers::wxPyEvtHandlers()
403 // : pyOnActivate(0),
404 // pyOnChar(0),
405 // pyOnCharHook(0),
406 // pyOnClose(0),
407 // pyOnCommand(0),
408 // pyOnDropFiles(0),
409 // pyOnDefaultAction(0),
410 // pyOnEvent(0),
411 // pyOnInitMenuPopup(0),
412 // pyOnKillFocus(0),
413 // pyOnMenuCommand(0),
414 // pyOnMenuSelect(0),
415 // pyOnMove(0),
416 // pyOnPaint(0),
417 // pyOnScroll(0),
418 // pyOnSetFocus(0),
419 // pyOnSize(0),
420 // pyOnSysColourChange(0),
421 // pyOnLeftClick(0),
422 // pyOnMouseEnter(0),
423 // pyOnRightClick(0),
424 // pyOnDoubleClickSash(0),
425 // pyOnUnsplit(0)
426 //{
427 //}
428
429
430 //wxPyEvtHandlers::~wxPyEvtHandlers()
431 //{
432 // Py_XDECREF(pyOnActivate);
433 // Py_XDECREF(pyOnChar);
434 // Py_XDECREF(pyOnCharHook);
435 // Py_XDECREF(pyOnClose);
436 // Py_XDECREF(pyOnCommand);
437 // Py_XDECREF(pyOnDropFiles);
438 // Py_XDECREF(pyOnDefaultAction);
439 // Py_XDECREF(pyOnEvent);
440 // Py_XDECREF(pyOnInitMenuPopup);
441 // Py_XDECREF(pyOnKillFocus);
442 // Py_XDECREF(pyOnMenuCommand);
443 // Py_XDECREF(pyOnMenuSelect);
444 // Py_XDECREF(pyOnMove);
445 // Py_XDECREF(pyOnPaint);
446 // Py_XDECREF(pyOnScroll);
447 // Py_XDECREF(pyOnSetFocus);
448 // Py_XDECREF(pyOnSize);
449 // Py_XDECREF(pyOnSysColourChange);
450 // Py_XDECREF(pyOnLeftClick);
451 // Py_XDECREF(pyOnMouseEnter);
452 // Py_XDECREF(pyOnRightClick);
453 // Py_XDECREF(pyOnDoubleClickSash);
454 // Py_XDECREF(pyOnUnsplit);
455
456 // wxNode* node = cleanupList.First();
457 // while (node) {
458 // delete (wxPyEvtHandlers*)node->Data();
459 // delete node;
460 // node = cleanupList.First();
461 // }
462
463 // node = decrefList.First();
464 // while (node) {
465 // PyObject* obj = (PyObject*)node->Data();
466 // Py_DECREF(obj);
467 // delete node;
468 // node = decrefList.First();
469 // }
470 //// printf("~wxPyEvtHandlers: %p\n", this);
471 //}
472
473 ////----------------------------------------------------------------------
474
475 //Bool wxPyEvtHandlers::addCallback(char* name, PyObject* callback)
476 //{
477 // Py_INCREF(callback);
478
479 // if (strcmp(name, "OnActivate") == 0) {
480 // pyOnActivate = callback;
481 // return TRUE;
482 // }
483 // if (strcmp(name, "OnChar") == 0) {
484 // pyOnChar = callback;
485 // return TRUE;
486 // }
487 // if (strcmp(name, "OnCharHook") == 0) {
488 // pyOnCharHook = callback;
489 // return TRUE;
490 // }
491 // if (strcmp(name, "OnClose") == 0) {
492 // pyOnClose = callback;
493 // return TRUE;
494 // }
495 // if (strcmp(name, "OnCommand") == 0) {
496 // pyOnCommand = callback;
497 // return TRUE;
498 // }
499 // if (strcmp(name, "OnDropFiles") == 0) {
500 // pyOnDropFiles = callback;
501 // return TRUE;
502 // }
503 // if (strcmp(name, "OnDefaultAction") == 0) {
504 // pyOnDefaultAction = callback;
505 // return TRUE;
506 // }
507 // if (strcmp(name, "OnEvent") == 0) {
508 // pyOnEvent = callback;
509 // return TRUE;
510 // }
511 // if (strcmp(name, "OnInitMenuPopup") == 0) {
512 // pyOnInitMenuPopup = callback;
513 // return TRUE;
514 // }
515 // if (strcmp(name, "OnKillFocus") == 0) {
516 // pyOnKillFocus = callback;
517 // return TRUE;
518 // }
519 // if (strcmp(name, "OnMenuCommand") == 0) {
520 // pyOnMenuCommand = callback;
521 // return TRUE;
522 // }
523 // if (strcmp(name, "OnMenuSelect") == 0) {
524 // pyOnMenuSelect = callback;
525 // return TRUE;
526 // }
527 // if (strcmp(name, "OnMove") == 0) {
528 // pyOnMove = callback;
529 // return TRUE;
530 // }
531 // if (strcmp(name, "OnPaint") == 0) {
532 // pyOnPaint = callback;
533 // return TRUE;
534 // }
535 // if (strcmp(name, "OnScroll") == 0) {
536 // pyOnScroll = callback;
537 // return TRUE;
538 // }
539 // if (strcmp(name, "OnSetFocus") == 0) {
540 // pyOnSetFocus = callback;
541 // return TRUE;
542 // }
543 // if (strcmp(name, "OnSize") == 0) {
544 // pyOnSize = callback;
545 // return TRUE;
546 // }
547 // if (strcmp(name, "OnSysColourChange") == 0) {
548 // pyOnSysColourChange = callback;
549 // return TRUE;
550 // }
551 // if (strcmp(name, "OnLeftClick") == 0) {
552 // pyOnLeftClick = callback;
553 // return TRUE;
554 // }
555 // if (strcmp(name, "OnMouseEnter") == 0) {
556 // pyOnMouseEnter = callback;
557 // return TRUE;
558 // }
559 // if (strcmp(name, "OnRightClick") == 0) {
560 // pyOnRightClick = callback;
561 // return TRUE;
562 // }
563 // if (strcmp(name, "OnDoubleClickSash") == 0) {
564 // pyOnDoubleClickSash = callback;
565 // return TRUE;
566 // }
567 // if (strcmp(name, "OnUnsplit") == 0) {
568 // pyOnUnsplit = callback;
569 // return TRUE;
570 // }
571
572 // // If we get here, there was no match.
573 // Py_DECREF(callback);
574 // return FALSE;
575 //}
576
577
578 ////----------------------------------------------------------------------
579 //// Helpers to assist in calling the python callable objects
580 ////----------------------------------------------------------------------
581
582 //PyObject* wxPyEvtHandlers::constructObject(void* ptr, char* className)
583 //{
584 // char buff[64]; // should be big enough...
585 // char swigptr[64];
586
587 // sprintf(buff, "_%s_p", className);
588 // SWIG_MakePtr(swigptr, ptr, buff);
589
590 // sprintf(buff, "%sPtr", className);
591 // PyObject* classobj = PyDict_GetItemString(wxPython_dict, buff);
592 // PyObject* arg = Py_BuildValue("(s)", swigptr);
593 // PyObject* obj = PyInstance_New(classobj, arg, NULL);
594 // Py_DECREF(arg);
595
596 // return obj;
597 //}
598
599
600
601 //int wxPyEvtHandlers::callFunc(PyObject* func, PyObject* arglist)
602 //{
603 // PyObject* result;
604 // int retval = FALSE;
605
606 // result = PyEval_CallObject(func, arglist);
607 // Py_DECREF(arglist);
608 // if (result) { // Assumes an integer return type...
609 // retval = PyInt_AsLong(result);
610 // Py_DECREF(result);
611 // PyErr_Clear(); // forget about it if it's not...
612 // } else {
613 // PyErr_Print();
614 // }
615 // return retval;
616 //}
617
618 ////---------------------------------------------------------------------------
619 //// Methods and helpers of the wxPy* classes
620 ////---------------------------------------------------------------------------
621
622 //IMP_OnActivate(wxFrame, wxPyFrame);
623 //IMP_OnCharHook(wxFrame, wxPyFrame);
624 //IMP_OnClose(wxFrame, wxPyFrame);
625 //IMP_OnMenuCommand(wxFrame, wxPyFrame);
626 //IMP_OnMenuSelect(wxFrame, wxPyFrame);
627 //IMP_OnSize(wxFrame, wxPyFrame);
628 //IMP_OnDropFiles(wxFrame, wxPyFrame);
629
630 //IMP_OnChar(wxCanvas, wxPyCanvas);
631 //IMP_OnEvent(wxCanvas, wxPyCanvas);
632 //IMP_OnPaint(wxCanvas, wxPyCanvas);
633 //IMP_OnScroll(wxCanvas, wxPyCanvas);
634 //IMP_OnDropFiles(wxCanvas, wxPyCanvas);
635
636 //IMP_OnChar(wxPanel, wxPyPanel);
637 //IMP_OnEvent(wxPanel, wxPyPanel);
638 //IMP_OnPaint(wxPanel, wxPyPanel);
639 //IMP_OnScroll(wxPanel, wxPyPanel);
640 //IMP_OnCommand(wxPanel, wxPyPanel);
641 //IMP_OnDefaultAction(wxPanel, wxPyPanel);
642 //IMP_OnDropFiles(wxPanel, wxPyPanel);
643
644 //IMP_OnChar(wxTextWindow, wxPyTextWindow);
645 //IMP_OnDropFiles(wxTextWindow, wxPyTextWindow);
646
647 //IMP_OnCharHook(wxDialogBox, wxPyDialogBox);
648 //IMP_OnClose(wxDialogBox, wxPyDialogBox);
649 //IMP_OnSize(wxDialogBox, wxPyDialogBox);
650 //IMP_OnDropFiles(wxDialogBox, wxPyDialogBox);
651 //IMP_OnChar(wxDialogBox, wxPyDialogBox);
652 //IMP_OnEvent(wxDialogBox, wxPyDialogBox);
653 //IMP_OnPaint(wxDialogBox, wxPyDialogBox);
654 //IMP_OnScroll(wxDialogBox, wxPyDialogBox);
655 //IMP_OnCommand(wxDialogBox, wxPyDialogBox);
656 //IMP_OnDefaultAction(wxDialogBox, wxPyDialogBox);
657
658 //IMP_OnChar(wxToolBar, wxPyToolBar);
659 //IMP_OnEvent(wxToolBar, wxPyToolBar);
660 //IMP_OnPaint(wxToolBar, wxPyToolBar);
661 //IMP_OnScroll(wxToolBar, wxPyToolBar);
662 //IMP_OnCommand(wxToolBar, wxPyToolBar);
663 //IMP_OnDefaultAction(wxToolBar, wxPyToolBar);
664 //IMP_OnDropFiles(wxToolBar, wxPyToolBar);
665 //IMP_OnMouseEnter(wxToolBar, wxPyToolBar);
666 //IMP_OnRightClick(wxToolBar, wxPyToolBar);
667
668 //IMP_OnChar(wxButtonBar, wxPyButtonBar);
669 //IMP_OnEvent(wxButtonBar, wxPyButtonBar);
670 //IMP_OnPaint(wxButtonBar, wxPyButtonBar);
671 //IMP_OnScroll(wxButtonBar, wxPyButtonBar);
672 //IMP_OnCommand(wxButtonBar, wxPyButtonBar);
673 //IMP_OnDefaultAction(wxButtonBar, wxPyButtonBar);
674 //IMP_OnDropFiles(wxButtonBar, wxPyButtonBar);
675 //IMP_OnMouseEnter(wxButtonBar, wxPyButtonBar);
676 //IMP_OnRightClick(wxButtonBar, wxPyButtonBar);
677
678 //IMP_OnDoubleClickSash(wxSplitterWindow, wxPySplitterWindow);
679 //IMP_OnUnsplit(wxSplitterWindow, wxPySplitterWindow);
680
681
682
683 //Bool wxPyToolBar::OnLeftClick(int a, int b) {
684 // wxPyEvtHandlers* peh=(wxPyEvtHandlers*)GetClientData();
685 // if (peh->pyOnLeftClick)
686 // return peh->callFunc(peh->pyOnLeftClick, Py_BuildValue("(ii)",a,b));
687 // else {
688 // // If there is no Python callback, redirect the request to
689 // // the OnMenuCommand of the parent frame.
690 // wxFrame* frame = (wxFrame*)GetParent();
691 // frame->OnMenuCommand(a);
692 // return TRUE;
693 // }
694 //// else
695 //// return wxToolBar::OnLeftClick(a,b);
696 //}
697 //Bool wxPyToolBar::baseclass_OnLeftClick(int a, int b) {
698 // return wxToolBar::OnLeftClick(a,b);
699 //}
700
701
702 //Bool wxPyButtonBar::OnLeftClick(int a, int b) {
703 // wxPyEvtHandlers* peh=(wxPyEvtHandlers*)GetClientData();
704 // if (peh->pyOnLeftClick)
705 // return peh->callFunc(peh->pyOnLeftClick, Py_BuildValue("(ii)",a,b));
706 // else {
707 // // If there is no Python callback, redirect the request to
708 // // the OnMenuCommand of the parent frame.
709 // wxFrame* frame = (wxFrame*)GetParent();
710 // frame->OnMenuCommand(a);
711 // return TRUE;
712 // }
713 //}
714 //Bool wxPyButtonBar::baseclass_OnLeftClick(int a, int b) {
715 // return wxButtonBar::OnLeftClick(a,b);
716 //}
717
718
719
720 //wxPyMenu::wxPyMenu(PyObject* _func)
721 // : wxMenu(NULL, (wxFunction)(func ? MenuCallback : NULL)), func(0) {
722
723 // if (_func) {
724 // func = _func;
725 // Py_INCREF(func);
726 // }
727 //}
728
729 //wxPyMenu::~wxPyMenu() {
730 // if (func)
731 // Py_DECREF(func);
732 //}
733
734
735 //void wxPyMenu::MenuCallback(wxWindow& win, wxCommandEvent& evt) {
736 // wxPyEvtHandlers* peh= new wxPyEvtHandlers; // Used for the helper methods
737 // PyObject* evtobj = peh->constructObject((void*)&evt, "wxCommandEvent");
738 // PyObject* winobj = peh->constructObject((void*)&win, "wxWindow");
739 // if (PyErr_Occurred()) {
740 // // bail out if a problem
741 // PyErr_Print();
742 // delete peh;
743 // return;
744 // }
745 // // Now call the callback...
746 // PyObject* func = ((wxPyMenu*)&win)->func;
747 // peh->callFunc(func, Py_BuildValue("(OO)", winobj, evtobj));
748 // Py_DECREF(evtobj);
749 // Py_DECREF(winobj);
750 // delete peh;
751 //}
752
753
754
755 //wxPyTimer::wxPyTimer(PyObject* callback) {
756 // func = callback;
757 // Py_INCREF(func);
758 //}
759
760 //wxPyTimer::~wxPyTimer() {
761 // Py_DECREF(func);
762 //}
763
764 //void wxPyTimer::Notify() {
765 // wxPyEvtHandlers* peh= new wxPyEvtHandlers; // just for the helper methods
766 // peh->callFunc(func, Py_BuildValue("()"));
767 // delete peh;
768 //}
769
770 //----------------------------------------------------------------------
771 //----------------------------------------------------------------------
772 // Some helper functions for typemaps in my_typemaps.i, so they won't be
773 // imcluded in every file...
774
775
776 int* int_LIST_helper(PyObject* source) {
777 if (!PyList_Check(source)) {
778 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
779 return NULL;
780 }
781 int count = PyList_Size(source);
782 int* temp = new int[count];
783 if (! temp) {
784 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
785 return NULL;
786 }
787 for (int x=0; x<count; x++) {
788 PyObject* o = PyList_GetItem(source, x);
789 if (! PyInt_Check(o)) {
790 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
791 return NULL;
792 }
793 temp[x] = PyInt_AsLong(o);
794 }
795 return temp;
796 }
797
798
799 long* long_LIST_helper(PyObject* source) {
800 if (!PyList_Check(source)) {
801 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
802 return NULL;
803 }
804 int count = PyList_Size(source);
805 long* temp = new long[count];
806 if (! temp) {
807 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
808 return NULL;
809 }
810 for (int x=0; x<count; x++) {
811 PyObject* o = PyList_GetItem(source, x);
812 if (! PyInt_Check(o)) {
813 PyErr_SetString(PyExc_TypeError, "Expected a list of integers.");
814 return NULL;
815 }
816 temp[x] = PyInt_AsLong(o);
817 }
818 return temp;
819 }
820
821
822 char** string_LIST_helper(PyObject* source) {
823 if (!PyList_Check(source)) {
824 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
825 return NULL;
826 }
827 int count = PyList_Size(source);
828 char** temp = new char*[count];
829 if (! temp) {
830 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
831 return NULL;
832 }
833 for (int x=0; x<count; x++) {
834 PyObject* o = PyList_GetItem(source, x);
835 if (! PyString_Check(o)) {
836 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
837 return NULL;
838 }
839 temp[x] = PyString_AsString(o);
840 }
841 return temp;
842 }
843
844
845
846 wxPoint* wxPoint_LIST_helper(PyObject* source) {
847 if (!PyList_Check(source)) {
848 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
849 return NULL;
850 }
851 int count = PyList_Size(source);
852 wxPoint* temp = new wxPoint[count];
853 if (! temp) {
854 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
855 return NULL;
856 }
857 for (int x=0; x<count; x++) {
858 PyObject* o = PyList_GetItem(source, x);
859 if (PyString_Check(o)) {
860 char* st = PyString_AsString(o);
861 wxPoint* pt;
862 if (SWIG_GetPtr(st,(void **) &pt,"_wxPoint_p")) {
863 PyErr_SetString(PyExc_TypeError,"Expected _wxPoint_p.");
864 return NULL;
865 }
866 temp[x] = *pt;
867 }
868 else if (PyTuple_Check(o)) {
869 PyObject* o1 = PyTuple_GetItem(o, 0);
870 PyObject* o2 = PyTuple_GetItem(o, 1);
871
872 temp[x].x = PyInt_AsLong(o1);
873 temp[x].y = PyInt_AsLong(o2);
874 }
875 else {
876 PyErr_SetString(PyExc_TypeError, "Expected a list of 2-tuples or wxPoints.");
877 return NULL;
878 }
879 }
880 return temp;
881 }
882
883
884 wxBitmap** wxBitmap_LIST_helper(PyObject* source) {
885 if (!PyList_Check(source)) {
886 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
887 return NULL;
888 }
889 int count = PyList_Size(source);
890 wxBitmap** temp = new wxBitmap*[count];
891 if (! temp) {
892 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
893 return NULL;
894 }
895 for (int x=0; x<count; x++) {
896 PyObject* o = PyList_GetItem(source, x);
897 if (PyString_Check(o)) {
898 char* st = PyString_AsString(o);
899 wxBitmap* pt;
900 if (SWIG_GetPtr(st,(void **) &pt,"_wxBitmap_p")) {
901 PyErr_SetString(PyExc_TypeError,"Expected _wxBitmap_p.");
902 return NULL;
903 }
904 temp[x] = pt;
905 }
906 else {
907 PyErr_SetString(PyExc_TypeError, "Expected a list of wxBitmaps.");
908 return NULL;
909 }
910 }
911 return temp;
912 }
913
914
915
916 wxString* wxString_LIST_helper(PyObject* source) {
917 if (!PyList_Check(source)) {
918 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
919 return NULL;
920 }
921 int count = PyList_Size(source);
922 wxString* temp = new wxString[count];
923 if (! temp) {
924 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
925 return NULL;
926 }
927 for (int x=0; x<count; x++) {
928 PyObject* o = PyList_GetItem(source, x);
929 if (! PyString_Check(o)) {
930 PyErr_SetString(PyExc_TypeError, "Expected a list of strings.");
931 return NULL;
932 }
933 temp[x] = PyString_AsString(o);
934 }
935 return temp;
936 }
937
938
939 wxAcceleratorEntry* wxAcceleratorEntry_LIST_helper(PyObject* source) {
940 if (!PyList_Check(source)) {
941 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
942 return NULL;
943 }
944 int count = PyList_Size(source);
945 wxAcceleratorEntry* temp = new wxAcceleratorEntry[count];
946 if (! temp) {
947 PyErr_SetString(PyExc_MemoryError, "Unable to allocate temporary array");
948 return NULL;
949 }
950 for (int x=0; x<count; x++) {
951 PyObject* o = PyList_GetItem(source, x);
952 if (PyString_Check(o)) {
953 char* st = PyString_AsString(o);
954 wxAcceleratorEntry* ae;
955 if (SWIG_GetPtr(st,(void **) &ae,"_wxAcceleratorEntry_p")) {
956 PyErr_SetString(PyExc_TypeError,"Expected _wxAcceleratorEntry_p.");
957 return NULL;
958 }
959 temp[x] = *ae;
960 }
961 else if (PyTuple_Check(o)) {
962 PyObject* o1 = PyTuple_GetItem(o, 0);
963 PyObject* o2 = PyTuple_GetItem(o, 1);
964 PyObject* o3 = PyTuple_GetItem(o, 2);
965
966 temp[x].m_flags = PyInt_AsLong(o1);
967 temp[x].m_keyCode = PyInt_AsLong(o2);
968 temp[x].m_command = PyInt_AsLong(o3);
969 }
970 else {
971 PyErr_SetString(PyExc_TypeError, "Expected a list of 3-tuples or wxAcceleratorEntry objects.");
972 return NULL;
973 }
974 }
975 return temp;
976 }
977
978
979
980 //----------------------------------------------------------------------
981 // A WinMain for when wxWindows and Python are linked together in a single
982 // application, instead of as a dynamic module
983
984
985 //#if !defined(WIN_PYD) && defined(WIN32)
986
987 //extern "C" int Py_Main(int argc, char** argv);
988
989 //int APIENTRY WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR m_lpCmdLine,
990 // int nCmdShow )
991 //{
992
993 // wxpCreateApp();
994
995 // // Initialize wxWindows, but don't start the main loop
996 // wxEntry(hInstance, hPrevInstance, m_lpCmdLine, nCmdShow, FALSE);
997
998 // Py_Initialize();
999 // PyObject *argvList = PyList_New(0);
1000
1001 // char* stderrfilename = "wxpstderr.log";
1002 // int pyargc = 1;
1003 // char* script = NULL;
1004 // int argc = wxPythonApp->argc;
1005 // char** argv = wxPythonApp->argv;
1006
1007 // for (int i = 1; i < argc; i++) {
1008 // if (strncmp(argv[i], "wxpstderr=", 10) == 0)
1009 // stderrfilename = argv[i]+10;
1010 // else {
1011 // PyList_Append(argvList, PyString_FromString(argv[i]));
1012 // if (!script)
1013 // script = argv[i];
1014 // pyargc++;
1015 // }
1016 // }
1017
1018 // PySys_SetObject("argv", argvList);
1019
1020 //#if 1
1021 // char buf[256];
1022 //// //PyRun_SimpleString("import sys; sys.stdout=open('wxpstdout.log','w')");
1023 // sprintf(buf, "import sys; sys.stdout=sys.stderr=open('%s','w')", stderrfilename);
1024 // PyRun_SimpleString(buf);
1025 //#endif
1026
1027 // initwxPythonc();
1028
1029 // if (script) {
1030 // FILE *fp = fopen(script, "r");
1031 // if (fp) {
1032 // PyRun_SimpleFile(fp, script);// This returns after wxpApp constructor
1033 // fclose(fp);
1034 // }
1035 // else {
1036 // char msg[256];
1037 // sprintf(msg, "Cannot open %s", script);
1038 // wxMessageBox(msg);
1039 // }
1040 // }
1041 // else
1042 // PyRun_SimpleString("import wxpide");
1043
1044 // return 0;
1045 //}
1046
1047
1048 //#endif
1049
1050 //----------------------------------------------------------------------
1051
1052 /////////////////////////////////////////////////////////////////////////////
1053 //
1054 // $Log$
1055 // Revision 1.1 1998/08/09 08:25:51 RD
1056 // Initial version
1057 //
1058 //