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