1. some DDE tests in exec
[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 SetIcon(wxICON(mondrian));
194
195 // create a menu bar
196 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
197 menuFile->Append(Exec_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
198
199 wxMenu *execMenu = new wxMenu;
200 execMenu->Append(Exec_SyncExec, _T("Sync &execution...\tCtrl-E"),
201 _T("Launch a program and return when it terminates"));
202 execMenu->Append(Exec_AsyncExec, _T("&Async execution...\tCtrl-A"),
203 _T("Launch a program and return immediately"));
204 execMenu->Append(Exec_Shell, _T("Execute &shell command...\tCtrl-S"),
205 _T("Launch a shell and execute a command in it"));
206
207 #ifdef __WINDOWS__
208 execMenu->AppendSeparator();
209 execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D"));
210 #endif
211
212 wxMenu *helpMenu = new wxMenu(_T(""), wxMENU_TEAROFF);
213 helpMenu->Append(Exec_About, _T("&About...\tF1"), _T("Show about dialog"));
214
215 // now append the freshly created menu to the menu bar...
216 wxMenuBar *menuBar = new wxMenuBar();
217 menuBar->Append(menuFile, _T("&File"));
218 menuBar->Append(execMenu, _T("&Exec"));
219 menuBar->Append(helpMenu, _T("&Help"));
220
221 // ... and attach this menu bar to the frame
222 SetMenuBar(menuBar);
223
224 #if wxUSE_STATUSBAR
225 // create a status bar just for fun (by default with 1 pane only)
226 CreateStatusBar(2);
227 SetStatusText(_T("Welcome to wxWindows!"));
228 #endif // wxUSE_STATUSBAR
229 }
230
231
232 // event handlers
233
234 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
235 {
236 // TRUE is to force the frame to close
237 Close(TRUE);
238 }
239
240 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
241 {
242 wxMessageBox(_T("Exec sample\n© 2000 Vadim Zeitlin"),
243 _T("About Exec"), wxOK | wxICON_INFORMATION, this);
244 }
245
246 void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event))
247 {
248 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
249 DIALOG_TITLE,
250 m_cmdLast);
251
252 if ( !cmd )
253 return;
254
255 int code = wxExecute(cmd, TRUE /* sync */);
256 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
257 cmd.c_str(), code);
258 m_cmdLast = cmd;
259 }
260
261 void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event))
262 {
263 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
264 DIALOG_TITLE,
265 m_cmdLast);
266
267 if ( !cmd )
268 return;
269
270 wxProcess *process = new MyProcess(this, cmd);
271 if ( !wxExecute(cmd, FALSE /* async */, process) )
272 {
273 wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
274
275 delete process;
276 }
277 else
278 {
279 m_cmdLast = cmd;
280 }
281 }
282
283 void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event))
284 {
285 wxString cmd = wxGetTextFromUser(_T("Enter the command: "),
286 DIALOG_TITLE,
287 m_cmdLast);
288
289 if ( !cmd )
290 return;
291
292 int code = wxShell(cmd);
293 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
294 cmd.c_str(), code);
295 m_cmdLast = cmd;
296 }
297
298 void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event))
299 {
300 #ifdef __WINDOWS__
301 wxString server = wxGetTextFromUser(_T("Server to connect to:"),
302 DIALOG_TITLE, _T("IExplore"));
303 if ( !server )
304 return;
305
306 wxString topic = wxGetTextFromUser(_T("DDE topic:"),
307 DIALOG_TITLE, _T("WWW_OpenURL"));
308 if ( !topic )
309 return;
310
311 wxString cmd = wxGetTextFromUser(_T("DDE command:"),
312 DIALOG_TITLE,
313 _T("\"file:F:\\wxWindows\\samples\\"
314 "image\\horse.gif\",,-1,,,,,"));
315 if ( !cmd )
316 return;
317
318 wxDDEClient client;
319 wxConnectionBase *conn = client.MakeConnection("", server, topic);
320 if ( !conn )
321 {
322 wxLogError(_T("Failed to connect to the DDE server '%s'."),
323 server.c_str());
324 }
325 else
326 {
327 if ( !conn->Execute(cmd) )
328 {
329 wxLogError(_T("Failed to execute command '%s' via DDE."),
330 cmd.c_str());
331 }
332 else
333 {
334 wxLogStatus(_T("Successfully executed DDE command"));
335 }
336 }
337 #endif // __WINDOWS__
338 }
339
340 // ----------------------------------------------------------------------------
341 // MyProcess
342 // ----------------------------------------------------------------------------
343
344 void MyProcess::OnTerminate(int pid, int status)
345 {
346 wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."),
347 pid, m_cmd.c_str(), status);
348
349 // we're not needed any more
350 delete this;
351 }