]>
git.saurik.com Git - wxWidgets.git/blob - samples/exec/exec.cpp
dd794c9b34460ab909a69ba8db1c1e2fbcb1b211
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: exec sample demonstrates wxExecute and related functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "exec.cpp"
22 #pragma interface "exec.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
39 #include "wx/msgdlg.h"
40 #include "wx/textdlg.h"
43 #include "wx/process.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // Define a new application type, each program should derive a class from wxApp
54 class MyApp
: public wxApp
57 // override base class virtuals
58 // ----------------------------
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();
66 // Define a new frame type: this is going to be our main frame
67 class MyFrame
: public wxFrame
71 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
73 // event handlers (these functions should _not_ be virtual)
74 void OnQuit(wxCommandEvent
& event
);
76 void OnSyncExec(wxCommandEvent
& event
);
77 void OnAsyncExec(wxCommandEvent
& event
);
78 void OnShell(wxCommandEvent
& event
);
79 void OnDDEExec(wxCommandEvent
& event
);
81 void OnAbout(wxCommandEvent
& event
);
86 // any class wishing to process wxWindows events must use this macro
90 // This is the handler for process termination events
91 class MyProcess
: public wxProcess
94 MyProcess(wxFrame
*parent
, const wxString
& cmd
)
95 : wxProcess(parent
), m_cmd(cmd
)
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
103 virtual void OnTerminate(int pid
, int status
);
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 // IDs for the controls and the menu commands
126 static const wxChar
*DIALOG_TITLE
= _T("Exec sample");
128 // ----------------------------------------------------------------------------
129 // event tables and other macros for wxWindows
130 // ----------------------------------------------------------------------------
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
)
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
)
143 EVT_MENU(Exec_About
, MyFrame::OnAbout
)
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
153 // ============================================================================
155 // ============================================================================
157 // ----------------------------------------------------------------------------
158 // the application class
159 // ----------------------------------------------------------------------------
161 // `Main program' equivalent: the program execution "starts" here
164 // Create the main application window
165 MyFrame
*frame
= new MyFrame(_T("Exec wxWindows sample"),
166 wxDefaultPosition
, wxSize(500, 140));
168 // Show it and tell the application that it's our main window
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.
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
183 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
184 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
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
;
192 // set the frame icon
194 SetIcon(wxICON(mondrian
));
198 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
199 menuFile
->Append(Exec_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
210 execMenu
->AppendSeparator();
211 execMenu
->Append(Exec_DDEExec
, _T("Execute command via &DDE...\tCtrl-D"));
214 wxMenu
*helpMenu
= new wxMenu(_T(""), wxMENU_TEAROFF
);
215 helpMenu
->Append(Exec_About
, _T("&About...\tF1"), _T("Show about dialog"));
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"));
223 // ... and attach this menu bar to the frame
226 // create the listbox in which we will show misc messages as they come
227 m_listbox
= new wxListBox(this, -1);
230 // create a status bar just for fun (by default with 1 pane only)
232 SetStatusText(_T("Welcome to wxWindows exec sample!"));
233 #endif // wxUSE_STATUSBAR
239 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
241 // TRUE is to force the frame to close
245 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
247 wxMessageBox(_T("Exec sample\n© 2000 Vadim Zeitlin"),
248 _T("About Exec"), wxOK
| wxICON_INFORMATION
, this);
251 void MyFrame::OnSyncExec(wxCommandEvent
& WXUNUSED(event
))
253 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
260 int code
= wxExecute(cmd
, TRUE
/* sync */);
261 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
266 void MyFrame::OnAsyncExec(wxCommandEvent
& WXUNUSED(event
))
268 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
275 wxProcess
*process
= new MyProcess(this, cmd
);
276 if ( !wxExecute(cmd
, FALSE
/* async */, process
) )
278 wxLogError(_T("Execution of '%s' failed."), cmd
.c_str());
288 void MyFrame::OnShell(wxCommandEvent
& WXUNUSED(event
))
290 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
297 int code
= wxShell(cmd
);
298 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
303 void MyFrame::OnDDEExec(wxCommandEvent
& WXUNUSED(event
))
306 wxString server
= wxGetTextFromUser(_T("Server to connect to:"),
307 DIALOG_TITLE
, _T("IExplore"));
311 wxString topic
= wxGetTextFromUser(_T("DDE topic:"),
312 DIALOG_TITLE
, _T("WWW_OpenURL"));
316 wxString cmd
= wxGetTextFromUser(_T("DDE command:"),
318 _T("\"file:F:\\wxWindows\\samples\\"
319 "image\\horse.gif\",,-1,,,,,"));
324 wxConnectionBase
*conn
= client
.MakeConnection("", server
, topic
);
327 wxLogError(_T("Failed to connect to the DDE server '%s'."),
332 if ( !conn
->Execute(cmd
) )
334 wxLogError(_T("Failed to execute command '%s' via DDE."),
339 wxLogStatus(_T("Successfully executed DDE command"));
342 #endif // __WINDOWS__
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 void MyProcess::OnTerminate(int pid
, int status
)
351 wxLogStatus(m_parent
, _T("Process %u ('%s') terminated with exit code %d."),
352 pid
, m_cmd
.c_str(), status
);
354 // we're not needed any more