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