Use correct length for buffer
[TortoiseGit.git] / src / TortoiseProc / ResolveDlg.cpp
blob1538eb5100d3d9cf18a130fd8b8540968324fd63
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-2016 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "TortoiseProc.h"
22 #include "MessageBox.h"
23 #include "ResolveDlg.h"
24 #include "AppUtils.h"
25 #include "Git.h"
27 #define REFRESHTIMER 100
29 IMPLEMENT_DYNAMIC(CResolveDlg, CResizableStandAloneDialog)
30 CResolveDlg::CResolveDlg(CWnd* pParent /*=nullptr*/)
31 : CResizableStandAloneDialog(CResolveDlg::IDD, pParent)
32 , m_bThreadRunning(FALSE)
33 , m_bCancelled(false)
37 CResolveDlg::~CResolveDlg()
41 void CResolveDlg::DoDataExchange(CDataExchange* pDX)
43 CResizableStandAloneDialog::DoDataExchange(pDX);
44 DDX_Control(pDX, IDC_RESOLVELIST, m_resolveListCtrl);
45 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
49 BEGIN_MESSAGE_MAP(CResolveDlg, CResizableStandAloneDialog)
50 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
51 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
52 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
53 ON_WM_TIMER()
54 END_MESSAGE_MAP()
56 BOOL CResolveDlg::OnInitDialog()
58 CResizableStandAloneDialog::OnInitDialog();
59 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
61 m_resolveListCtrl.Init(GITSLC_COLEXT, _T("ResolveDlg"), GITSLC_POPALL ^ (GITSLC_POPIGNORE | GITSLC_POPADD | GITSLC_POPCOMMIT | GITSLC_POPEXPORT | GITSLC_POPRESTORE | GITSLC_POPSAVEAS | GITSLC_PREPAREDIFF));
62 m_resolveListCtrl.SetConfirmButton((CButton*)GetDlgItem(IDOK));
63 m_resolveListCtrl.SetSelectButton(&m_SelectAll);
64 m_resolveListCtrl.SetCancelBool(&m_bCancelled);
65 m_resolveListCtrl.SetBackgroundImage(IDI_RESOLVE_BKG);
66 m_resolveListCtrl.EnableFileDrop();
68 CString sWindowTitle;
69 GetWindowText(sWindowTitle);
70 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
72 AdjustControlSize(IDC_SELECTALL);
74 AddAnchor(IDC_RESOLVELIST, TOP_LEFT, BOTTOM_RIGHT);
75 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
76 AddAnchor(IDOK, BOTTOM_RIGHT);
77 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
78 AddAnchor(IDHELP, BOTTOM_RIGHT);
79 AddAnchor(IDC_STATIC_REMINDER, BOTTOM_RIGHT);
80 if (hWndExplorer)
81 CenterWindow(CWnd::FromHandle(hWndExplorer));
82 EnableSaveRestore(_T("ResolveDlg"));
84 // first start a thread to obtain the file list with the status without
85 // blocking the dialog
86 if (!AfxBeginThread(ResolveThreadEntry, this))
88 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
90 InterlockedExchange(&m_bThreadRunning, TRUE);
92 return TRUE;
95 void CResolveDlg::OnOK()
97 if (m_bThreadRunning)
98 return;
100 //save only the files the user has selected into the path list
101 m_resolveListCtrl.WriteCheckedNamesToPathList(m_pathList);
103 CResizableStandAloneDialog::OnOK();
106 void CResolveDlg::OnCancel()
108 m_bCancelled = true;
109 if (m_bThreadRunning)
110 return;
112 CResizableStandAloneDialog::OnCancel();
115 void CResolveDlg::OnBnClickedSelectall()
117 UINT state = (m_SelectAll.GetState() & 0x0003);
118 if (state == BST_INDETERMINATE)
120 // It is not at all useful to manually place the checkbox into the indeterminate state...
121 // We will force this on to the unchecked state
122 state = BST_UNCHECKED;
123 m_SelectAll.SetCheck(state);
125 theApp.DoWaitCursor(1);
126 m_resolveListCtrl.SelectAll(state == BST_CHECKED);
127 theApp.DoWaitCursor(-1);
130 UINT CResolveDlg::ResolveThreadEntry(LPVOID pVoid)
132 return ((CResolveDlg*)pVoid)->ResolveThread();
134 UINT CResolveDlg::ResolveThread()
136 // get the status of all selected file/folders recursively
137 // and show the ones which are in conflict
138 DialogEnableWindow(IDOK, false);
140 m_bCancelled = false;
142 m_resolveListCtrl.Clear();
143 if (!m_resolveListCtrl.GetStatus(&m_pathList))
144 m_resolveListCtrl.SetEmptyString(m_resolveListCtrl.GetLastErrorMessage());
145 m_resolveListCtrl.Show(GITSLC_SHOWCONFLICTED|GITSLC_SHOWINEXTERNALS, GITSLC_SHOWCONFLICTED);
147 InterlockedExchange(&m_bThreadRunning, FALSE);
148 return 0;
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() )
162 PostMessage(WM_COMMAND, IDOK);
163 return TRUE;
166 break;
167 case VK_F5:
169 if (!m_bThreadRunning)
171 if (!AfxBeginThread(ResolveThreadEntry, this))
172 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
173 else
174 InterlockedExchange(&m_bThreadRunning, TRUE);
177 break;
181 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
184 LRESULT CResolveDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
186 if (!AfxBeginThread(ResolveThreadEntry, this))
187 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
188 return 0;
191 LRESULT CResolveDlg::OnFileDropped(WPARAM, LPARAM lParam)
193 BringWindowToTop();
194 SetForegroundWindow();
195 SetActiveWindow();
196 // if multiple files/folders are dropped
197 // this handler is called for every single item
198 // separately.
199 // To avoid creating multiple refresh threads and
200 // causing crashes, we only add the items to the
201 // list control and start a timer.
202 // When the timer expires, we start the refresh thread,
203 // but only if it isn't already running - otherwise we
204 // restart the timer.
205 CTGitPath path;
206 path.SetFromWin((LPCTSTR)lParam);
208 if (!m_resolveListCtrl.HasPath(path))
210 if (m_pathList.AreAllPathsFiles())
212 m_pathList.AddPath(path);
213 m_pathList.RemoveDuplicates();
215 else
217 // if the path list contains folders, we have to check whether
218 // our just (maybe) added path is a child of one of those. If it is
219 // a child of a folder already in the list, we must not add it. Otherwise
220 // that path could show up twice in the list.
221 bool bHasParentInList = false;
222 for (int i=0; i<m_pathList.GetCount(); ++i)
224 if (m_pathList[i].IsAncestorOf(path))
226 bHasParentInList = true;
227 break;
230 if (!bHasParentInList)
232 m_pathList.AddPath(path);
233 m_pathList.RemoveDuplicates();
238 // Always start the timer, since the status of an existing item might have changed
239 SetTimer(REFRESHTIMER, 200, nullptr);
240 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Item %s dropped, timer started\n"), path.GetWinPath());
241 return 0;
244 void CResolveDlg::OnTimer(UINT_PTR nIDEvent)
246 switch (nIDEvent)
248 case REFRESHTIMER:
249 if (m_bThreadRunning)
251 SetTimer(REFRESHTIMER, 200, nullptr);
252 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
254 else
256 KillTimer(REFRESHTIMER);
257 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
258 OnSVNStatusListCtrlNeedsRefresh(0, 0);
260 break;
262 __super::OnTimer(nIDEvent);