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