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, bool isQuoted=false);
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; }
00079     bool SaveLog(const wxString &filename);
00081     void CopyLogToClipboard();
00082     
00083     virtual ~MyExecPanel();
00084 
00085 private:
00086 
00087     void AddToOutput(wxInputStream & s);
00088 
00089     void DoAsyncExec(const wxString& cmd);
00090 
00091     void AddAsyncProcess(MyPipedProcess *process);
00092 
00093     void RemoveAsyncProcess(MyPipedProcess *process);
00094 
00095     // the PID of the last process we launched asynchronously
00096     long m_pidLast;
00097 
00098     // last command we executed
00099     wxString m_cmdLast;
00100 
00101 //    wxString m_output;
00102 
00103 #ifdef HUGIN_EXEC_LISTBOX
00104     wxListBox *m_lbox;
00105     wxString   m_currLine;
00106 #else
00107     wxTextCtrl *m_textctrl;
00108     long m_lastLineStart;
00109 #endif
00110 
00111     MyProcessesArray m_running;
00112 
00113     // the idle event wake up timer
00114     wxTimer m_timerIdleWakeUp;
00115 
00116     // any class wishing to process wxWidgets events must use this macro
00117     DECLARE_EVENT_TABLE()
00118 };
00119 
00120 // ----------------------------------------------------------------------------
00121 // wxProcess-derived classes
00122 // ----------------------------------------------------------------------------
00123 
00124 // This is the handler for process termination events
00125 class MyProcess : public wxProcess
00126 {
00127 public:
00128     MyProcess(MyProcessListener *parent, const wxString& cmd)
00129         : wxProcess(0), m_cmd(cmd)
00130     {
00131         m_parent = parent;
00132     }
00133     // instead of overriding this virtual function we might as well process the
00134     // event from it in the frame class - this might be more convenient in some
00135     // cases
00136     virtual void OnTerminate(int pid, int status);
00137 protected:
00138     MyProcessListener *m_parent;
00139     wxString m_cmd;
00140 };
00141 
00142 // A specialization of MyProcess for redirecting the output
00143 class MyPipedProcess : public MyProcess
00144 {
00145 public:
00146     MyPipedProcess(MyProcessListener *parent, const wxString& cmd)
00147         : MyProcess(parent, cmd)
00148         {
00149             Redirect();
00150         }
00151 
00152     virtual void OnTerminate(int pid, int status);
00153 };
00154 
00155 
00156 // Define a new exec dialog, which uses MyExecPanel defined above
00157 class MyExecDialog : public wxDialog
00158 {
00159 public:
00160 // ctor(s)
00161     MyExecDialog(wxWindow * parent, const wxString& title, const wxPoint& pos, const wxSize& size);
00162 
00163     void OnCancel(wxCommandEvent& event);
00164 
00165     int ExecWithRedirect(wxString command);
00166     
00167     void OnProcessTerminate(wxProcessEvent & event);
00168     
00169     virtual ~MyExecDialog();
00170 
00171 private:
00172     
00173     MyExecPanel * m_execPanel;
00174     bool m_cancelled;
00175 
00176     // any class wishing to process wxWidgets events must use this macro
00177     DECLARE_EVENT_TABLE()
00178 };
00179 
00180 //----------
00181 
00182 
00183 
00184 class MyExternalCmdExecDialog : public wxDialog
00185 {
00186 public:
00187     MyExternalCmdExecDialog(wxWindow* parent, 
00188                             wxWindowID id, 
00189                             const wxString& title = _("Command Line Progress"), 
00190                             const wxPoint& pos = wxDefaultPosition, 
00191                             const wxSize& size = wxDefaultSize, 
00192 #ifdef __WXMAC__
00193                             long style = wxRESIZE_BORDER|wxFRAME_FLOAT_ON_PARENT|wxMINIMIZE_BOX,
00194 #else
00195                             long style = wxDEFAULT_DIALOG_STYLE,
00196 #endif
00197                             const wxString& name = wxT("externalCmDialogBox"));
00198     
00199     int ShowModal(const wxString &cmd);
00200         int Execute(const wxString & cmd);
00201         int GetExitCode();
00202     void SetExitCode(int ret);
00203     void OnTimer(wxTimerEvent& WXUNUSED(event));
00204     void OnIdle(wxIdleEvent& event);
00205     //wxListBox *GetLogListBox() const { return m_lbox; }
00206     wxTextCtrl *GetLogTextBox() const { return m_tbox; }
00207     virtual ~MyExternalCmdExecDialog();
00208     
00209 private:
00210     //wxListBox *m_lbox;
00211     wxTextCtrl *m_tbox;
00212     wxTimer m_timerIdleWakeUp;
00213     HuginPipedProcess *process;
00214     long processID;
00215     int m_exitCode;
00216 
00217     DECLARE_EVENT_TABLE()
00218 };
00219 
00220 //----------
00221 
00222 class HuginPipedProcess : public wxProcess
00223 {
00224 public:
00225     HuginPipedProcess(MyExternalCmdExecDialog *parent, const wxString& cmd)
00226     : wxProcess(parent), m_cmd(cmd)
00227     {
00228         m_parent = parent;
00229         Redirect();
00230     }
00231     
00232     virtual void OnTerminate(int pid, int status);
00233     virtual bool HasInput();
00234     
00235 protected:
00236     MyExternalCmdExecDialog *m_parent;
00237     wxString m_cmd;
00238 };
00239 
00240 #endif

Generated on 1 Nov 2014 for Hugintrunk by  doxygen 1.4.7