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