]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/exec/exec.cpp
Correct file format errors
[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#if defined(__GNUG__) && !defined(__APPLE__)
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/log.h"
37 #include "wx/frame.h"
38 #include "wx/panel.h"
39
40 #include "wx/timer.h"
41
42 #include "wx/utils.h"
43 #include "wx/menu.h"
44
45 #include "wx/msgdlg.h"
46 #include "wx/textdlg.h"
47 #include "wx/filedlg.h"
48 #include "wx/choicdlg.h"
49
50 #include "wx/button.h"
51 #include "wx/textctrl.h"
52 #include "wx/listbox.h"
53
54 #include "wx/sizer.h"
55#endif
56
57#include "wx/txtstrm.h"
58#include "wx/numdlg.h"
59
60#include "wx/process.h"
61
62#include "wx/mimetype.h"
63
64#ifdef __WINDOWS__
65 #include "wx/dde.h"
66#endif // __WINDOWS__
67
68// ----------------------------------------------------------------------------
69// the usual application and main frame classes
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
85// Define an array of process pointers used by MyFrame
86class MyPipedProcess;
87WX_DEFINE_ARRAY_PTR(MyPipedProcess *, MyProcessesArray);
88
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
99 void OnKill(wxCommandEvent& event);
100
101 void OnClear(wxCommandEvent& event);
102
103 void OnSyncExec(wxCommandEvent& event);
104 void OnAsyncExec(wxCommandEvent& event);
105 void OnShell(wxCommandEvent& event);
106 void OnExecWithRedirect(wxCommandEvent& event);
107 void OnExecWithPipe(wxCommandEvent& event);
108
109 void OnPOpen(wxCommandEvent& event);
110
111 void OnFileExec(wxCommandEvent& event);
112
113 void OnAbout(wxCommandEvent& event);
114
115 // polling output of async processes
116 void OnTimer(wxTimerEvent& event);
117 void OnIdle(wxIdleEvent& event);
118
119 // for MyPipedProcess
120 void OnProcessTerminated(MyPipedProcess *process);
121 wxListBox *GetLogListBox() const { return m_lbox; }
122
123private:
124 void ShowOutput(const wxString& cmd,
125 const wxArrayString& output,
126 const wxString& title);
127
128 void DoAsyncExec(const wxString& cmd);
129
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
155 // the PID of the last process we launched asynchronously
156 long m_pidLast;
157
158 // last command we executed
159 wxString m_cmdLast;
160
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
173 wxListBox *m_lbox;
174
175 MyProcessesArray m_running;
176
177 // the idle event wake up timer
178 wxTimer m_timerIdleWakeUp;
179
180 // any class wishing to process wxWindows events must use this macro
181 DECLARE_EVENT_TABLE()
182};
183
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:
196 void OnTextEnter(wxCommandEvent& WXUNUSED(event)) { DoSend(); }
197 void OnBtnSend(wxCommandEvent& WXUNUSED(event)) { DoSend(); }
198 void OnBtnGet(wxCommandEvent& WXUNUSED(event)) { DoGet(); }
199
200 void OnClose(wxCloseEvent& event);
201
202 void OnProcessTerm(wxProcessEvent& event);
203
204 void DoSend()
205 {
206 m_out.WriteString(m_textIn->GetValue() + _T('\n'));
207 m_textIn->Clear();
208
209 DoGet();
210 }
211
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
230// This is the handler for process termination events
231class MyProcess : public wxProcess
232{
233public:
234 MyProcess(MyFrame *parent, const wxString& cmd)
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
245protected:
246 MyFrame *m_parent;
247 wxString m_cmd;
248};
249
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 {
257 Redirect();
258 }
259
260 virtual void OnTerminate(int pid, int status);
261
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;
279};
280
281// ----------------------------------------------------------------------------
282// constants
283// ----------------------------------------------------------------------------
284
285// IDs for the controls and the menu commands
286enum
287{
288 // menu items
289 Exec_Quit = 100,
290 Exec_Kill,
291 Exec_ClearLog,
292 Exec_SyncExec = 200,
293 Exec_AsyncExec,
294 Exec_Shell,
295 Exec_POpen,
296 Exec_OpenFile,
297 Exec_DDEExec,
298 Exec_DDERequest,
299 Exec_Redirect,
300 Exec_Pipe,
301 Exec_About = 300,
302
303 // control ids
304 Exec_Btn_Send = 1000,
305 Exec_Btn_Get
306};
307
308static const wxChar *DIALOG_TITLE = _T("Exec sample");
309
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)
319 EVT_MENU(Exec_Kill, MyFrame::OnKill)
320 EVT_MENU(Exec_ClearLog, MyFrame::OnClear)
321
322 EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec)
323 EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec)
324 EVT_MENU(Exec_Shell, MyFrame::OnShell)
325 EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect)
326 EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe)
327
328 EVT_MENU(Exec_POpen, MyFrame::OnPOpen)
329
330 EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec)
331
332#ifdef __WINDOWS__
333 EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec)
334 EVT_MENU(Exec_DDERequest, MyFrame::OnDDERequest)
335#endif // __WINDOWS__
336
337 EVT_MENU(Exec_About, MyFrame::OnAbout)
338
339 EVT_IDLE(MyFrame::OnIdle)
340
341 EVT_TIMER(-1, MyFrame::OnTimer)
342END_EVENT_TABLE()
343
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)
351
352 EVT_END_PROCESS(-1, MyPipeFrame::OnProcessTerm)
353END_EVENT_TABLE()
354
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
391#ifdef __VISUALC__
392#pragma warning(disable: 4355) // this used in base member initializer list
393#endif
394
395// frame constructor
396MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
397 : wxFrame((wxFrame *)NULL, -1, title, pos, size),
398 m_timerIdleWakeUp(this)
399{
400 m_pidLast = 0;
401
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
408 // create a menu bar
409 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
410 menuFile->Append(Exec_Kill, _T("&Kill process...\tCtrl-K"),
411 _T("Kill a process by PID"));
412 menuFile->AppendSeparator();
413 menuFile->Append(Exec_ClearLog, _T("&Clear log\tCtrl-C"),
414 _T("Clear the log window"));
415 menuFile->AppendSeparator();
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"));
425 execMenu->AppendSeparator();
426 execMenu->Append(Exec_Redirect, _T("Capture command &output...\tCtrl-O"),
427 _T("Launch a program and capture its output"));
428 execMenu->Append(Exec_Pipe, _T("&Pipe through command..."),
429 _T("Pipe a string through a filter"));
430 execMenu->Append(Exec_POpen, _T("&Open a pipe to a command...\tCtrl-P"),
431 _T("Open a pipe to and from another program"));
432
433 execMenu->AppendSeparator();
434 execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"),
435 _T("Launch the command to open this kind of files"));
436#ifdef __WINDOWS__
437 execMenu->AppendSeparator();
438 execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D"));
439 execMenu->Append(Exec_DDERequest, _T("Send DDE &request...\tCtrl-R"));
440#endif
441
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
454 // create the listbox in which we will show misc messages as they come
455 m_lbox = new wxListBox(this, -1);
456 wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL,
457 wxFONTWEIGHT_NORMAL);
458 if ( font.Ok() )
459 m_lbox->SetFont(font);
460
461#if wxUSE_STATUSBAR
462 // create a status bar just for fun (by default with 1 pane only)
463 CreateStatusBar();
464 SetStatusText(_T("Welcome to wxWindows exec sample!"));
465#endif // wxUSE_STATUSBAR
466}
467
468// ----------------------------------------------------------------------------
469// event handlers: file and help menu
470// ----------------------------------------------------------------------------
471
472void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
473{
474 // TRUE is to force the frame to close
475 Close(TRUE);
476}
477
478void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
479{
480 m_lbox->Clear();
481}
482
483void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
484{
485