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