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