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