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