X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6bcfd974ef26faf6f91a62cac09827e09463fd1..32956769e9470c617a968ede7da90423897fdaea:/samples/exec/exec.cpp?ds=sidebyside diff --git a/samples/exec/exec.cpp b/samples/exec/exec.cpp index e8483a828a..b540460720 100644 --- a/samples/exec/exec.cpp +++ b/samples/exec/exec.cpp @@ -39,12 +39,15 @@ #include "wx/msgdlg.h" #include "wx/textdlg.h" #include "wx/listbox.h" + #include "wx/filedlg.h" #endif #include "wx/txtstrm.h" #include "wx/process.h" +#include "wx/mimetype.h" + #ifdef __WINDOWS__ #include "wx/dde.h" #endif // __WINDOWS__ @@ -88,6 +91,8 @@ public: void OnExecWithRedirect(wxCommandEvent& event); void OnExecWithPipe(wxCommandEvent& event); + void OnFileExec(wxCommandEvent& event); + void OnDDEExec(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); @@ -104,6 +109,8 @@ private: const wxArrayString& output, const wxString& title); + void DoAsyncExec(const wxString& cmd); + wxString m_cmdLast; wxListBox *m_lbox; @@ -178,6 +185,7 @@ enum Exec_SyncExec = 200, Exec_AsyncExec, Exec_Shell, + Exec_OpenFile, Exec_DDEExec, Exec_Redirect, Exec_Pipe, @@ -203,6 +211,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Exec_Redirect, MyFrame::OnExecWithRedirect) EVT_MENU(Exec_Pipe, MyFrame::OnExecWithPipe) + EVT_MENU(Exec_OpenFile, MyFrame::OnFileExec) + EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec) EVT_MENU(Exec_About, MyFrame::OnAbout) @@ -276,6 +286,9 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) execMenu->Append(Exec_Pipe, _T("&Pipe through command...\tCtrl-P"), _T("Pipe a string through a filter")); + execMenu->AppendSeparator(); + execMenu->Append(Exec_OpenFile, _T("Open &file...\tCtrl-F"), + _T("Launch the command to open this kind of files")); #ifdef __WINDOWS__ execMenu->AppendSeparator(); execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D")); @@ -295,6 +308,10 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) // create the listbox in which we will show misc messages as they come m_lbox = new wxListBox(this, -1); + wxFont font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, + wxFONTWEIGHT_NORMAL); + if ( font.Ok() ) + m_lbox->SetFont(font); #if wxUSE_STATUSBAR // create a status bar just for fun (by default with 1 pane only) @@ -323,6 +340,24 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) _T("About Exec"), wxOK | wxICON_INFORMATION, this); } +void MyFrame::DoAsyncExec(const wxString& cmd) +{ + wxProcess *process = new MyProcess(this, cmd); + long pid = wxExecute(cmd, FALSE /* async */, process); + if ( !pid ) + { + wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); + + delete process; + } + else + { + wxLogStatus(_T("Process %ld (%s) launched."), pid, cmd.c_str()); + + m_cmdLast = cmd; + } +} + void MyFrame::OnSyncExec(wxCommandEvent& WXUNUSED(event)) { wxString cmd = wxGetTextFromUser(_T("Enter the command: "), @@ -350,20 +385,7 @@ void MyFrame::OnAsyncExec(wxCommandEvent& WXUNUSED(event)) if ( !cmd ) return; - wxProcess *process = new MyProcess(this, cmd); - long pid = wxExecute(cmd, FALSE /* async */, process); - if ( !pid ) - { - wxLogError(_T("Execution of '%s' failed."), cmd.c_str()); - - delete process; - } - else - { - wxLogStatus(_T("Process %ld (%s) launched."), pid, cmd.c_str()); - - m_cmdLast = cmd; - } + DoAsyncExec(cmd); } void MyFrame::OnShell(wxCommandEvent& WXUNUSED(event)) @@ -474,6 +496,39 @@ void MyFrame::OnExecWithPipe(wxCommandEvent& WXUNUSED(event)) m_cmdLast = cmd; } +void MyFrame::OnFileExec(wxCommandEvent& event) +{ + static wxString s_filename; + + wxString filename = wxLoadFileSelector(_T("file"), _T(""), s_filename); + if ( !filename ) + return; + + s_filename = filename; + + wxString ext = filename.AfterFirst(_T('.')); + wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); + if ( !ft ) + { + wxLogError(_T("Impossible to determine the file type for extension '%s'"), + ext.c_str()); + return; + } + + wxString cmd; + bool ok = ft->GetOpenCommand(&cmd, + wxFileType::MessageParameters(filename, _T(""))); + delete ft; + if ( !ok ) + { + wxLogError(_T("Impossible to find out how to open files of extension '%s'"), + ext.c_str()); + return; + } + + DoAsyncExec(cmd); +} + void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event)) { #ifdef __WINDOWS__