added missing headers
[wxWidgets.git] / samples / exec / exec.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: exec.cpp
3 // Purpose: exec sample demonstrates wxExecute and related functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 15.01.00
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "exec.cpp"
22 #pragma interface "exec.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
34 #ifndef WX_PRECOMP
35 #include "wx/app.h"
36 #include "wx/frame.h"
37 #include "wx/utils.h"
38 #include "wx/menu.h"
39 #include "wx/msgdlg.h"
40 #include "wx/textdlg.h"
41 #endif
42
43 #include "wx/process.h"
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp : public wxApp
51 {
52 public:
53 // override base class virtuals
54 // ----------------------------
55
56 // this one is called on application startup and is a good place for the app
57 // initialization (doing it here and not in the ctor allows to have an error
58 // return: if OnInit() returns false, the application terminates)
59 virtual bool OnInit();
60 };
61
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame : public wxFrame
64 {
65 public:
66 // ctor(s)
67 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
68
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent& event);
71
72 void OnSyncExec(wxCommandEvent& event);
73 void OnAsyncExec(wxCommandEvent& event);
74 void OnShell(wxCommandEvent& event);
75
76 void OnAbout(wxCommandEvent& event);
77
78 private:
79 wxString m_cmdLast;
80
81 // any class wishing to process wxWindows events must use this macro
82 DECLARE_EVENT_TABLE()
83 };
84
85 // This is the handler for process termination events
86 class MyProcess : public wxProcess
87 {
88 public:
89 MyProcess(wxFrame *parent, const wxString& cmd)
90 : wxProcess(parent), m_cmd(cmd)
91 {
92 m_parent = parent;
93 }
94
95 // instead of overriding this virtual function we might as well process the
96 // event from it in the frame class - this might be more convenient in some
97 // cases
98 virtual void OnTerminate(int pid, int status);
99
100 private:
101 wxFrame *m_parent;
102 wxString m_cmd;
103 };
104
105 // ----------------------------------------------------------------------------
106 // constants
107 // ----------------------------------------------------------------------------
108
109 // IDs for the controls and the menu commands
110 enum
111 {
112 // menu items
113 Exec_Quit = 100,
114 Exec_SyncExec = 200,
115 Exec_AsyncExec,
116 Exec_Shell,
117 Exec_About = 300
118 };
119
120 // ----------------------------------------------------------------------------
121 // event tables and other macros for wxWindows
122 // ----------------------------------------------------------------------------
123
124 // the event tables connect the wxWindows events with the functions (event
125 // handlers) which process them. It can be also done at run-time, but for the
126 // simple menu events like this the static method is much simpler.
127 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
128 EVT_MENU(Exec_Quit, MyFrame::OnQuit)
129
130 EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec)
131 EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec)
132 EVT_MENU(Exec_Shell, MyFrame::OnShell)
133
134 EVT_MENU(Exec_About, MyFrame::OnAbout)
135 END_EVENT_TABLE()
136
137 // Create a new application object: this macro will allow wxWindows to create
138 // the application object during program execution (it's better than using a
139 // static object for many reasons) and also declares the accessor function
140 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
141 // not wxApp)
142 IMPLEMENT_APP(MyApp)
143
144 // ============================================================================
145 // implementation
146 // ============================================================================
147
148 // ----------------------------------------------------------------------------
149 // the application class
150 // ----------------------------------------------------------------------------
151
152 // `Main program' equivalent: the program execution "starts" here
153 bool MyApp::OnInit()
154 {
155 // Create the main application window
156 MyFrame *frame = new MyFrame(_T("Exec wxWindows sample"),
157 wxDefaultPosition, wxSize(500, 140));
158
159 // Show it and tell the application that it's our main window
160 frame->Show(TRUE);
161 SetTopWindow(frame);
162
163 // success: wxApp::OnRun() will be called which will enter the main message
164 // loop and the application will run. If we returned FALSE here, the
165 // application would exit immediately.
166 return TRUE;
167 }
168
169 // ----------------------------------------------------------------------------
170 // main frame
171 // ----------------------------------------------------------------------------
172
173 // frame constructor
174 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
175 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
176 {
177 #ifdef __WXMAC__
178 // we need this in order to allow the about menu relocation, since ABOUT is
179 // not the default id of the about menu
180 wxApp::s_macAboutMenuItemId = Exec_About;
181 #endif
182
183 // set the frame icon
184 SetIcon(wxICON(mondrian));
185
186 // create a menu bar
187 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
188 menuFile->Append(Exec_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
189
190 wxMenu *execMenu = new wxMenu;
191 execMenu->Append(Exec_SyncExec, _T("Sync &execution...\tCtrl-E"),
192 _T("Launch a program and return when it terminates"));
193 execMenu->Append(Exec_AsyncExec, _T("&Async execution...\tCtrl-A"),
194 _T("Launch a program and return immediately"));
195 execMenu->Append(Exec_Shell, _T("Execute &shell command...\tCtrl-S"),
196 _T("Launch a shell and execute a command in it"));
197
198 wxMenu *helpMenu = new wxMenu(_T(""), wxMENU_TEAROFF);
199 helpMenu->Append(Exec_About, _T("&About...\tF1"), _T("Show about dialog"));
200
201 // now append the freshly created menu to the menu bar...
202 wxMenuBar *menuBar = new wxMenuBar();
203 menuBar->Append(menuFile, _T("&File"));
204 menuBar->Append(execMenu, _T("&Exec"));
205 menuBar->Append(helpMenu, _T("&Help"));
206
207 // ... and attach this menu bar to the frame
208 SetMenuBar(menuBar);
209
210 #if wxUSE_STATUSBAR
211 // create a status bar just for fun (by default with 1 pane only)
212 CreateStatusBar(2);
213 SetStatusText(_T("Welcome to wxWindows!"));
214 #endif // wxUSE_STATUSBAR
215 }
216
217
218 // event handlers
219
220 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
221 {
222 // TRUE is to force the frame to close
223 Close(TRUE);
224 }
225
226 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
227 {
228 wxMessageBox(_T("Exec sample\n© 2000 Vadim Zeitlin"),
229 _T("About Exec"), wxOK | wxICON_INFORMATION, this);
230 }
231
232 void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event))
233 {
234 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
235 _T("Exec sample"),
236 m_cmdLast);
237
238 if ( !cmd )
239 return;
240
241 int code = wxExecute(cmd, TRUE /* sync */);
242 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
243 cmd.c_str(), code);
244 m_cmdLast = cmd;
245 }
246
247 void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event))
248 {
249 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
250 _T("Exec sample"),
251 m_cmdLast);
252
253 if ( !cmd )
254 return;
255
256 wxProcess *process = new MyProcess(this, cmd);
257 if ( !wxExecute(cmd, FALSE /* async */, process) )
258 {
259 wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
260
261 delete process;
262 }
263 else
264 {
265 m_cmdLast = cmd;
266 }
267 }
268
269 void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event))
270 {
271 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
272 _T("Exec sample"),
273 m_cmdLast);
274
275 if ( !cmd )
276 return;
277
278 int code = wxShell(cmd);
279 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
280 cmd.c_str(), code);
281 m_cmdLast = cmd;
282 }
283
284 // ----------------------------------------------------------------------------
285 // MyProcess
286 // ----------------------------------------------------------------------------
287
288 void MyProcess::OnTerminate(int pid, int status)
289 {
290 wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."),
291 pid, m_cmd.c_str(), status);
292
293 // we're not needed any more
294 delete this;
295 }