Fixed issue #1536: Committing via log causes hidden progress dialog
[TortoiseGit.git] / src / TortoiseProc / ProgressDlg.cpp
blob5eb5d386c2e8feed17ed29293bafb9dc1709534c
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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 = AtlLoadSystemLibraryUsingFullPath(_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 // Make sure this dialog is shown in foreground (see issue #1536)
151 SetForegroundWindow();
153 return TRUE;
156 UINT CProgressDlg::ProgressThreadEntry(LPVOID pVoid)
158 return ((CProgressDlg*)pVoid)->ProgressThread();
161 //static function, Share with SyncDialog
162 UINT CProgressDlg::RunCmdList(CWnd *pWnd,std::vector<CString> &cmdlist,bool bShowCommand,CString *pfilename,bool *bAbort,CGitByteArray *pdata)
164 UINT ret=0;
166 PROCESS_INFORMATION pi;
167 HANDLE hRead = 0;
169 memset(&pi,0,sizeof(PROCESS_INFORMATION));
171 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
173 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_START,0);
175 if(pdata)
176 pdata->clear();
178 for(int i=0;i<cmdlist.size();i++)
180 if(cmdlist[i].IsEmpty())
181 continue;
183 if (bShowCommand)
185 CStringA str = CUnicodeUtils::GetMulti(cmdlist[i].Trim() + _T("\n\n"), CP_UTF8);
186 for(int j=0;j<str.GetLength();j++)
188 if(pdata)
190 pdata->m_critSec.Lock();
191 pdata->push_back(str[j]);
192 pdata->m_critSec.Unlock();
194 else
195 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,str[j]);
197 if(pdata)
198 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,0);
201 g_Git.RunAsync(cmdlist[i].Trim(),&pi, &hRead, NULL, pfilename);
203 DWORD readnumber;
204 char byte;
205 CString output;
206 while(ReadFile(hRead,&byte,1,&readnumber,NULL))
208 if(pdata)
210 if(byte == 0)
211 byte = '\n';
213 pdata->m_critSec.Lock();
214 pdata->push_back( byte);
215 pdata->m_critSec.Unlock();
217 if(byte == '\r' || byte == '\n')
218 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,0);
220 else
221 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI,MSG_PROGRESSDLG_RUN,byte);
224 CloseHandle(pi.hThread);
226 WaitForSingleObject(pi.hProcess, INFINITE);
228 DWORD status=0;
229 if(!GetExitCodeProcess(pi.hProcess,&status) || *bAbort)
231 CloseHandle(pi.hProcess);
233 CloseHandle(hRead);
235 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI, MSG_PROGRESSDLG_FAILED, status);
236 return TGIT_GIT_ERROR_GET_EXIT_CODE;
238 ret |= status;
241 CloseHandle(pi.hProcess);
243 CloseHandle(hRead);
245 pWnd->PostMessage(MSG_PROGRESSDLG_UPDATE_UI, MSG_PROGRESSDLG_END, ret);
247 return ret;
251 UINT CProgressDlg::ProgressThread()
254 m_GitCmdList.push_back(m_GitCmd);
256 CString *pfilename;
258 if(m_LogFile.IsEmpty())
259 pfilename=NULL;
260 else
261 pfilename=&m_LogFile;
263 m_startTick = GetTickCount();
264 m_GitStatus = RunCmdList(this,m_GitCmdList,m_bShowCommand,pfilename,&m_bAbort,&this->m_Databuf);;
265 return 0;
268 LRESULT CProgressDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)
270 if(wParam == MSG_PROGRESSDLG_START)
272 m_BufStart = 0 ;
273 m_Animate.Play(0, INT_MAX, INT_MAX);
274 this->DialogEnableWindow(IDOK,FALSE);
275 if (m_pTaskbarList)
277 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
278 m_pTaskbarList->SetProgressValue(m_hWnd, 0, 100);
281 if(wParam == MSG_PROGRESSDLG_END || wParam == MSG_PROGRESSDLG_FAILED)
283 DWORD tickSpent = GetTickCount() - m_startTick;
284 CString strEndTime = CLoglistUtils::FormatDateAndTime(CTime::GetCurrentTime(), DATE_SHORTDATE, true, false);
286 if(m_bBufferAll)
288 m_Databuf.m_critSec.Lock();
289 m_Databuf.push_back(0);
290 m_Databuf.m_critSec.Unlock();
291 InsertCRLF();
292 m_Databuf.m_critSec.Lock();
293 m_Log.SetWindowText(Convert2UnionCode((char*)&m_Databuf[0]));
294 m_Databuf.m_critSec.Unlock();
295 m_Log.LineScroll(m_Log.GetLineCount() - m_Log.GetFirstVisibleLine() - 4);
297 m_BufStart=0;
298 m_Databuf.m_critSec.Lock();
299 this->m_Databuf.clear();
300 m_Databuf.m_critSec.Unlock();
302 m_bDone = true;
303 m_Animate.Stop();
304 m_Progress.SetPos(100);
305 this->DialogEnableWindow(IDOK,TRUE);
307 m_GitStatus = (DWORD)lParam;
309 // detect crashes of perl when performing git svn actions
310 if (m_GitStatus == 0 && m_GitCmd.Find(_T(" svn ")) > 1)
312 CString log;
313 m_Log.GetWindowText(log);
314 if (log.GetLength() > 18 && log.Mid(log.GetLength() - 18) == _T("perl.exe.stackdump"))
315 m_GitStatus = (DWORD)-1;
318 if(this->m_GitStatus)
320 if (m_pTaskbarList)
322 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
323 m_pTaskbarList->SetProgressValue(m_hWnd, 100, 100);
325 CString log;
326 log.Format(IDS_PROC_PROGRESS_GITUNCLEANEXIT, m_GitStatus);
327 CString err;
328 err.Format(_T("\r\n\r\n%s (%d ms @ %s)\r\n"), log, tickSpent, strEndTime);
329 InsertColorText(this->m_Log, err, RGB(255,0,0));
331 else {
332 if (m_pTaskbarList)
333 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
334 CString temp;
335 temp.LoadString(IDS_SUCCESS);
336 CString log;
337 log.Format(_T("\r\n%s (%d ms @ %s)\r\n"), temp, tickSpent, strEndTime);
338 InsertColorText(this->m_Log, log, RGB(0,0,255));
339 this->DialogEnableWindow(IDCANCEL,FALSE);
342 if (wParam == MSG_PROGRESSDLG_END)
344 if (m_PostCmdCallback) // new handling method using callback
346 m_PostCmdCallback(this, m_caller, m_GitStatus);
348 if (m_PostCmdList.GetCount() > 0)
350 m_ctrlPostCmd.AddEntries(m_PostCmdList);
351 GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_SHOW);
354 else if (m_GitStatus == 0) // default old behaviour on success
356 if (m_PostCmdList.GetCount() > 0)
358 m_ctrlPostCmd.AddEntries(m_PostCmdList);
359 GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_SHOW);
362 else // simple method to show buttons on failed
364 if (m_PostFailCmdList.GetCount() > 0)
366 m_ctrlPostCmd.AddEntries(m_PostFailCmdList);
367 GetDlgItem(IDC_PROGRESS_BUTTON1)->ShowWindow(SW_SHOW);
372 if(wParam == MSG_PROGRESSDLG_END && m_GitStatus == 0)
374 if(m_bAutoCloseOnSuccess)
376 m_Log.GetWindowText(this->m_LogText);
377 EndDialog(IDOK);
382 if(!m_bBufferAll)
384 if(lParam == 0)
386 m_Databuf.m_critSec.Lock();
387 for(int i=this->m_BufStart;i<this->m_Databuf.size();i++)
389 char c = this->m_Databuf[m_BufStart];
390 m_BufStart++;
391 m_Databuf.m_critSec.Unlock();
392 ParserCmdOutput(c);
394 m_Databuf.m_critSec.Lock();
397 if(m_BufStart>1000)
399 m_Databuf.erase(m_Databuf.begin(), m_Databuf.begin()+m_BufStart);
400 m_BufStart =0;
402 m_Databuf.m_critSec.Unlock();
405 else
406 ParserCmdOutput((char)lParam);
408 return 0;
411 //static function, Share with SyncDialog
412 int CProgressDlg::FindPercentage(CString &log)
414 int s1=log.Find(_T('%'));
415 if(s1<0)
416 return -1;
418 int s2=s1-1;
419 for(int i=s1-1;i>=0;i--)
421 if(log[i]>=_T('0') && log[i]<=_T('9'))
422 s2=i;
423 else
424 break;
426 return _ttol(log.Mid(s2,s1-s2));
429 void CProgressDlg::ParserCmdOutput(char ch)
431 ParserCmdOutput(this->m_Log,this->m_Progress,this->m_hWnd,this->m_pTaskbarList,this->m_LogTextA,ch,&this->m_CurrentWork);
433 void CProgressDlg::ClearESC(CString &str)
435 // see http://ascii-table.com/ansi-escape-sequences.php and http://tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html
436 str.Replace(_T("\033[K"), _T("")); // erase until end of line; no need to care for this, because we always clear the whole line
438 // drop colors
439 while (true)
441 int escapePosition = str.Find(_T('\033'));
442 if (escapePosition >= 0 && str.GetLength() >= escapePosition + 3)
444 if (str.Mid(escapePosition, 2) == _T("\033["))
446 int colorEnd = str.Find(_T('m'), escapePosition + 2);
447 if (colorEnd > 0)
449 bool found = true;
450 for (int i = escapePosition + 2; i < colorEnd; i++)
452 if (str[i] != _T(';') && (str[i] < _T('0') && str[i] > _T('9')))
454 found = false;
455 break;
458 if (found)
460 if (escapePosition > 0)
461 str = str.Left(escapePosition) + str.Mid(colorEnd + 1);
462 else
463 str = str.Mid(colorEnd);
464 continue;
469 break;
472 void CProgressDlg::ParserCmdOutput(CRichEditCtrl &log,CProgressCtrl &progressctrl,HWND m_hWnd,CComPtr<ITaskbarList3> m_pTaskbarList,CStringA &oneline, char ch, CWnd *CurrentWork)
474 //TRACE(_T("%c"),ch);
475 if( ch == ('\r') || ch == ('\n'))
477 CString str;
479 // TRACE(_T("End Char %s \r\n"),ch==_T('\r')?_T("lf"):_T(""));
480 // TRACE(_T("End Char %s \r\n"),ch==_T('\n')?_T("cr"):_T(""));
482 int lines = log.GetLineCount();
483 g_Git.StringAppend(&str, (BYTE*)oneline.GetBuffer(), CP_UTF8);
484 str.Trim();
485 // TRACE(_T("%s"), str);
487 ClearESC(str);
489 if(ch == ('\r'))
491 int start=log.LineIndex(lines-1);
492 log.SetSel(start, log.GetTextLength());
493 log.ReplaceSel(str);
495 else
497 int length = log.GetWindowTextLength();
498 log.SetSel(length, length);
499 if (length > 0)
500 log.ReplaceSel(_T("\r\n") + str);
501 else
502 log.ReplaceSel(str);
505 if (lines > 500) //limited log length
507 int end=log.LineIndex(1);
508 log.SetSel(0,end);
509 log.ReplaceSel(_T(""));
511 log.LineScroll(log.GetLineCount() - log.GetFirstVisibleLine() - 4);
513 int s1=oneline.ReverseFind(_T(':'));
514 int s2=oneline.Find(_T('%'));
515 if (s1 > 0 && s2 > 0)
517 if(CurrentWork)
518 CurrentWork->SetWindowTextW(str.Left(s1));
520 int pos=FindPercentage(str);
521 TRACE(_T("Pos %d\r\n"),pos);
522 if(pos>0)
524 progressctrl.SetPos(pos);
525 if (m_pTaskbarList)
527 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
528 m_pTaskbarList->SetProgressValue(m_hWnd, pos, 100);
533 oneline="";
536 else
538 oneline+=ch;
541 void CProgressDlg::RemoveLastLine(CString &str)
543 int start;
544 start=str.ReverseFind(_T('\n'));
545 if(start>0)
546 str=str.Left(start);
547 return;
549 // CProgressDlg message handlers
551 void CProgressDlg::OnBnClickedOk()
553 m_Log.GetWindowText(this->m_LogText);
554 OnOK();
557 void CProgressDlg::OnBnClickedButton1()
559 this->EndDialog((int)(IDC_PROGRESS_BUTTON1 + this->m_ctrlPostCmd.GetCurrentEntry()));
562 void CProgressDlg::OnClose()
564 DialogEnableWindow(IDCANCEL, TRUE);
565 __super::OnClose();
568 void CProgressDlg::OnCancel()
570 m_bAbort = true;
571 if(m_bDone)
573 CResizableStandAloneDialog::OnCancel();
574 return;
577 if( g_Git.m_CurrentGitPi.hProcess )
579 if(::GenerateConsoleCtrlEvent(CTRL_C_EVENT,0))
581 ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess ,10000);
583 else
585 GetLastError();
588 KillProcessTree(g_Git.m_CurrentGitPi.dwProcessId);
591 ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess ,10000);
592 CResizableStandAloneDialog::OnCancel();
595 void CProgressDlg::KillProcessTree(DWORD dwProcessId, unsigned int depth)
597 // recursively kills a process tree
598 // This is not optimized, but works and isn't called very often ;)
600 if (!dwProcessId || depth > 20)
601 return;
603 PROCESSENTRY32 pe;
604 memset(&pe, 0, sizeof(PROCESSENTRY32));
605 pe.dwSize = sizeof(PROCESSENTRY32);
607 CAutoGeneralHandle hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
609 if (::Process32First(hSnap, &pe))
613 if (pe.th32ParentProcessID == dwProcessId)
614 KillProcessTree(pe.th32ProcessID, depth + 1);
615 } while (::Process32Next(hSnap, &pe));
617 HANDLE hProc = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
618 if (hProc)
619 ::TerminateProcess(hProc, 1);
623 void CProgressDlg::InsertCRLF()
625 m_Databuf.m_critSec.Lock();
626 for(int i=0;i<m_Databuf.size();i++)
628 if(m_Databuf[i]==('\n'))
630 if(i==0 || m_Databuf[i-1]!= ('\r'))
632 m_Databuf.insert(m_Databuf.begin()+i,('\r'));
633 i++;
637 m_Databuf.m_critSec.Unlock();
640 void CProgressDlg::InsertColorText(CRichEditCtrl &edit,CString text,COLORREF rgb)
642 CHARFORMAT old,cf;
643 edit.GetDefaultCharFormat(cf);
644 old=cf;
645 cf.dwMask|=CFM_COLOR;
646 cf.crTextColor=rgb;
647 cf.dwEffects|=CFE_BOLD;
648 cf.dwEffects &= ~CFE_AUTOCOLOR ;
649 edit.SetSel(edit.GetTextLength()-1,edit.GetTextLength());
650 edit.ReplaceSel(text);
651 edit.SetSel(edit.LineIndex(edit.GetLineCount()-2),edit.GetTextLength());
652 edit.SetSelectionCharFormat(cf);
653 edit.SetSel(edit.GetTextLength(),edit.GetTextLength());
654 edit.SetDefaultCharFormat(old);
655 edit.LineScroll(edit.GetLineCount() - edit.GetFirstVisibleLine() - 4);
658 CString CCommitProgressDlg::Convert2UnionCode(char *buff, int size)
660 CString str;
662 CString cmd, output;
663 int cp=CP_UTF8;
665 cmd=_T("git.exe config i18n.logOutputEncoding");
666 if (g_Git.Run(cmd, &output, NULL, CP_UTF8))
667 cp=CP_UTF8;
669 int start=0;
670 output=output.Tokenize(_T("\n"),start);
671 cp=CUnicodeUtils::GetCPCode(output);
673 start =0;
674 if(size == -1)
675 size = (int)strlen(buff);
677 for(int i=0;i<size;i++)
679 if(buff[i] == ']')
680 start = i;
681 if( start >0 && buff[i] =='\n' )
683 start =i;
684 break;
688 str.Empty();
689 g_Git.StringAppend(&str, (BYTE*)buff, cp, start);
690 g_Git.StringAppend(&str, (BYTE*)buff + start, CP_UTF8, size - start);
692 ClearESC(str);
694 return str;
697 LRESULT CProgressDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
699 m_pTaskbarList.Release();
700 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
701 return 0;
704 BOOL CProgressDlg::PreTranslateMessage(MSG* pMsg)
706 if (pMsg->message == WM_KEYDOWN)
708 if (pMsg->wParam == VK_ESCAPE)
710 // pressing the ESC key should close the dialog. But since we disabled the escape
711 // key (so the user doesn't get the idea that he could simply undo an e.g. update)
712 // this won't work.
713 // So if the user presses the ESC key, change it to VK_RETURN so the dialog gets
714 // the impression that the OK button was pressed.
715 if ((!GetDlgItem(IDCANCEL)->IsWindowEnabled())
716 &&(GetDlgItem(IDOK)->IsWindowEnabled())&&(GetDlgItem(IDOK)->IsWindowVisible()))
718 // since we convert ESC to RETURN, make sure the OK button has the focus.
719 GetDlgItem(IDOK)->SetFocus();
720 pMsg->wParam = VK_RETURN;
724 else if (pMsg->message == WM_CONTEXTMENU || pMsg->message == WM_RBUTTONDOWN)
726 CWnd * pWnd = (CWnd*) GetDlgItem(IDC_LOG);
727 if (pWnd == GetFocus())
729 CIconMenu popup;
730 if (popup.CreatePopupMenu())
732 popup.AppendMenuIcon(WM_COPY, IDS_SCIEDIT_COPY, IDI_COPYCLIP);
733 if (m_Log.GetSelText().IsEmpty())
734 popup.EnableMenuItem(WM_COPY, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
735 popup.AppendMenu(MF_SEPARATOR);
736 popup.AppendMenuIcon(EM_SETSEL, IDS_SCIEDIT_SELECTALL);
737 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, pMsg->pt.x, pMsg->pt.y, this);
738 switch (cmd)
740 case 0: // no command selected
741 break;
742 case EM_SETSEL:
743 case WM_COPY:
744 ::SendMessage(GetDlgItem(IDC_LOG)->GetSafeHwnd(), cmd, 0, -1);
745 break;
747 return TRUE;
751 return __super::PreTranslateMessage(pMsg);