Limit GUI updates the the main thread
[TortoiseGit.git] / src / TortoiseProc / ProgressDlg.cpp
bloba3ef99b4a863aa3c73e4ed981bc915e4c284f683
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // ProgressDlg.cpp : implementation file
22 #include "stdafx.h"
23 #include "TortoiseProc.h"
24 #include "ProgressDlg.h"
25 #include "Git.h"
26 #include "atlconv.h"
27 #include "UnicodeUtils.h"
28 #include "IconMenu.h"
29 #include "LoglistCommonResource.h"
30 #include "Tlhelp32.h"
31 #include "AppUtils.h"
32 #include "SmartHandle.h"
33 #include "../TGitCache/CacheInterface.h"
34 #include "LoglistUtils.h"
36 // CProgressDlg dialog
38 IMPLEMENT_DYNAMIC(CProgressDlg, CResizableStandAloneDialog)
40 CProgressDlg::CProgressDlg(CWnd* pParent /*=NULL*/)
41 : CResizableStandAloneDialog(CProgressDlg::IDD, pParent), m_bShowCommand(true), m_bAutoCloseOnSuccess(false), m_bAbort(false), m_bDone(false), m_startTick(GetTickCount())
43 m_pThread = NULL;
44 m_PostCmdCallback = NULL;
45 m_caller = NULL;
46 m_bAltAbortPress=false;
47 m_bBufferAll=false;
48 m_GitStatus = -1;
51 CProgressDlg::~CProgressDlg()
53 if(m_pThread != NULL)
55 delete m_pThread;
59 void CProgressDlg::DoDataExchange(CDataExchange* pDX)
61 CDialog::DoDataExchange(pDX);
62 DDX_Control(pDX, IDC_CURRENT, this->m_CurrentWork);
63 DDX_Control(pDX, IDC_TITLE_ANIMATE, this->m_Animate);
64 DDX_Control(pDX, IDC_RUN_PROGRESS, this->m_Progress);
65 DDX_Control(pDX, IDC_LOG, this->m_Log);
66 DDX_Control(pDX, IDC_PROGRESS_BUTTON1, this->m_ctrlPostCmd);
69 BEGIN_MESSAGE_MAP(CProgressDlg, CResizableStandAloneDialog)
70 ON_WM_CLOSE()
71 ON_MESSAGE(MSG_PROGRESSDLG_UPDATE_UI, OnProgressUpdateUI)
72 ON_BN_CLICKED(IDOK, &CProgressDlg::OnBnClickedOk)
73 ON_BN_CLICKED(IDC_PROGRESS_BUTTON1,&CProgressDlg::OnBnClickedButton1)
74 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
75 END_MESSAGE_MAP()
77 BOOL CProgressDlg::OnInitDialog()
79 CResizableStandAloneDialog::OnInitDialog();
81 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
82 // do this, Explorer would be unable to send that message to our window if we
83 // were running elevated. It's OK to make the call all the time, since if we're
84 // not elevated, this is a no-op.
85 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
86 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
87 CAutoLibrary hUser = ::LoadLibrary(_T("user32.dll"));
88 if (hUser)
90 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
91 if (pfnChangeWindowMessageFilterEx)
93 pfnChangeWindowMessageFilterEx(m_hWnd, WM_TASKBARBTNCREATED, MSGFLT_ALLOW, &cfs);
96 m_pTaskbarList.Release();
97 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
99 AddAnchor(IDC_TITLE_ANIMATE, TOP_LEFT, TOP_RIGHT);
100 AddAnchor(IDC_RUN_PROGRESS, TOP_LEFT,TOP_RIGHT);
101 AddAnchor(IDC_LOG, TOP_LEFT,BOTTOM_RIGHT);
103 AddAnchor(IDOK,BOTTOM_RIGHT);
104 AddAnchor(IDCANCEL,BOTTOM_RIGHT);
105 AddAnchor(IDC_PROGRESS_BUTTON1,BOTTOM_LEFT);
106 AddAnchor(IDC_CURRENT,TOP_LEFT);
108 this->GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_HIDE);
109 m_Animate.Open(IDR_DOWNLOAD);
111 CFont m_logFont;
112 CAppUtils::CreateFontForLogs(m_logFont);
113 //GetDlgItem(IDC_CMD_LOG)->SetFont(&m_logFont);
114 m_Log.SetFont(&m_logFont);
116 CString InitialText;
117 if ( !m_PreText.IsEmpty() )
119 InitialText = m_PreText + _T("\r\n");
121 #if 0
122 if (m_bShowCommand && (!m_GitCmd.IsEmpty() ))
124 InitialText += m_GitCmd+_T("\r\n\r\n");
126 #endif
127 m_Log.SetWindowTextW(InitialText);
128 m_CurrentWork.SetWindowTextW(_T(""));
130 EnableSaveRestore(_T("ProgressDlg"));
132 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
133 if (m_pThread==NULL)
135 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
137 else
139 m_pThread->m_bAutoDelete = FALSE;
140 m_pThread->ResumeThread();
143 if(!m_Title.IsEmpty())
144 this->SetWindowText(m_Title);
146 CString sWindowTitle;
147 GetWindowText(sWindowTitle);
148 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
150 return TRUE;
153 UINT CProgressDlg::ProgressThreadEntry(LPVOID pVoid)
155 return ((CProgressDlg*)pVoid)->ProgressThread();
158 //static function, Share with SyncDialog
159 UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bShowCommand,CString *pfilename,bool *bAbort,CGitByteArray *pdata)
161 UINT ret=0;
163 PROCESS_INFORMATION pi;
164 HANDLE hRead = 0;
166 memset(&pi,0,sizeof(PROCESS_INFORMATION));
168 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
170 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_START,0);
172 if(pdata)
173 pdata->clear();
175 for(int i=0;i<cmdlist.size();i++)
177 if(cmdlist[i].IsEmpty())
178 continue;
180 if (bShowCommand)
182 CStringA str = CUnicodeUtils::GetMulti(cmdlist[i].Trim() + _T("\n\n"), CP_UTF8);
183 for(int j=0;j<str.GetLength();j++)
185 if(pdata)
187 pdata->m_critSec.Lock();
188 pdata->push_back(str[j]);
189 pdata->m_critSec.Unlock();
191 else
192 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,str[j]);
194 if(pdata)
195 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,0);
198 g_Git.RunAsync(cmdlist[i].Trim(),&pi, &hRead, NULL, pfilename);
200 DWORD readnumber;
201 char byte;
202 CString output;
203 while(ReadFile(hRead,&byte,1,&readnumber,NULL))
205 if(pdata)
207 if(byte == 0)
208 byte = '\n';
210 pdata->m_critSec.Lock();
211 pdata->push_back( byte);
212 pdata->m_critSec.Unlock();
214 if(byte == '\r' || byte == '\n')
215 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,0);
217 else
218 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,byte);
221 CloseHandle(pi.hThread);
223 WaitForSingleObject(pi.hProcess, INFINITE);
225 DWORD status=0;
226 if(!GetExitCodeProcess(pi.hProcess,&status) || *bAbort)
228 CloseHandle(pi.hProcess);
230 CloseHandle(hRead);
232 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI, MSG_PROGRESSDLG_FAILED, status);
233 return TGIT_GIT_ERROR_GET_EXIT_CODE;
235 ret |= status;
238 CloseHandle(pi.hProcess);
240 CloseHandle(hRead);
242 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI, MSG_PROGRESSDLG_END, ret);
244 return ret;
248 UINT CProgressDlg::ProgressThread()
251 m_GitCmdList.push_back(m_GitCmd);
253 CString *pfilename;
255 if(m_LogFile.IsEmpty())
256 pfilename=NULL;
257 else
258 pfilename=&m_LogFile;
260 m_startTick = GetTickCount();
261 m_GitStatus = RunCmdList(this,m_GitCmdList,m_bShowCommand,pfilename,&m_bAbort,&this->m_Databuf);;
262 return 0;
265 LRESULT CProgressDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)
267 if(wParam == MSG_PROGRESSDLG_START)
269 m_BufStart = 0 ;
270 m_Animate.Play(0, INT_MAX, INT_MAX);
271 this->DialogEnableWindow(IDOK,FALSE);
272 if (m_pTaskbarList)
274 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
275 m_pTaskbarList->SetProgressValue(m_hWnd, 0, 100);
278 if(wParam == MSG_PROGRESSDLG_END || wParam == MSG_PROGRESSDLG_FAILED)
280 DWORD tickSpent = GetTickCount() - m_startTick;
281 CString strEndTime = CLoglistUtils::FormatDateAndTime(CTime::GetCurrentTime(), DATE_SHORTDATE, true, false);
283 if(m_bBufferAll)
285 m_Databuf.m_critSec.Lock();
286 m_Databuf.push_back(0);
287 m_Databuf.m_critSec.Unlock();
288 InsertCRLF();
289 m_Databuf.m_critSec.Lock();
290 m_Log.SetWindowText(Convert2UnionCode((char*)&m_Databuf[0]));
291 m_Databuf.m_critSec.Unlock();
292 m_Log.LineScroll(m_Log.GetLineCount() - m_Log.GetFirstVisibleLine() - 4);
294 m_BufStart=0;
295 m_Databuf.m_critSec.Lock();
296 this->m_Databuf.clear();
297 m_Databuf.m_critSec.Unlock();
299 m_bDone = true;
300 m_Animate.Stop();
301 m_Progress.SetPos(100);
302 this->DialogEnableWindow(IDOK,TRUE);
304 m_GitStatus = (DWORD)lParam;
306 // detect crashes of perl when performing git svn actions
307 if (m_GitStatus == 0 && m_GitCmd.Find(_T(" svn ")) > 1)
309 CString log;
310 m_Log.GetWindowText(log);
311 if (log.GetLength() > 18 && log.Mid(log.GetLength() - 18) == _T("perl.exe.stackdump"))
312 m_GitStatus = (DWORD)-1;
315 if(this->m_GitStatus)
317 if (m_pTaskbarList)
319 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
320 m_pTaskbarList->SetProgressValue(m_hWnd, 100, 100);
322 CString log;
323 log.Format(IDS_PROC_PROGRESS_GITUNCLEANEXIT, m_GitStatus);
324 CString err;
325 err.Format(_T("\r\n\r\n%s (%d ms @ %s)\r\n"), log, tickSpent, strEndTime);
326 InsertColorText(this->m_Log, err, RGB(255,0,0));
328 else {
329 if (m_pTaskbarList)
330 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
331 CString temp;
332 temp.LoadString(IDS_SUCCESS);
333 CString log;
334 log.Format(_T("\r\n%s (%d ms @ %s)\r\n"), temp, tickSpent, strEndTime);
335 InsertColorText(this->m_Log, log, RGB(0,0,255));
336 this->DialogEnableWindow(IDCANCEL,FALSE);
339 if (wParam == MSG_PROGRESSDLG_END)
341 if (m_PostCmdCallback) // new handling method using callback
343 m_PostCmdCallback(this, m_caller, m_GitStatus);
345 if (m_PostCmdList.GetCount() > 0)
347 m_ctrlPostCmd.AddEntries(m_PostCmdList);
348 GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_SHOW);
351 else if (m_GitStatus == 0) // default old behaviour on success
353 if (m_PostCmdList.GetCount() > 0)
355 m_ctrlPostCmd.AddEntries(m_PostCmdList);
356 GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_SHOW);
359 else // simple method to show buttons on failed
361 if (m_PostFailCmdList.GetCount() > 0)
363 m_ctrlPostCmd.AddEntries(m_PostFailCmdList);
364 GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_SHOW);
369 if(wParam == MSG_PROGRESSDLG_END && m_GitStatus == 0)
371 if(m_bAutoCloseOnSuccess)
373 m_Log.GetWindowText(this->m_LogText);
374 EndDialog(IDOK);
379 if(!m_bBufferAll)
381 if(lParam == 0)
383 m_Databuf.m_critSec.Lock();
384 for(int i=this->m_BufStart;i<this->m_Databuf.size();i++)
386 char c = this->m_Databuf[m_BufStart];
387 m_BufStart++;
388 m_Databuf.m_critSec.Unlock();
389 ParserCmdOutput(c);
391 m_Databuf.m_critSec.Lock();
394 if(m_BufStart>1000)
396 m_Databuf.erase(m_Databuf.begin(), m_Databuf.begin()+m_BufStart);
397 m_BufStart =0;
399 m_Databuf.m_critSec.Unlock();
402 else
403 ParserCmdOutput((char)lParam);
405 return 0;
408 //static function, Share with SyncDialog
409 int CProgressDlg::FindPercentage(CString &log)
411 int s1=log.Find(_T('%'));
412 if(s1<0)
413 return -1;
415 int s2=s1-1;
416 for(int i=s1-1;i>=0;i--)
418 if(log[i]>=_T('0') && log[i]<=_T('9'))
419 s2=i;
420 else
421 break;
423 return _ttol(log.Mid(s2,s1-s2));
426 void CProgressDlg::ParserCmdOutput(char ch)
428 ParserCmdOutput(this->m_Log,this->m_Progress,this->m_hWnd,this->m_pTaskbarList,this->m_LogTextA,ch,&this->m_CurrentWork);
430 void CProgressDlg::ClearESC(CString &str)
432 // see http://ascii-table.com/ansi-escape-sequences.php and http://tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html
433 str.Replace(_T("\033[K"), _T("")); // erase until end of line; no need to care for this, because we always clear the whole line
435 // drop colors
436 while (true)
438 int escapePosition = str.Find(_T('\033'));
439 if (escapePosition >= 0 && str.GetLength() >= escapePosition + 3)
441 if (str.Mid(escapePosition, 2) == _T("\033["))
443 int colorEnd = str.Find(_T('m'), escapePosition + 2);
444 if (colorEnd > 0)
446 bool found = true;
447 for (int i = escapePosition + 2; i < colorEnd; i++)
449 if (str[i] != _T(';') && (str[i] < _T('0') && str[i] > _T('9')))
451 found = false;
452 break;
455 if (found)
457 if (escapePosition > 0)
458 str = str.Left(escapePosition) + str.Mid(colorEnd + 1);
459 else
460 str = str.Mid(colorEnd);
461 continue;
466 break;
469 void CProgressDlg::ParserCmdOutput(CRichEditCtrl &log,CProgressCtrl &progressctrl,HWND m_hWnd,CComPtr<ITaskbarList3> m_pTaskbarList,CStringA &oneline, char ch, CWnd *CurrentWork)
471 //TRACE(_T("%c"),ch);
472 if( ch == ('\r') || ch == ('\n'))
474 CString str;
476 // TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));
477 // TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));
479 int lines = log.GetLineCount();
480 g_Git.StringAppend(&str, (BYTE*)oneline.GetBuffer(), CP_UTF8);
481 str.Trim();
482 // TRACE(_T("%s"), str);
484 ClearESC(str);
486 if(ch == ('\r'))
488 int start=log.LineIndex(lines-1);
489 log.SetSel(start, log.GetTextLength());
490 log.ReplaceSel(str);
492 else
494 int length = log.GetWindowTextLength();
495 log.SetSel(length, length);
496 if (length > 0)
497 log.ReplaceSel(_T("\r\n") + str);
498 else
499 log.ReplaceSel(str);
502 if (lines > 500) //limited log length
504 int end=log.LineIndex(1);
505 log.SetSel(0,end);
506 log.ReplaceSel(_T(""));
508 log.LineScroll(log.GetLineCount() - log.GetFirstVisibleLine() - 4);
510 int s1=oneline.ReverseFind(_T(':'));
511 int s2=oneline.Find(_T('%'));
512 if (s1 > 0 && s2 > 0)
514 if(CurrentWork)
515 CurrentWork->SetWindowTextW(str.Left(s1));
517 int pos=FindPercentage(str);
518 TRACE(_T("Pos %d\r\n"),pos);
519 if(pos>0)
521 progressctrl.SetPos(pos);
522 if (m_pTaskbarList)
524 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
525 m_pTaskbarList->SetProgressValue(m_hWnd, pos, 100);
530 oneline="";
533 else
535 oneline+=ch;
538 void CProgressDlg::RemoveLastLine(CString &str)
540 int start;
541 start=str.ReverseFind(_T('\n'));
542 if(start>0)
543 str=str.Left(start);
544 return;
546 // CProgressDlg message handlers
548 void CProgressDlg::OnBnClickedOk()
550 m_Log.GetWindowText(this->m_LogText);
551 OnOK();
554 void CProgressDlg::OnBnClickedButton1()
556 this->EndDialog((int)(IDC_PROGRESS_BUTTON1 + this->m_ctrlPostCmd.GetCurrentEntry()));
559 void CProgressDlg::OnClose()
561 DialogEnableWindow(IDCANCEL, TRUE);
562 __super::OnClose();
565 void CProgressDlg::OnCancel()
567 m_bAbort = true;
568 if(m_bDone)
570 CResizableStandAloneDialog::OnCancel();
571 return;
574 if( g_Git.m_CurrentGitPi.hProcess )
576 if(::GenerateConsoleCtrlEvent(CTRL_C_EVENT,0))
578 ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess ,10000);
580 else
582 GetLastError();
585 KillProcessTree(g_Git.m_CurrentGitPi.dwProcessId);
588 ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess ,10000);
589 CResizableStandAloneDialog::OnCancel();
592 void CProgressDlg::KillProcessTree(DWORD dwProcessId, unsigned int depth)
594 // recursively kills a process tree
595 // This is not optimized, but works and isn't called very often ;)
597 if (!dwProcessId || depth > 20)
598 return;
600 PROCESSENTRY32 pe;
601 memset(&pe, 0, sizeof(PROCESSENTRY32));
602 pe.dwSize = sizeof(PROCESSENTRY32);
604 CAutoGeneralHandle hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
606 if (::Process32First(hSnap, &pe))
610 if (pe.th32ParentProcessID == dwProcessId)
611 KillProcessTree(pe.th32ProcessID, depth + 1);
612 } while (::Process32Next(hSnap, &pe));
614 HANDLE hProc = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
615 if (hProc)
616 ::TerminateProcess(hProc, 1);
620 void CProgressDlg::InsertCRLF()
622 m_Databuf.m_critSec.Lock();
623 for(int i=0;i<m_Databuf.size();i++)
625 if(m_Databuf[i]==('\n'))
627 if(i==0 || m_Databuf[i-1]!= ('\r'))
629 m_Databuf.insert(m_Databuf.begin()+i,('\r'));
630 i++;
634 m_Databuf.m_critSec.Unlock();
637 void CProgressDlg::InsertColorText(CRichEditCtrl &edit,CString text,COLORREF rgb)
639 CHARFORMAT old,cf;
640 edit.GetDefaultCharFormat(cf);
641 old=cf;
642 cf.dwMask|=CFM_COLOR;
643 cf.crTextColor=rgb;
644 cf.dwEffects|=CFE_BOLD;
645 cf.dwEffects &= ~CFE_AUTOCOLOR ;
646 edit.SetSel(edit.GetTextLength()-1,edit.GetTextLength());
647 edit.ReplaceSel(text);
648 edit.SetSel(edit.LineIndex(edit.GetLineCount()-2),edit.GetTextLength());
649 edit.SetSelectionCharFormat(cf);
650 edit.SetSel(edit.GetTextLength(),edit.GetTextLength());
651 edit.SetDefaultCharFormat(old);
652 edit.LineScroll(edit.GetLineCount() - edit.GetFirstVisibleLine() - 4);
655 CString CCommitProgressDlg::Convert2UnionCode(char *buff, int size)
657 CString str;
659 CString cmd, output;
660 int cp=CP_UTF8;
662 cmd=_T("git.exe config i18n.logOutputEncoding");
663 if (g_Git.Run(cmd, &output, NULL, CP_UTF8))
664 cp=CP_UTF8;
666 int start=0;
667 output=output.Tokenize(_T("\n"),start);
668 cp=CUnicodeUtils::GetCPCode(output);
670 start =0;
671 if(size == -1)
672 size = (int)strlen(buff);
674 for(int i=0;i<size;i++)
676 if(buff[i] == ']')
677 start = i;
678 if( start >0 && buff[i] =='\n' )
680 start =i;
681 break;
685 str.Empty();
686 g_Git.StringAppend(&str, (BYTE*)buff, cp, start);
687 g_Git.StringAppend(&str, (BYTE*)buff + start, CP_UTF8, size - start);
689 ClearESC(str);
691 return str;
694 LRESULT CProgressDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
696 m_pTaskbarList.Release();
697 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
698 return 0;
701 BOOL CProgressDlg::PreTranslateMessage(MSG* pMsg)
703 if (pMsg->message == WM_KEYDOWN)
705 if (pMsg->wParam == VK_ESCAPE)
707 // pressing the ESC key should close the dialog. But since we disabled the escape
708 // key (so the user doesn't get the idea that he could simply undo an e.g. update)
709 // this won't work.
710 // So if the user presses the ESC key, change it to VK_RETURN so the dialog gets
711 // the impression that the OK button was pressed.
712 if ((!GetDlgItem(IDCANCEL)->IsWindowEnabled())
713 &&(GetDlgItem(IDOK)->IsWindowEnabled())&&(GetDlgItem(IDOK)->IsWindowVisible()))
715 // since we convert ESC to RETURN, make sure the OK button has the focus.
716 GetDlgItem(IDOK)->SetFocus();
717 pMsg->wParam = VK_RETURN;
721 else if (pMsg->message == WM_CONTEXTMENU || pMsg->message == WM_RBUTTONDOWN)
723 CWnd * pWnd = (CWnd*) GetDlgItem(IDC_LOG);
724 if (pWnd == GetFocus())
726 CIconMenu popup;
727 if (popup.CreatePopupMenu())
729 popup.AppendMenuIcon(WM_COPY, IDS_SCIEDIT_COPY, IDI_COPYCLIP);
730 if (m_Log.GetSelText().IsEmpty())
731 popup.EnableMenuItem(WM_COPY, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
732 popup.AppendMenu(MF_SEPARATOR);
733 popup.AppendMenuIcon(EM_SETSEL, IDS_SCIEDIT_SELECTALL);
734 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, pMsg->pt.x, pMsg->pt.y, this);
735 switch (cmd)
737 case 0: // no command selected
738 break;
739 case EM_SETSEL:
740 case WM_COPY:
741 ::SendMessage(GetDlgItem(IDC_LOG)->GetSafeHwnd(), cmd, 0, -1);
742 break;
744 return TRUE;
748 return __super::PreTranslateMessage(pMsg);