OSDN Git Service

Unified SyncDlg and progressdlg log handle
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / ProgressDlg.cpp
index af71e5b..f7ab402 100644 (file)
@@ -15,6 +15,7 @@ CProgressDlg::CProgressDlg(CWnd* pParent /*=NULL*/)
 {\r
        m_pThread = NULL;\r
        m_bAltAbortPress=false;\r
+       m_bBufferAll=false;\r
 }\r
 \r
 CProgressDlg::~CProgressDlg()\r
@@ -92,7 +93,7 @@ UINT CProgressDlg::ProgressThreadEntry(LPVOID pVoid)
 }\r
 \r
 //static function, Share with SyncDialog\r
-UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bShowCommand,CString *pfilename,bool *bAbort)\r
+UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bShowCommand,CString *pfilename,bool *bAbort,std::vector<TCHAR>*pdata)\r
 {\r
        UINT ret=0;\r
 \r
@@ -101,6 +102,9 @@ UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bSho
 \r
        pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_START,0);\r
 \r
+       if(pdata)\r
+               pdata->clear();\r
+                \r
        for(int i=0;i<cmdlist.size();i++)\r
        {\r
                if(cmdlist[i].IsEmpty())\r
@@ -111,7 +115,14 @@ UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bSho
                        CString str;\r
                        str+= cmdlist[i]+_T("\n\n");\r
                        for(int j=0;j<str.GetLength();j++)\r
-                               pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,str[j]);\r
+                       {\r
+                               if(pdata)\r
+                                       pdata->push_back(str[j]);\r
+                               else\r
+                                       pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,str[j]);\r
+                       }\r
+                       if(pdata)\r
+                               pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,0);\r
                }\r
 \r
                g_Git.RunAsync(cmdlist[i],&pi, &hRead,pfilename);\r
@@ -122,7 +133,15 @@ UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bSho
                while(ReadFile(hRead,buffer,1,&readnumber,NULL))\r
                {\r
                        buffer[readnumber]=0;\r
-                       pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,(TCHAR)buffer[0]);\r
+                       \r
+                       if(pdata)\r
+                       {\r
+                               pdata->push_back((TCHAR)buffer[0]);\r
+\r
+                               if(buffer[0] == '\r' || buffer[0] == '\n')\r
+                                       pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,0);\r
+                       }else\r
+                               pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,buffer[0]);\r
                }\r
        \r
                CloseHandle(pi.hThread);\r
@@ -164,7 +183,7 @@ UINT CProgressDlg::ProgressThread()
        else\r
                pfilename=&m_LogFile;   \r
 \r
-       m_GitStatus = RunCmdList(this,m_GitCmdList,m_bShowCommand,pfilename,&m_bAbort);;\r
+       m_GitStatus = RunCmdList(this,m_GitCmdList,m_bShowCommand,pfilename,&m_bAbort,&this->m_Databuf);;\r
        return 0;\r
 }\r
 \r
@@ -172,11 +191,21 @@ LRESULT CProgressDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)
 {\r
        if(wParam == MSG_PROGRESSDLG_START)\r
        {\r
+               m_BufStart = 0 ;\r
                m_Animate.Play(0,-1,-1);\r
                this->DialogEnableWindow(IDOK,FALSE);\r
        }\r
        if(wParam == MSG_PROGRESSDLG_END || wParam == MSG_PROGRESSDLG_FAILED)\r
        {\r
+               if(m_bBufferAll)\r
+               {\r
+                       m_Databuf.push_back(0);\r
+                       InsertCRLF();\r
+                       m_Log.SetWindowText(&m_Databuf[0]);\r
+               }\r
+               m_BufStart=0;\r
+               this->m_Databuf.clear();\r
+\r
                m_bDone = true;\r
                m_Animate.Stop();\r
                m_Progress.SetPos(100);\r
@@ -202,9 +231,18 @@ LRESULT CProgressDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)
                        DialogEnableWindow(IDCANCEL, FALSE);\r
        }\r
 \r
-       if(lParam != 0)\r
-               ParserCmdOutput((TCHAR)lParam);\r
-\r
+       if(!m_bBufferAll)\r
+       {\r
+               if(lParam == 0)\r
+               {\r
+                       for(int i=this->m_BufStart;i<this->m_Databuf.size();i++)\r
+                       {\r
+                               ParserCmdOutput(this->m_Databuf[m_BufStart]);\r
+                               m_BufStart++;\r
+                       }\r
+               }else\r
+                       ParserCmdOutput((TCHAR)lParam);\r
+       }\r
        return 0;\r
 }\r
 \r
@@ -226,6 +264,7 @@ int CProgressDlg::FindPercentage(CString &log)
        return _ttol(log.Mid(s2,s1-s2));\r
 }\r
 \r
+#if 0\r
 void CProgressDlg::ParserCmdOutput(TCHAR ch)\r
 {\r
        TRACE(_T("%c"),ch);\r
@@ -234,15 +273,28 @@ void CProgressDlg::ParserCmdOutput(TCHAR ch)
                TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));\r
                TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));\r
 \r
-               CString text;\r
-               m_Log.GetWindowTextW(text);\r
+               int lines=m_Log.GetLineCount();\r
+\r
                if(ch == _T('\r'))\r
+               {       \r
+                       int start=m_Log.LineIndex(lines-1);\r
+                       int length=m_Log.LineLength(lines-1);\r
+                       m_Log.SetSel( start,start+length);\r
+                       m_Log.ReplaceSel(m_LogText);\r
+\r
+               }else\r
                {\r
-                       RemoveLastLine(text);\r
+                       m_Log.SetSel(m_Log.GetWindowTextLength(),\r
+                                            m_Log.GetWindowTextLength());\r
+                       m_Log.ReplaceSel(CString(_T("\r\n"))+m_LogText);\r
                }\r
-               text+=_T("\r\n")+m_LogText;\r
-               m_Log.SetWindowTextW(text);\r
                \r
+               if( lines > 500 ) //limited log length\r
+               {\r
+                       int end=m_Log.LineIndex(1);\r
+                       m_Log.SetSel(0,end);\r
+                       m_Log.ReplaceSel(_T(""));\r
+               }\r
                m_Log.LineScroll(m_Log.GetLineCount());\r
 \r
                int s1=m_LogText.Find(_T(':'));\r
@@ -264,6 +316,64 @@ void CProgressDlg::ParserCmdOutput(TCHAR ch)
        }\r
 \r
 }\r
+#endif\r
+\r
+void CProgressDlg::ParserCmdOutput(TCHAR ch)\r
+{\r
+       ParserCmdOutput(this->m_Log,this->m_Progress,this->m_LogText,ch);\r
+}\r
+void CProgressDlg::ParserCmdOutput(CRichEditCtrl &log,CProgressCtrl &progressctrl,CString &oneline, TCHAR ch)\r
+{\r
+       //TRACE(_T("%c"),ch);\r
+       TRACE(_T("%c"),ch);\r
+       if( ch == _T('\r') || ch == _T('\n'))\r
+       {\r
+               TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));\r
+               TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));\r
+\r
+               int lines=log.GetLineCount();\r
+\r
+               if(ch == _T('\r'))\r
+               {       \r
+                       int start=log.LineIndex(lines-1);\r
+                       int length=log.LineLength(lines-1);\r
+                       log.SetSel( start,start+length);\r
+                       log.ReplaceSel(oneline);\r
+\r
+               }else\r
+               {\r
+                       log.SetSel(log.GetWindowTextLength(),\r
+                                            log.GetWindowTextLength());\r
+                       log.ReplaceSel(CString(_T("\r\n"))+oneline);\r
+               }\r
+               \r
+               if( lines > 500 ) //limited log length\r
+               {\r
+                       int end=log.LineIndex(1);\r
+                       log.SetSel(0,end);\r
+                       log.ReplaceSel(_T(""));\r
+               }\r
+               log.LineScroll(log.GetLineCount());\r
+\r
+               int s1=oneline.Find(_T(':'));\r
+               int s2=oneline.Find(_T('%'));\r
+               if(s1>0 && s2>0)\r
+               {\r
+                       //this->m_CurrentWork.SetWindowTextW(m_LogText.Left(s1));\r
+                       int pos=FindPercentage(oneline);\r
+                       TRACE(_T("Pos %d\r\n"),pos);\r
+                       if(pos>0)\r
+                               progressctrl.SetPos(pos);\r
+               }\r
+\r
+               oneline=_T("");\r
+\r
+       }else\r
+       {\r
+               oneline+=ch;\r
+       }\r
+\r
+}\r
 void CProgressDlg::RemoveLastLine(CString &str)\r
 {\r
        int start;\r
@@ -288,11 +398,47 @@ void CProgressDlg::OnBnClickedButton1()
 }\r
 void CProgressDlg::OnCancel()\r
 {\r
+       m_bAbort = true;\r
        if(m_bDone)\r
        {\r
                CResizableStandAloneDialog::OnCancel();\r
                return;\r
        }\r
+\r
+       if( g_Git.m_CurrentGitPi.hProcess )\r
+       {\r
+               if(::GenerateConsoleCtrlEvent(CTRL_C_EVENT,0))\r
+               {\r
+                       ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess ,10000);\r
+               }else\r
+               {\r
+                       int error=::GetLastError();\r
+               }\r
+\r
+               HANDLE  hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE,g_Git.m_CurrentGitPi.dwProcessId);\r
+               if( hProcessHandle )\r
+                       if(!::TerminateProcess(hProcessHandle,-1) )\r
+                       {\r
+                               int error =::GetLastError();\r
+                       }\r
+       }\r
        \r
-       m_bAbort = true;\r
+       ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess ,10000);\r
+       CResizableStandAloneDialog::OnCancel();\r
+\r
 }\r
+\r
+void CProgressDlg::InsertCRLF()\r
+{\r
+       for(int i=0;i<m_Databuf.size();i++)\r
+       {\r
+               if(m_Databuf[i]==_T('\n'))\r
+               {\r
+                       if(i==0 || m_Databuf[i-1]!= _T('\r'))\r
+                       {\r
+                               m_Databuf.insert(m_Databuf.begin()+i,_T('\r'));\r
+                               i++;\r
+                       }\r
+               }\r
+       }\r
+}
\ No newline at end of file