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