]> git.saurik.com Git - wxWidgets.git/blame - wxPython/samples/embedded/embedded.cpp
Add GetClassDefaultAttribute. Since it is static we need to put the
[wxWidgets.git] / wxPython / samples / embedded / embedded.cpp
CommitLineData
a2426843
RD
1//----------------------------------------------------------------------
2// Name: embedded.cpp
248834a0 3// Purpose: To serve as an example of how to use wxPython from
a2426843
RD
4// within a C++ wxWindows program.
5//
6// Author: Robin Dunn
7//
8// Created: 1-May-2002
9// RCS-ID: $Id$
10// Copyright: (c) 2002 by Total Control Software
11// Licence: wxWindows license
12//----------------------------------------------------------------------
13
6b7e0a86
RD
14#include <Python.h>
15
16
a2426843
RD
17// For compilers that support precompilation, includes "wx/wx.h".
18#include <wx/wxprec.h>
19
20#ifdef __BORLANDC__
21 #pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25 #include <wx/wx.h>
26#endif
27
28#include <wx/splitter.h>
29
30#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
31 #include "mondrian.xpm"
32#endif
33
34// Import Python and wxPython headers
6b7e0a86
RD
35#include <wx/wxPython/wxPython.h>
36
a2426843
RD
37
38//----------------------------------------------------------------------
39// Class definitions
40
41class MyApp : public wxApp
42{
43public:
44 virtual bool OnInit();
45 virtual ~MyApp();
46 void Init_wxPython();
6b7e0a86
RD
47private:
48 PyThreadState* m_mainTState;
a2426843
RD
49};
50
51
52class MyFrame : public wxFrame
53{
54public:
55 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
56 void RedirectStdio();
57 wxWindow* DoPythonStuff(wxWindow* parent);
58 void OnExit(wxCommandEvent& event);
59 void OnPyFrame(wxCommandEvent& event);
60
61private:
62 DECLARE_EVENT_TABLE()
63};
64
65//----------------------------------------------------------------------
66// MyApp methods
67
68
69bool MyApp::OnInit()
70{
71 Init_wxPython();
72 MyFrame *frame = new MyFrame(_T("Embedded wxPython Test"),
73 wxPoint(50, 50), wxSize(700, 600));
74 frame->Show(TRUE);
75 return TRUE;
76}
77
78
79void MyApp::Init_wxPython()
80{
81 // Initialize Python
82 Py_Initialize();
83 PyEval_InitThreads();
84
2620b36e
RD
85 // Load the wxPython core API. Imports the wx._core module and sets a
86 // local pointer to a function table located there.
a2426843
RD
87 wxPyCoreAPI_IMPORT();
88
89 // Save the current Python thread state and release the
90 // Global Interpreter Lock.
6b7e0a86 91 m_mainTState = wxPyBeginAllowThreads();
a2426843
RD
92}
93
94
95MyApp::~MyApp()
96{
97 // Restore the thread state and tell Python to cleanup after itself.
2620b36e 98 // wxPython will do its own cleanup as part of that process.
6b7e0a86 99 wxPyEndAllowThreads(m_mainTState);
a2426843
RD
100 Py_Finalize();
101}
102
2620b36e 103
a2426843
RD
104IMPLEMENT_APP(MyApp)
105
106//----------------------------------------------------------------------
107
108enum
109{
110 ID_EXIT=1001,
111 ID_PYFRAME
112};
113
114
115BEGIN_EVENT_TABLE(MyFrame, wxFrame)
5d26a7e6
RD
116 EVT_MENU(ID_EXIT, MyFrame::OnExit)
117 EVT_MENU(ID_PYFRAME, MyFrame::OnPyFrame)
a2426843
RD
118END_EVENT_TABLE()
119
120
121
122MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
123 : wxFrame(NULL, -1, title, pos, size,
124 wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
125{
126 SetIcon(wxICON(mondrian));
127
128 wxMenuBar* mbar = new wxMenuBar;
129 wxMenu* menu = new wxMenu;
6b7e0a86 130 menu->Append(ID_PYFRAME, _T("Make wx&Python frame"));
a2426843 131 menu->AppendSeparator();
6b7e0a86
RD
132 menu->Append(ID_EXIT, _T("&Close Frame\tAlt-X"));
133 mbar->Append(menu, _T("&File"));
a2426843
RD
134 SetMenuBar(mbar);
135
136 CreateStatusBar();
137 RedirectStdio();
138
139 // Make some child windows from C++
140 wxSplitterWindow* sp = new wxSplitterWindow(this, -1);
141 wxPanel* p1 = new wxPanel(sp, -1);
142 p1->SetFont(wxFont(12, wxSWISS, wxNORMAL, wxBOLD));
143 new wxStaticText(p1, -1,
6b7e0a86 144 _T("The frame, menu, splitter, this panel and this text were created in C++..."),
a2426843
RD
145 wxPoint(10,10));
146
147 // And get a panel from Python
148 wxWindow* p2 = DoPythonStuff(sp);
149
150 sp->SplitHorizontally(p1, p2, GetClientSize().y/4);
151}
152
153void MyFrame::OnExit(wxCommandEvent& event)
154{
155 Close();
156}
157
158
159//----------------------------------------------------------------------
6b7e0a86 160// This is where the fun begins...
a2426843
RD
161
162
163char* python_code1 = "\
2620b36e
RD
164import wx\n\
165f = wx.Frame(None, -1, 'Hello from wxPython!', size=(250, 150))\n\
a2426843
RD
166f.Show()\n\
167";
168
169void MyFrame::OnPyFrame(wxCommandEvent& event)
170{
171 // For simple Python code that doesn't have to interact with the
172 // C++ code in any way, you can execute it with PyRun_SimpleString.
173
174
175 // First, whenever you do anyting with Python objects or code, you
176 // *MUST* aquire the Global Interpreter Lock and block other
177 // Python threads from running.
da32eb53 178 bool blocked = wxPyBeginBlockThreads();
a2426843
RD
179
180 // Execute the code in the __main__ module
181 PyRun_SimpleString(python_code1);
182
183 // Finally, release the GIL and let other Python threads run.
da32eb53 184 wxPyEndBlockThreads(blocked);
a2426843
RD
185}
186
187
188void MyFrame::RedirectStdio()
189{
190 // This is a helpful little tidbit to help debugging and such. It
191 // redirects Python's stdout and stderr to a window that will popup
192 // only on demand when something is printed, like a traceback.
193 char* python_redirect = "\
194import sys\n\
2620b36e
RD
195import wx\n\
196output = wx.PyOnDemandOutputWindow()\n\
a2426843
RD
197sys.stdin = sys.stderr = output\n\
198";
da32eb53 199 bool blocked = wxPyBeginBlockThreads();
a2426843 200 PyRun_SimpleString(python_redirect);
da32eb53 201 wxPyEndBlockThreads(blocked);
a2426843
RD
202}
203
204
205
206
207char* python_code2 = "\
b8a42834
RD
208import sys\n\
209sys.path.append('.')\n\
a2426843
RD
210import embedded_sample\n\
211\n\
212def makeWindow(parent):\n\
213 win = embedded_sample.MyPanel(parent)\n\
214 return win\n\
215";
216
217wxWindow* MyFrame::DoPythonStuff(wxWindow* parent)
218{
219 // More complex embedded situations will require passing C++ objects to
220 // Python and/or returning objects from Python to be used in C++. This
221 // sample shows one way to do it. NOTE: The above code could just have
a9f4542f 222 // easily come from a file, or the whole thing could be in the Python
a2426843
RD
223 // module that is imported and manipulated directly in this C++ code. See
224 // the Python API for more details.
225
226 wxWindow* window = NULL;
227 PyObject* result;
228
229 // As always, first grab the GIL
da32eb53 230 bool blocked = wxPyBeginBlockThreads();
a2426843
RD
231
232 // Now make a dictionary to serve as the global namespace when the code is
233 // executed. Put a reference to the builtins module in it. (Yes, the
234 // names are supposed to be different, I don't know why...)
235 PyObject* globals = PyDict_New();
236 PyObject* builtins = PyImport_ImportModule("__builtin__");
237 PyDict_SetItemString(globals, "__builtins__", builtins);
238 Py_DECREF(builtins);
239
240 // Execute the code to make the makeWindow function
241 result = PyRun_String(python_code2, Py_file_input, globals, globals);
242 // Was there an exception?
243 if (! result) {
244 PyErr_Print();
6b7e0a86 245 wxPyEndBlockThreads(blocked);
a2426843
RD
246 return NULL;
247 }
248 Py_DECREF(result);
249
250 // Now there should be an object named 'makeWindow' in the dictionary that
251 // we can grab a pointer to:
252 PyObject* func = PyDict_GetItemString(globals, "makeWindow");
253 wxASSERT(PyCallable_Check(func));
254
255 // Now build an argument tuple and call the Python function. Notice the
256 // use of another wxPython API to take a wxWindows object and build a
257 // wxPython object that wraps it.
258 PyObject* arg = wxPyMake_wxObject(parent);
259 wxASSERT(arg != NULL);
a2426843
RD
260 PyObject* tuple = PyTuple_New(1);
261 PyTuple_SET_ITEM(tuple, 0, arg);
262 result = PyEval_CallObject(func, tuple);
263
264 // Was there an exception?
265 if (! result)
266 PyErr_Print();
267 else {
268 // Otherwise, get the returned window out of Python-land and
269 // into C++-ville...
6b7e0a86
RD
270 bool success = wxPyConvertSwigPtr(result, (void**)&window, _T("wxWindow"));
271 wxASSERT_MSG(success, _T("Returned object was not a wxWindow!"));
a2426843
RD
272 Py_DECREF(result);
273 }
274
275 // Release the python objects we still have
276 Py_DECREF(globals);
277 Py_DECREF(tuple);
278
279 // Finally, after all Python stuff is done, release the GIL
da32eb53 280 wxPyEndBlockThreads(blocked);
a2426843
RD
281
282 return window;
283}
284
285
286//----------------------------------------------------------------------
287
288
289
290
291
292