]>
git.saurik.com Git - wxWidgets.git/blob - samples/exec/exec.cpp
b508cc9efde510c87be49104be57e174eddc7557
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
40 #include "wx/process.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // Define a new application type, each program should derive a class from wxApp
47 class MyApp
: public wxApp
50 // override base class virtuals
51 // ----------------------------
53 // this one is called on application startup and is a good place for the app
54 // initialization (doing it here and not in the ctor allows to have an error
55 // return: if OnInit() returns false, the application terminates)
56 virtual bool OnInit();
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame
: public wxFrame
64 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
66 // event handlers (these functions should _not_ be virtual)
67 void OnQuit(wxCommandEvent
& event
);
69 void OnSyncExec(wxCommandEvent
& event
);
70 void OnAsyncExec(wxCommandEvent
& event
);
71 void OnShell(wxCommandEvent
& event
);
73 void OnAbout(wxCommandEvent
& event
);
78 // any class wishing to process wxWindows events must use this macro
82 // This is the handler for process termination events
83 class MyProcess
: public wxProcess
86 MyProcess(wxFrame
*parent
, const wxString
& cmd
)
87 : wxProcess(parent
), m_cmd(cmd
)
92 // instead of overriding this virtual function we might as well process the
93 // event from it in the frame class - this might be more convenient in some
95 virtual void OnTerminate(int pid
, int status
);
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // IDs for the controls and the menu commands
117 // ----------------------------------------------------------------------------
118 // event tables and other macros for wxWindows
119 // ----------------------------------------------------------------------------
121 // the event tables connect the wxWindows events with the functions (event
122 // handlers) which process them. It can be also done at run-time, but for the
123 // simple menu events like this the static method is much simpler.
124 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
125 EVT_MENU(Exec_Quit
, MyFrame::OnQuit
)
127 EVT_MENU(Exec_SyncExec
, MyFrame::OnSyncExec
)
128 EVT_MENU(Exec_AsyncExec
, MyFrame::OnAsyncExec
)
129 EVT_MENU(Exec_Shell
, MyFrame::OnShell
)
131 EVT_MENU(Exec_About
, MyFrame::OnAbout
)
134 // Create a new application object: this macro will allow wxWindows to create
135 // the application object during program execution (it's better than using a
136 // static object for many reasons) and also declares the accessor function
137 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
141 // ============================================================================
143 // ============================================================================
145 // ----------------------------------------------------------------------------
146 // the application class
147 // ----------------------------------------------------------------------------
149 // `Main program' equivalent: the program execution "starts" here
152 // Create the main application window
153 MyFrame
*frame
= new MyFrame(_T("Exec wxWindows sample"),
154 wxDefaultPosition
, wxSize(500, 140));
156 // Show it and tell the application that it's our main window
160 // success: wxApp::OnRun() will be called which will enter the main message
161 // loop and the application will run. If we returned FALSE here, the
162 // application would exit immediately.
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
171 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
172 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
175 // we need this in order to allow the about menu relocation, since ABOUT is
176 // not the default id of the about menu
177 wxApp::s_macAboutMenuItemId
= Exec_About
;
180 // set the frame icon
181 SetIcon(wxICON(mondrian
));
184 wxMenu
*menuFile
= new wxMenu(_T(""), wxMENU_TEAROFF
);
185 menuFile
->Append(Exec_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
187 wxMenu
*execMenu
= new wxMenu
;
188 execMenu
->Append(Exec_SyncExec
, _T("Sync &execution...\tCtrl-E"),
189 _T("Launch a program and return when it terminates"));
190 execMenu
->Append(Exec_AsyncExec
, _T("&Async execution...\tCtrl-A"),
191 _T("Launch a program and return immediately"));
192 execMenu
->Append(Exec_Shell
, _T("Execute &shell command...\tCtrl-S"),
193 _T("Launch a shell and execute a command in it"));
195 wxMenu
*helpMenu
= new wxMenu(_T(""), wxMENU_TEAROFF
);
196 helpMenu
->Append(Exec_About
, _T("&About...\tF1"), _T("Show about dialog"));
198 // now append the freshly created menu to the menu bar...
199 wxMenuBar
*menuBar
= new wxMenuBar();
200 menuBar
->Append(menuFile
, _T("&File"));
201 menuBar
->Append(execMenu
, _T("&Exec"));
202 menuBar
->Append(helpMenu
, _T("&Help"));
204 // ... and attach this menu bar to the frame
208 // create a status bar just for fun (by default with 1 pane only)
210 SetStatusText(_T("Welcome to wxWindows!"));
211 #endif // wxUSE_STATUSBAR
217 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
219 // TRUE is to force the frame to close
223 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
225 wxMessageBox(_T("Exec sample\n© 2000 Vadim Zeitlin"),
226 _T("About Exec"), wxOK
| wxICON_INFORMATION
, this);
229 void MyFrame::OnSyncExec(wxCommandEvent
& WXUNUSED(event
))
231 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
238 int code
= wxExecute(cmd
, TRUE
/* sync */);
239 wxLogStatus(_T("Process '%s' terminated with exit code %d."),
244 void MyFrame::OnAsyncExec(wxCommandEvent
& WXUNUSED(event
))
246 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
253 wxProcess
*process
= new MyProcess(this, cmd
);
254 if ( !wxExecute(cmd
, FALSE
/* async */, process
) )
256 wxLogError(_T("Execution of '%s' failed."), cmd
.c_str());
266 void MyFrame::OnShell(wxCommandEvent
& WXUNUSED(event
))
268 wxString cmd
= wxGetTextFromUser(_T("Enter the command: "),
275 int code
= wxShell(cmd
);
276 wxLogStatus(_T("Shell command '%s' terminated with exit code %d."),
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 void MyProcess::OnTerminate(int pid
, int status
)
287 wxLogStatus(m_parent
, _T("Process %u ('%s') terminated with exit code %d."),
288 pid
, m_cmd
.c_str(), status
);
290 // we're not needed any more