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