Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages
hugin1/base_wx/MyExternalCmdExecDialog.h
Go to the documentation of this file.00001 // -*- c-basic-offset: 4 -*- 00024 // This class is written based on 'exec' sample of wxWidgets library. 00025 00026 00027 #ifndef _MYEXTERNALCMDEXECDIALOG__H 00028 #define _MYEXTERNALCMDEXECDIALOG__H 00029 00030 #include <hugin_shared.h> 00031 00032 const int HUGIN_EXIT_CODE_CANCELLED = -255; 00033 00034 class MyExternalCmdExecDialog; 00035 class HuginPipedProcess; 00036 WXIMPEX int MyExecuteCommandOnDialog(wxString command, wxString args, wxWindow* parent, wxString title); 00037 00038 //#define HUGIN_EXEC_LISTBOX 1 00039 00040 // Define an array of process pointers used by MyFrame 00041 class MyPipedProcess; 00042 WX_DEFINE_ARRAY_PTR(MyPipedProcess *, MyProcessesArray); 00043 00044 00045 class MyPipedProcess; 00046 00047 class MyProcessListener 00048 { 00049 public: 00050 virtual void OnProcessTerminated(MyPipedProcess *process, int pid, int status) = 0; 00051 virtual ~MyProcessListener() {} 00052 }; 00053 00054 00055 // Define a new exec dialog 00056 class MyExecPanel : public wxPanel, public MyProcessListener 00057 { 00058 public: 00059 // ctor(s) 00060 MyExecPanel(wxWindow * parent); 00061 00062 void KillProcess(); 00063 void PauseProcess(bool pause = true); 00064 void ContinueProcess(); 00065 long GetPid(); 00066 00067 void OnExecWithRedirect(wxCommandEvent& event); 00068 00069 int ExecWithRedirect(wxString command); 00070 00071 // polling output of async processes 00072 void OnTimer(wxTimerEvent& event); 00073 00074 // for MyPipedProcess 00075 void OnProcessTerminated(MyPipedProcess *process, int pid, int status); 00076 //wxListBox *GetLogListBox() const { return m_lbox; } 00077 00078 virtual ~MyExecPanel(); 00079 00080 private: 00081 00082 void AddToOutput(wxInputStream & s); 00083 00084 void DoAsyncExec(const wxString& cmd); 00085 00086 void AddAsyncProcess(MyPipedProcess *process); 00087 00088 void RemoveAsyncProcess(MyPipedProcess *process); 00089 00090 // the PID of the last process we launched asynchronously 00091 long m_pidLast; 00092 00093 // last command we executed 00094 wxString m_cmdLast; 00095 00096 // wxString m_output; 00097 00098 #ifdef HUGIN_EXEC_LISTBOX 00099 wxListBox *m_lbox; 00100 wxString m_currLine; 00101 #else 00102 wxTextCtrl *m_textctrl; 00103 long m_lastLineStart; 00104 #endif 00105 00106 MyProcessesArray m_running; 00107 00108 // the idle event wake up timer 00109 wxTimer m_timerIdleWakeUp; 00110 00111 // any class wishing to process wxWidgets events must use this macro 00112 DECLARE_EVENT_TABLE() 00113 }; 00114 00115 // ---------------------------------------------------------------------------- 00116 // wxProcess-derived classes 00117 // ---------------------------------------------------------------------------- 00118 00119 // This is the handler for process termination events 00120 class MyProcess : public wxProcess 00121 { 00122 public: 00123 MyProcess(MyProcessListener *parent, const wxString& cmd) 00124 : wxProcess(0), m_cmd(cmd) 00125 { 00126 m_parent = parent; 00127 } 00128 // instead of overriding this virtual function we might as well process the 00129 // event from it in the frame class - this might be more convenient in some 00130 // cases 00131 virtual void OnTerminate(int pid, int status); 00132 protected: 00133 MyProcessListener *m_parent; 00134 wxString m_cmd; 00135 }; 00136 00137 // A specialization of MyProcess for redirecting the output 00138 class MyPipedProcess : public MyProcess 00139 { 00140 public: 00141 MyPipedProcess(MyProcessListener *parent, const wxString& cmd) 00142 : MyProcess(parent, cmd) 00143 { 00144 Redirect(); 00145 } 00146 00147 virtual void OnTerminate(int pid, int status); 00148 }; 00149 00150 00151 // Define a new exec dialog, which uses MyExecPanel defined above 00152 class MyExecDialog : public wxDialog 00153 { 00154 public: 00155 // ctor(s) 00156 MyExecDialog(wxWindow * parent, const wxString& title, const wxPoint& pos, const wxSize& size); 00157 00158 void OnCancel(wxCommandEvent& event); 00159 00160 int ExecWithRedirect(wxString command); 00161 00162 void OnProcessTerminate(wxProcessEvent & event); 00163 00164 virtual ~MyExecDialog(); 00165 00166 private: 00167 00168 MyExecPanel * m_execPanel; 00169 bool m_cancelled; 00170 00171 // any class wishing to process wxWidgets events must use this macro 00172 DECLARE_EVENT_TABLE() 00173 }; 00174 00175 //---------- 00176 00177 00178 00179 class MyExternalCmdExecDialog : public wxDialog 00180 { 00181 public: 00182 MyExternalCmdExecDialog(wxWindow* parent, 00183 wxWindowID id, 00184 const wxString& title = _("Command Line Progress"), 00185 const wxPoint& pos = wxDefaultPosition, 00186 const wxSize& size = wxDefaultSize, 00187 #ifdef __WXMAC__ 00188 long style = wxRESIZE_BORDER|wxFRAME_FLOAT_ON_PARENT|wxMINIMIZE_BOX, 00189 #else 00190 long style = wxDEFAULT_DIALOG_STYLE, 00191 #endif 00192 const wxString& name = wxT("externalCmDialogBox")); 00193 00194 int ShowModal(const wxString &cmd); 00195 int Execute(const wxString & cmd); 00196 int GetExitCode(); 00197 void SetExitCode(int ret); 00198 void OnTimer(wxTimerEvent& WXUNUSED(event)); 00199 void OnIdle(wxIdleEvent& event); 00200 //wxListBox *GetLogListBox() const { return m_lbox; } 00201 wxTextCtrl *GetLogTextBox() const { return m_tbox; } 00202 virtual ~MyExternalCmdExecDialog(); 00203 00204 private: 00205 //wxListBox *m_lbox; 00206 wxTextCtrl *m_tbox; 00207 wxTimer m_timerIdleWakeUp; 00208 HuginPipedProcess *process; 00209 long processID; 00210 int m_exitCode; 00211 00212 DECLARE_EVENT_TABLE() 00213 }; 00214 00215 //---------- 00216 00217 class HuginPipedProcess : public wxProcess 00218 { 00219 public: 00220 HuginPipedProcess(MyExternalCmdExecDialog *parent, const wxString& cmd) 00221 : wxProcess(parent), m_cmd(cmd) 00222 { 00223 m_parent = parent; 00224 Redirect(); 00225 } 00226 00227 virtual void OnTerminate(int pid, int status); 00228 virtual bool HasInput(); 00229 00230 protected: 00231 MyExternalCmdExecDialog *m_parent; 00232 wxString m_cmd; 00233 }; 00234 00235 #endif
1.3.9.1