]>
Commit | Line | Data |
---|---|---|
69c33c6c VZ |
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" | |
a8f0faf3 GRG |
38 | #include "wx/menu.h" |
39 | #include "wx/msgdlg.h" | |
40 | #include "wx/textdlg.h" | |
d8e41d42 | 41 | #include "wx/listbox.h" |
8ce88dfa | 42 | #include "wx/filedlg.h" |
69c33c6c VZ |
43 | #endif |
44 | ||
d8e41d42 VZ |
45 | #include "wx/txtstrm.h" |
46 | ||
69c33c6c VZ |
47 | #include "wx/process.h" |
48 | ||
6ba63600 VZ |
49 | #include "wx/mimetype.h" |
50 | ||
d93c719a VZ |
51 | #ifdef __WINDOWS__ |
52 | #include "wx/dde.h" | |
53 | #endif // __WINDOWS__ | |
54 | ||
69c33c6c VZ |
55 | // ---------------------------------------------------------------------------- |
56 | // private classes | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // Define a new application type, each program should derive a class from wxApp | |
60 | class MyApp : public wxApp | |
61 | { | |
62 | public: | |
63 | // override base class virtuals | |
64 | // ---------------------------- | |
65 | ||
66 | // this one is called on application startup and is a good place for the app | |
67 | // initialization (doing it here and not in the ctor allows to have an error | |
68 | // return: if OnInit() returns false, the application terminates) | |
69 | virtual bool OnInit(); | |
70 | }; | |
71 | ||
cd6ce4a9 VZ |
72 | // Define an array of process pointers used by MyFrame |
73 | class MyPipedProcess; | |
74 | WX_DEFINE_ARRAY(MyPipedProcess *, MyProcessesArray); | |
75 | ||
69c33c6c VZ |
76 | // Define a new frame type: this is going to be our main frame |
77 | class MyFrame : public wxFrame | |
78 | { | |
79 | public: | |
80 | // ctor(s) | |
81 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
82 | ||
83 | // event handlers (these functions should _not_ be virtual) | |
84 | void OnQuit(wxCommandEvent& event); | |
85 | ||
d8e41d42 VZ |
86 | void OnClear(wxCommandEvent& event); |
87 | ||
69c33c6c VZ |
88 | void OnSyncExec(wxCommandEvent& event); |
89 | void OnAsyncExec(wxCommandEvent& event); | |
90 | void OnShell(wxCommandEvent& event); | |
d8e41d42 | 91 | void OnExecWithRedirect(wxCommandEvent& event); |
f6bcfd97 BP |
92 | void OnExecWithPipe(wxCommandEvent& event); |
93 | ||
6ba63600 VZ |
94 | void OnFileExec(wxCommandEvent& event); |
95 | ||
d93c719a | 96 | void OnDDEExec(wxCommandEvent& event); |
69c33c6c VZ |
97 | |
98 | void OnAbout(wxCommandEvent& event); | |
99 | ||
cd6ce4a9 VZ |
100 | // polling output of async processes |
101 | void OnIdle(wxIdleEvent& event); | |
102 | ||
d8e41d42 | 103 | // for MyPipedProcess |
f6bcfd97 | 104 | void OnProcessTerminated(MyPipedProcess *process); |
d8e41d42 VZ |
105 | wxListBox *GetLogListBox() const { return m_lbox; } |
106 | ||
69c33c6c | 107 | private: |
f6bcfd97 BP |
108 | void ShowOutput(const wxString& cmd, |
109 | const wxArrayString& output, | |
110 | const wxString& title); | |
111 | ||
6ba63600 VZ |
112 | void DoAsyncExec(const wxString& cmd); |
113 | ||
69c33c6c VZ |
114 | wxString m_cmdLast; |
115 | ||
d8e41d42 VZ |
116 | wxListBox *m_lbox; |
117 | ||
cd6ce4a9 VZ |
118 | MyProcessesArray m_running; |
119 | ||
69c33c6c VZ |
120 | // any class wishing to process wxWindows events must use this macro |
121 | DECLARE_EVENT_TABLE() | |
122 | }; | |
123 | ||
124 | // This is the handler for process termination events | |
125 | class MyProcess : public wxProcess | |
126 | { | |
127 | public: | |
d8e41d42 | 128 | MyProcess(MyFrame *parent, const wxString& cmd) |
69c33c6c VZ |
129 | : wxProcess(parent), m_cmd(cmd) |
130 | { | |
131 | m_parent = parent; | |
132 | } | |
133 | ||
134 | // instead of overriding this virtual function we might as well process the | |
135 | // event from it in the frame class - this might be more convenient in some | |
136 | // cases | |
137 | virtual void OnTerminate(int pid, int status); | |
138 | ||
d8e41d42 VZ |
139 | protected: |
140 | MyFrame *m_parent; | |
69c33c6c VZ |
141 | wxString m_cmd; |
142 | }; | |
143 | ||
d8e41d42 VZ |
144 | // A specialization of MyProcess for redirecting the output |
145 | class MyPipedProcess : public MyProcess | |
146 | { | |
147 | public: | |
148 | MyPipedProcess(MyFrame *parent, const wxString& cmd) | |
149 | : MyProcess(parent, cmd) | |
150 | { | |
cd6ce4a9 | 151 | Redirect(); |
d8e41d42 VZ |
152 | } |
153 | ||
154 | virtual void OnTerminate(int pid, int status); | |
cd6ce4a9 | 155 | |
f6bcfd97 BP |
156 | virtual bool HasInput(); |
157 | }; | |
158 | ||
159 | // A version of MyPipedProcess which also sends input to the stdin of the | |
160 | // child process | |
161 | class MyPipedProcess2 : public MyPipedProcess | |
162 | { | |
163 | public: | |
164 | MyPipedProcess2(MyFrame *parent, const wxString& cmd, const wxString& input) | |
165 | : MyPipedProcess(parent, cmd), m_input(input) | |
166 | { | |
167 | } | |
168 | ||
169 | virtual bool HasInput(); | |
170 | ||
171 | private: | |
172 | wxString m_input; | |
d8e41d42 VZ |
173 | }; |
174 | ||
69c33c6c VZ |
175 | // ---------------------------------------------------------------------------- |
176 | // constants | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
179 | // IDs for the controls and the menu commands | |
180 | enum | |
181 | { | |
182 | // menu items | |
183 | Exec_Quit = 100, | |
d8e41d42 | 184 | Exec_ClearLog, |
69c33c6c VZ |
185 | Exec_SyncExec = 200, |
186 | Exec_AsyncExec, | |
187 | Exec_Shell, | |
6ba63600 | 188 | Exec_OpenFile, |
d93c719a | 189 | Exec_DDEExec, |
d8e41d42 | 190 | Exec_Redirect, |
f6bcfd97 | 191 | Exec_Pipe, |
69c33c6c VZ |
192 | Exec_About = 300 |
193 | }; | |
194 | ||
d93c719a VZ |
195 | static const wxChar *DIALOG_TITLE = _T("Exec sample"); |
196 | ||
69c33c6c VZ |
197 | // ---------------------------------------------------------------------------- |
198 | // event tables and other macros for wxWindows | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
201 | // the event tables connect the wxWindows events with the functions (event | |
202 | // handlers) which process them. It can be also done at run-time, but for the | |
203 | // simple menu events like this the static method is much simpler. | |
204 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
205 | EVT_MENU(Exec_Quit, MyFrame::OnQuit) | |
d8e41d42 | 206 | EVT_MENU(Exec_ClearLog, MyFrame::OnClear) |
69c33c6c VZ |
207 | |
208 | EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec) | |
209 | EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec) | |
210 | EVT_MENU(Exec_Shell, MyFrame::OnShell) | |
d8e41d42 | 211 | EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect) |
f6bcfd97 BP |
212 | EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe) |
213 | ||
6ba63600 VZ |
214 | EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec) |
215 | ||
d93c719a | 216 | EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec) |
69c33c6c VZ |
217 | |
218 | EVT_MENU(Exec_About, MyFrame::OnAbout) | |
cd6ce4a9 VZ |
219 | |
220 | EVT_IDLE(MyFrame::OnIdle) | |
69c33c6c VZ |
221 | END_EVENT_TABLE() |
222 | ||
223 | // Create a new application object: this macro will allow wxWindows to create | |
224 | // the application object during program execution (it's better than using a | |
225 | // static object for many reasons) and also declares the accessor function | |
226 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
227 | // not wxApp) | |
228 | IMPLEMENT_APP(MyApp) | |
229 | ||
230 | // ============================================================================ | |
231 | // implementation | |
232 | // ============================================================================ | |
233 | ||
234 | // ---------------------------------------------------------------------------- | |
235 | // the application class | |
236 | // ---------------------------------------------------------------------------- | |
237 | ||
238 | // `Main program' equivalent: the program execution "starts" here | |
239 | bool MyApp::OnInit() | |
240 | { | |
241 | // Create the main application window | |
242 | MyFrame *frame = new MyFrame(_T("Exec wxWindows sample"), | |
243 | wxDefaultPosition, wxSize(500, 140)); | |
244 | ||
245 | // Show it and tell the application that it's our main window | |
246 | frame->Show(TRUE); | |
247 | SetTopWindow(frame); | |
248 | ||
249 | // success: wxApp::OnRun() will be called which will enter the main message | |
250 | // loop and the application will run. If we returned FALSE here, the | |
251 | // application would exit immediately. | |
252 | return TRUE; | |
253 | } | |
254 | ||
255 | // ---------------------------------------------------------------------------- | |
256 | // main frame | |
257 | // ---------------------------------------------------------------------------- | |
258 | ||
259 | // frame constructor | |
260 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
261 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
262 | { | |
263 | #ifdef __WXMAC__ | |
264 | // we need this in order to allow the about menu relocation, since ABOUT is | |
265 | // not the default id of the about menu | |
266 | wxApp::s_macAboutMenuItemId = Exec_About; | |
267 | #endif | |
268 | ||
69c33c6c VZ |
269 | // create a menu bar |
270 | wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF); | |
d8e41d42 VZ |
271 | menuFile->Append(Exec_ClearLog, _T("&Clear log\tCtrl-C"), |
272 | _T("Clear the log window")); | |
273 | menuFile->AppendSeparator(); | |
69c33c6c VZ |
274 | menuFile->Append(Exec_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
275 | ||
276 | wxMenu *execMenu = new wxMenu; | |
277 | execMenu->Append(Exec_SyncExec, _T("Sync &execution...\tCtrl-E"), | |
278 | _T("Launch a program and return when it terminates")); | |
279 | execMenu->Append(Exec_AsyncExec, _T("&Async execution...\tCtrl-A"), | |
280 | _T("Launch a program and return immediately")); | |
281 | execMenu->Append(Exec_Shell, _T("Execute &shell command...\tCtrl-S"), | |
282 | _T("Launch a shell and execute a command in it")); | |
f6bcfd97 | 283 | execMenu->AppendSeparator(); |
d8e41d42 VZ |
284 | execMenu->Append(Exec_Redirect, _T("Capture command &output...\tCtrl-O"), |
285 | _T("Launch a program and capture its output")); | |
f6bcfd97 BP |
286 | execMenu->Append(Exec_Pipe, _T("&Pipe through command...\tCtrl-P"), |
287 | _T("Pipe a string through a filter")); | |
69c33c6c | 288 | |
6ba63600 VZ |
289 | execMenu->AppendSeparator(); |
290 | execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"), | |
291 | _T("Launch the command to open this kind of files")); | |
d93c719a VZ |
292 | #ifdef __WINDOWS__ |
293 | execMenu->AppendSeparator(); | |
294 | execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D")); | |
295 | #endif | |
296 | ||
69c33c6c VZ |
297 | wxMenu *helpMenu = new wxMenu(_T(""), wxMENU_TEAROFF); |
298 | helpMenu->Append(Exec_About, _T("&About...\tF1"), _T("Show about dialog")); | |
299 | ||
300 | // now append the freshly created menu to the menu bar... | |
301 | wxMenuBar *menuBar = new wxMenuBar(); | |
302 | menuBar->Append(menuFile, _T("&File")); | |
303 | menuBar->Append(execMenu, _T("&Exec")); | |
304 | menuBar->Append(helpMenu, _T("&Help")); | |
305 | ||
306 | // ... and attach this menu bar to the frame | |
307 | SetMenuBar(menuBar); | |
308 | ||
9121bed2 | 309 | // create the listbox in which we will show misc messages as they come |
d8e41d42 | 310 | m_lbox = new wxListBox(this, -1); |
8ce88dfa VZ |
311 | wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, |
312 | wxFONTWEIGHT_NORMAL); | |
313 | if ( font.Ok() ) | |
314 | m_lbox->SetFont(font); | |
9121bed2 | 315 | |
69c33c6c VZ |
316 | #if wxUSE_STATUSBAR |
317 | // create a status bar just for fun (by default with 1 pane only) | |
e680a378 | 318 | CreateStatusBar(); |
9121bed2 | 319 | SetStatusText(_T("Welcome to wxWindows exec sample!")); |
69c33c6c VZ |
320 | #endif // wxUSE_STATUSBAR |
321 | } | |
322 | ||
323 | ||
324 | // event handlers | |
325 | ||
326 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
327 | { | |
328 | // TRUE is to force the frame to close | |
329 | Close(TRUE); | |
330 | } | |
331 | ||
d8e41d42 VZ |
332 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
333 | { | |
334 | m_lbox->Clear(); | |
335 | } | |
336 | ||
69c33c6c VZ |
337 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
338 | { | |
339 |