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