Various makefile corrections,
[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 #if wxUSE_STATUSBAR
227 // create a status bar just for fun (by default with 1 pane only)
228 CreateStatusBar();
229 SetStatusText(_T("Welcome to wxWindows!"));
230 #endif // wxUSE_STATUSBAR
231 }
232
233
234 // event handlers
235
236 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
237 {
238 // TRUE is to force the frame to close
239 Close(TRUE);
240 }
241
242 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
243 {
244 wxMessageBox(_T("Exec sample\n© 2000 Vadim Zeitlin"),
245 _T("About Exec"), wxOK | wxICON_INFORMATION, this);
246 }
247
248 void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event))
249 {
250 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
251 DIALOG_TITLE,
252 m_cmdLast);
253
254 if ( !cmd )
255 return;
256
257 int code = wxExecute(cmd, TRUE /* sync */);
258 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
259 cmd.c_str(), code);
260 m_cmdLast = cmd;
261 }
262
263 void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event))
264 {
265 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
266 DIALOG_TITLE,
267 m_cmdLast);
268
269 if ( !cmd )
270 return;
271
272 wxProcess *process = new MyProcess(this, cmd);
273 if ( !wxExecute(cmd, FALSE /* async */, process) )
274 {
275 wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
276
277 delete process;
278 }
279 else
280 {
281 m_cmdLast = cmd;
282 }
283 }
284
285 void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event))
286 {
287 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
288 DIALOG_TITLE,
289 m_cmdLast);
290
291 if ( !cmd )
292 return;
293
294 int code = wxShell(cmd);
295 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
296 cmd.c_str(), code);
297 m_cmdLast = cmd;
298 }
299
300 void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event))
301 {
302 #ifdef __WINDOWS__
303 wxString server = wxGetTextFromUser(_T("Server to connect to:"),
304 DIALOG_TITLE, _T("IExplore"));
305 if ( !server )
306 return;
307
308 wxString topic = wxGetTextFromUser(_T("DDE topic:"),
309 DIALOG_TITLE, _T("WWW_OpenURL"));
310 if ( !topic )
311 return;
312
313 wxString cmd = wxGetTextFromUser(_T("DDE command:"),
314 DIALOG_TITLE,
315 _T("\"file:F:\\wxWindows\\samples\\"
316 "image\\horse.gif\",,-1,,,,,"));
317 if ( !cmd )
318 return;
319
320 wxDDEClient client;
321 wxConnectionBase *conn = client.MakeConnection("", server, topic);
322 if ( !conn )
323 {
324 wxLogError(_T("Failed to connect to the DDE server '%s'."),
325 server.c_str());
326 }
327 else
328 {
329 if ( !conn->Execute(cmd) )
330 {
331 wxLogError(_T("Failed to execute command '%s' via DDE."),
332 cmd.c_str());
333 }
334 else
335 {
336 wxLogStatus(_T("Successfully executed DDE command"));
337 }
338 }
339 #endif // __WINDOWS__
340 }
341
342 // ----------------------------------------------------------------------------
343 // MyProcess
344 // ----------------------------------------------------------------------------
345
346 void MyProcess::OnTerminate(int pid, int status)
347 {
348 wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."),
349 pid, m_cmd.c_str(), status);
350
351 // we're not needed any more
352 delete this;
353 }