Fixed blame crash at XP system
[TortoiseGit.git] / src / TortoiseProc / ResolveDlg.cpp
blob4b514e146024f48c20b1a8f79b694522e14a5b14
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
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 #include "stdafx.h"
20 #include "TortoiseProc.h"
21 #include "messagebox.h"
22 #include "ResolveDlg.h"
23 #include "CommonResource.h"
25 #define REFRESHTIMER 100
27 IMPLEMENT_DYNAMIC(CResolveDlg, CResizableStandAloneDialog)
28 CResolveDlg::CResolveDlg(CWnd* pParent /*=NULL*/)
29 : CResizableStandAloneDialog(CResolveDlg::IDD, pParent)
30 , m_bThreadRunning(FALSE)
31 , m_bCancelled(false)
35 CResolveDlg::~CResolveDlg()
39 void CResolveDlg::DoDataExchange(CDataExchange* pDX)
41 CResizableStandAloneDialog::DoDataExchange(pDX);
42 DDX_Control(pDX, IDC_RESOLVELIST, m_resolveListCtrl);
43 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
47 BEGIN_MESSAGE_MAP(CResolveDlg, CResizableStandAloneDialog)
48 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
49 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
50 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
51 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ADDFILE, OnFileDropped)
52 ON_WM_TIMER()
53 END_MESSAGE_MAP()
55 BOOL CResolveDlg::OnInitDialog()
57 CResizableStandAloneDialog::OnInitDialog();
59 m_resolveListCtrl.Init(SVNSLC_COLEXT, _T("ResolveDlg"), SVNSLC_POPALL ^ (SVNSLC_POPIGNORE|SVNSLC_POPADD|SVNSLC_POPCOMMIT));
60 m_resolveListCtrl.SetConfirmButton((CButton*)GetDlgItem(IDOK));
61 m_resolveListCtrl.SetSelectButton(&m_SelectAll);
62 m_resolveListCtrl.SetCancelBool(&m_bCancelled);
63 m_resolveListCtrl.SetBackgroundImage(IDI_RESOLVE_BKG);
64 m_resolveListCtrl.EnableFileDrop();
66 AdjustControlSize(IDC_SELECTALL);
68 AddAnchor(IDC_RESOLVELIST, TOP_LEFT, BOTTOM_RIGHT);
69 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
70 AddAnchor(IDOK, BOTTOM_RIGHT);
71 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
72 AddAnchor(IDHELP, BOTTOM_RIGHT);
73 AddAnchor(IDC_STATIC_REMINDER, BOTTOM_RIGHT);
74 if (hWndExplorer)
75 CenterWindow(CWnd::FromHandle(hWndExplorer));
76 EnableSaveRestore(_T("ResolveDlg"));
78 // first start a thread to obtain the file list with the status without
79 // blocking the dialog
80 if(AfxBeginThread(ResolveThreadEntry, this) == NULL)
82 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
84 InterlockedExchange(&m_bThreadRunning, TRUE);
86 return TRUE;
89 void CResolveDlg::OnOK()
91 if (m_bThreadRunning)
92 return;
94 //save only the files the user has selected into the path list
95 m_resolveListCtrl.WriteCheckedNamesToPathList(m_pathList);
97 CResizableStandAloneDialog::OnOK();
100 void CResolveDlg::OnCancel()
102 m_bCancelled = true;
103 if (m_bThreadRunning)
104 return;
106 CResizableStandAloneDialog::OnCancel();
109 void CResolveDlg::OnBnClickedSelectall()
111 UINT state = (m_SelectAll.GetState() & 0x0003);
112 if (state == BST_INDETERMINATE)
114 // It is not at all useful to manually place the checkbox into the indeterminate state...
115 // We will force this on to the unchecked state
116 state = BST_UNCHECKED;
117 m_SelectAll.SetCheck(state);
119 theApp.DoWaitCursor(1);
120 m_resolveListCtrl.SelectAll(state == BST_CHECKED);
121 theApp.DoWaitCursor(-1);
124 UINT CResolveDlg::ResolveThreadEntry(LPVOID pVoid)
126 return ((CResolveDlg*)pVoid)->ResolveThread();
128 UINT CResolveDlg::ResolveThread()
130 // get the status of all selected file/folders recursively
131 // and show the ones which are in conflict
132 DialogEnableWindow(IDOK, false);
134 m_bCancelled = false;
136 if (!m_resolveListCtrl.GetStatus(&m_pathList))
138 m_resolveListCtrl.SetEmptyString(m_resolveListCtrl.GetLastErrorMessage());
140 m_resolveListCtrl.Show(SVNSLC_SHOWCONFLICTED|SVNSLC_SHOWINEXTERNALS, SVNSLC_SHOWCONFLICTED);
142 InterlockedExchange(&m_bThreadRunning, FALSE);
143 return 0;
146 void CResolveDlg::OnBnClickedHelp()
148 OnHelp();
151 BOOL CResolveDlg::PreTranslateMessage(MSG* pMsg)
153 if (pMsg->message == WM_KEYDOWN)
155 switch (pMsg->wParam)
157 case VK_RETURN:
159 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
161 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
163 PostMessage(WM_COMMAND, IDOK);
165 return TRUE;
168 break;
169 case VK_F5:
171 if (!m_bThreadRunning)
173 if(AfxBeginThread(ResolveThreadEntry, this) == NULL)
175 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
177 else
178 InterlockedExchange(&m_bThreadRunning, TRUE);
181 break;
185 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
188 LRESULT CResolveDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
190 if(AfxBeginThread(ResolveThreadEntry, this) == NULL)
192 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
194 return 0;
197 LRESULT CResolveDlg::OnFileDropped(WPARAM, LPARAM lParam)
199 BringWindowToTop();
200 SetForegroundWindow();
201 SetActiveWindow();
202 // if multiple files/folders are dropped
203 // this handler is called for every single item
204 // separately.
205 // To avoid creating multiple refresh threads and
206 // causing crashes, we only add the items to the
207 // list control and start a timer.
208 // When the timer expires, we start the refresh thread,
209 // but only if it isn't already running - otherwise we
210 // restart the timer.
211 CTGitPath path;
212 path.SetFromWin((LPCTSTR)lParam);
214 if (!m_resolveListCtrl.HasPath(path))
216 if (m_pathList.AreAllPathsFiles())
218 m_pathList.AddPath(path);
219 m_pathList.RemoveDuplicates();
221 else
223 // if the path list contains folders, we have to check whether
224 // our just (maybe) added path is a child of one of those. If it is
225 // a child of a folder already in the list, we must not add it. Otherwise
226 // that path could show up twice in the list.
227 bool bHasParentInList = false;
228 for (int i=0; i<m_pathList.GetCount(); ++i)
230 if (m_pathList[i].IsAncestorOf(path))
232 bHasParentInList = true;
233 break;
236 if (!bHasParentInList)
238 m_pathList.AddPath(path);
239 m_pathList.RemoveDuplicates();
244 // Always start the timer, since the status of an existing item might have changed
245 SetTimer(REFRESHTIMER, 200, NULL);
246 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
247 return 0;
250 void CResolveDlg::OnTimer(UINT_PTR nIDEvent)
252 switch (nIDEvent)
254 case REFRESHTIMER:
255 if (m_bThreadRunning)
257 SetTimer(REFRESHTIMER, 200, NULL);
258 ATLTRACE("Wait some more before refreshing\n");
260 else
262 KillTimer(REFRESHTIMER);
263 ATLTRACE("Refreshing after items dropped\n");
264 OnSVNStatusListCtrlNeedsRefresh(0, 0);
266 break;
268 __super::OnTimer(nIDEvent);