]>
git.saurik.com Git - wxWidgets.git/blob - samples/exec/exec.cpp
65957f0d44e043e615720da01d4919f5eafbaefc
   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" 
  45 // ---------------------------------------------------------------------------- 
  47 // ---------------------------------------------------------------------------- 
  49 // Define a new application type, each program should derive a class from wxApp 
  50 class MyApp 
: public wxApp
 
  53     // override base class virtuals 
  54     // ---------------------------- 
  56     // this one is called on application startup and is a good place for the app 
  57     // initialization (doing it here and not in the ctor allows to have an error 
  58     // return: if OnInit() returns false, the application terminates) 
  59     virtual bool OnInit(); 
  62 // Define a new frame type: this is going to be our main frame 
  63 class MyFrame 
: public wxFrame
 
  67     MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
); 
  69     // event handlers (these functions should _not_ be virtual) 
  70     void OnQuit(wxCommandEvent
& event
); 
  72     void OnSyncExec(wxCommandEvent
& event
); 
  73     void OnAsyncExec(wxCommandEvent
& event
); 
  74     void OnShell(wxCommandEvent
& event
); 
  76     void OnAbout(wxCommandEvent
& event
); 
  81     // any class wishing to process wxWindows events must use this macro 
  85 // This is the handler for process termination events 
  86 class MyProcess 
: public wxProcess
 
  89     MyProcess(wxFrame 
*parent
, const wxString
& cmd
) 
  90         : wxProcess(parent
), m_cmd(cmd
) 
  95     // instead of overriding this virtual function we might as well process the 
  96     // event from it in the frame class - this might be more convenient in some 
  98     virtual void OnTerminate(int pid
, int status
); 
 105 // ---------------------------------------------------------------------------- 
 107 // ---------------------------------------------------------------------------- 
 109 // IDs for the controls and the menu commands 
 120 // ---------------------------------------------------------------------------- 
 121 // event tables and other macros for wxWindows 
 122 // ---------------------------------------------------------------------------- 
 124 // the event tables connect the wxWindows events with the functions (event 
 125 // handlers) which process them. It can be also done at run-time, but for the 
 126 // simple menu events like this the static method is much simpler. 
 127 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
) 
 128     EVT_MENU(Exec_Quit
,  MyFrame::OnQuit
) 
 130     EVT_MENU(Exec_SyncExec
, MyFrame::OnSyncExec
) 
 131     EVT_MENU(Exec_AsyncExec
, MyFrame::OnAsyncExec
) 
 132     EVT_MENU(Exec_Shell
, MyFrame::OnShell
) 
 134     EVT_MENU(Exec_About
, MyFrame::OnAbout
) 
 137 // Create a new application object: this macro will allow wxWindows to create 
 138 // the application object during program execution (it's better than using a 
 139 // static object for many reasons) and also declares the accessor function 
 140 // wxGetApp() which will return the reference of the right type (i.e. MyApp and 
 144 // ============================================================================ 
 146 // ============================================================================ 
 148 // ---------------------------------------------------------------------------- 
 149 // the application class 
 150 // ---------------------------------------------------------------------------- 
 152 // `Main program' equivalent: the program execution "starts" here 
 155     // Create the main application window 
 156     MyFrame 
*frame 
= new MyFrame(_T("Exec wxWindows sample"), 
 157                                  wxDefaultPosition
, wxSize(500, 140)); 
 159     // Show it and tell the application that it's our main window 
 163     // success: wxApp::OnRun() will be called which will enter the main message 
 164     // loop and the application will run. If we returned FALSE here, the 
 165     // application would exit immediately. 
 169 // ---------------------------------------------------------------------------- 
 171 // ---------------------------------------------------------------------------- 
 174 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
) 
 175        : wxFrame((wxFrame 
*)NULL
, -1, title
, pos
, size
) 
 178     // we need this in order to allow the about menu relocation, since ABOUT is 
 179     // not the default id of the about menu 
 180     wxApp::s_macAboutMenuItemId 
= Exec_About
; 
 183     // set the frame icon 
 184     SetIcon(wxICON(mondrian
)); 
 187     wxMenu 
*menuFile 
= new wxMenu(_T(""), wxMENU_TEAROFF
); 
 188     menuFile
->Append(Exec_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program")); 
 190     wxMenu 
*execMenu 
= new wxMenu
; 
 191     execMenu
->Append(Exec_SyncExec
, _T("Sync &execution...\tCtrl-E"), 
 192                      _T("Launch a program and return when it terminates")); 
 193     execMenu
->Append(Exec_AsyncExec
, _T("&Async execution...\tCtrl-A"), 
 194                      _T("Launch a program and return immediately")); 
 195     execMenu
->Append(Exec_Shell
, _T("Execute &shell command...\tCtrl-S"), 
 196                      _T("Launch a shell and execute a command in it")); 
 198     wxMenu 
*helpMenu 
= new wxMenu(_T(""), wxMENU_TEAROFF
); 
 199     helpMenu
->Append(Exec_About
, _T("&About...\tF1"), _T("Show about dialog")); 
 201     // now append the freshly created menu to the menu bar... 
 202     wxMenuBar 
*menuBar 
= new wxMenuBar(); 
 203     menuBar
->Append(menuFile
, _T("&File")); 
 204     menuBar
->Append(execMenu
, _T("&Exec")); 
 205     menuBar
->Append(helpMenu
, _T("&Help")); 
 207     // ... and attach this menu bar to the frame 
 211     // create a status bar just for fun (by default with 1 pane only) 
 213     SetStatusText(_T("Welcome to wxWindows!")); 
 214 #endif // wxUSE_STATUSBAR 
 220 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
)) 
 222     // TRUE is to force the frame to close 
 226 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
)) 
 228     wxMessageBox(_T("Exec sample\n© 2000 Vadim Zeitlin"), 
 229                  _T("About Exec"), wxOK 
| wxICON_INFORMATION
, this); 
 232 void MyFrame::OnSyncExec(wxCommandEvent
& WXUNUSED(event
)) 
 234     wxString cmd 
= wxGetTextFromUser(_T("Enter the command: "), 
 241     int code 
= wxExecute(cmd
, TRUE 
/* sync */); 
 242     wxLogStatus(_T("Process '%s' terminated with exit code %d."), 
 247 void MyFrame::OnAsyncExec(wxCommandEvent
& WXUNUSED(event
)) 
 249     wxString cmd 
= wxGetTextFromUser(_T("Enter the command: "), 
 256     wxProcess 
*process 
= new MyProcess(this, cmd
); 
 257     if ( !wxExecute(cmd
, FALSE 
/* async */, process
) ) 
 259         wxLogError(_T("Execution of '%s' failed."), cmd
.c_str()); 
 269 void MyFrame::OnShell(wxCommandEvent
& WXUNUSED(event
)) 
 271     wxString cmd 
= wxGetTextFromUser(_T("Enter the command: "), 
 278     int code 
= wxShell(cmd
); 
 279     wxLogStatus(_T("Shell command '%s' terminated with exit code %d."), 
 284 // ---------------------------------------------------------------------------- 
 286 // ---------------------------------------------------------------------------- 
 288 void MyProcess::OnTerminate(int pid
, int status
) 
 290     wxLogStatus(m_parent
, _T("Process %u ('%s') terminated with exit code %d."), 
 291                 pid
, m_cmd
.c_str(), status
); 
 293     // we're not needed any more