]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/exec/exec.cpp
added wxLog::ClearTraceMasks()
[wxWidgets.git] / samples / exec / exec.cpp
... / ...
CommitLineData
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"
38 #include "wx/menu.h"
39 #include "wx/msgdlg.h"
40 #include "wx/textdlg.h"
41 #include "wx/listbox.h"
42 #include "wx/filedlg.h"
43#endif
44
45#include "wx/txtstrm.h"
46
47#include "wx/process.h"
48
49#include "wx/mimetype.h"
50
51#ifdef __WINDOWS__
52 #include "wx/dde.h"
53#endif // __WINDOWS__
54
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
72// Define an array of process pointers used by MyFrame
73class MyPipedProcess;
74WX_DEFINE_ARRAY(MyPipedProcess *, MyProcessesArray);
75
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
86 void OnClear(wxCommandEvent& event);
87
88 void OnSyncExec(wxCommandEvent& event);
89 void OnAsyncExec(wxCommandEvent& event);
90 void OnShell(wxCommandEvent& event);
91 void OnExecWithRedirect(wxCommandEvent& event);
92 void OnExecWithPipe(wxCommandEvent& event);
93
94 void OnFileExec(wxCommandEvent& event);
95
96 void OnAbout(wxCommandEvent& event);
97
98 // polling output of async processes
99 void OnIdle(wxIdleEvent& event);
100
101 // for MyPipedProcess
102 void OnProcessTerminated(MyPipedProcess *process);
103 wxListBox *GetLogListBox() const { return m_lbox; }
104
105private:
106 void ShowOutput(const wxString& cmd,
107 const wxArrayString& output,
108 const wxString& title);
109
110 void DoAsyncExec(const wxString& cmd);
111
112 // last command we executed
113 wxString m_cmdLast;
114
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
127 wxListBox *m_lbox;
128
129 MyProcessesArray m_running;
130
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:
139 MyProcess(MyFrame *parent, const wxString& cmd)
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
150protected:
151 MyFrame *m_parent;
152 wxString m_cmd;
153};
154
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 {
162 Redirect();
163 }
164
165 virtual void OnTerminate(int pid, int status);
166
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;
184};
185
186// ----------------------------------------------------------------------------
187// constants
188// ----------------------------------------------------------------------------
189
190// IDs for the controls and the menu commands
191enum
192{
193 // menu items
194 Exec_Quit = 100,
195 Exec_ClearLog,
196 Exec_SyncExec = 200,
197 Exec_AsyncExec,
198 Exec_Shell,
199 Exec_OpenFile,
200 Exec_DDEExec,
201 Exec_DDERequest,
202 Exec_Redirect,
203 Exec_Pipe,
204 Exec_About = 300
205};
206
207static const wxChar *DIALOG_TITLE = _T("Exec sample");
208
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)
218 EVT_MENU(Exec_ClearLog, MyFrame::OnClear)
219
220 EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec)
221 EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec)
222 EVT_MENU(Exec_Shell, MyFrame::OnShell)
223 EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect)
224 EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe)
225
226 EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec)
227
228 EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec)
229 EVT_MENU(Exec_DDERequest, MyFrame::OnDDERequest)
230
231 EVT_MENU(Exec_About, MyFrame::OnAbout)
232
233 EVT_IDLE(MyFrame::OnIdle)
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
282 // create a menu bar
283 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
284 menuFile->Append(Exec_ClearLog, _T("&Clear log\tCtrl-C"),
285 _T("Clear the log window"));
286 menuFile->AppendSeparator();
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"));
296 execMenu->AppendSeparator();
297 execMenu->Append(Exec_Redirect, _T("Capture command &output...\tCtrl-O"),
298 _T("Launch a program and capture its output"));
299 execMenu->Append(Exec_Pipe, _T("&Pipe through command...\tCtrl-P"),
300 _T("Pipe a string through a filter"));
301
302 execMenu->AppendSeparator();
303 execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"),
304 _T("Launch the command to open this kind of files"));
305#ifdef __WINDOWS__
306 execMenu->AppendSeparator();
307 execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D"));
308 execMenu->Append(Exec_DDERequest, _T("Send DDE &request...\tCtrl-R"));
309#endif
310
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
323 // create the listbox in which we will show misc messages as they come
324 m_lbox = new wxListBox(this, -1);
325 wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL,
326 wxFONTWEIGHT_NORMAL);
327 if ( font.Ok() )
328 m_lbox->SetFont(font);
329
330#if wxUSE_STATUSBAR
331 // create a status bar just for fun (by default with 1 pane only)
332 CreateStatusBar();
333 SetStatusText(_T("Welcome to wxWindows exec sample!"));
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
346void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
347{
348 m_lbox->Clear();
349}
350
351void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
352{
353