Allow to move item past last item
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blob5132907294aff062146268b0fe39108e4dd42f4f
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 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 "RevertDlg.h"
24 #include "Git.h"
25 #include "registry.h"
26 #include "AppUtils.h"
28 #define REFRESHTIMER 100
30 IMPLEMENT_DYNAMIC(CRevertDlg, CResizableStandAloneDialog)
31 CRevertDlg::CRevertDlg(CWnd* pParent /*=nullptr*/)
32 : CResizableStandAloneDialog(CRevertDlg::IDD, pParent)
33 , m_bSelectAll(TRUE)
34 , m_bThreadRunning(FALSE)
35 , m_bCancelled(false)
36 , m_bRecursive(FALSE)
40 CRevertDlg::~CRevertDlg()
44 void CRevertDlg::DoDataExchange(CDataExchange* pDX)
46 CResizableStandAloneDialog::DoDataExchange(pDX);
47 DDX_Control(pDX, IDC_REVERTLIST, m_RevertList);
48 DDX_Check(pDX, IDC_SELECTALL, m_bSelectAll);
49 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
53 BEGIN_MESSAGE_MAP(CRevertDlg, CResizableStandAloneDialog)
54 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
55 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
56 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
57 ON_WM_TIMER()
58 END_MESSAGE_MAP()
62 BOOL CRevertDlg::OnInitDialog()
64 CResizableStandAloneDialog::OnInitDialog();
65 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
67 m_RevertList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD| GITSLC_COLDEL, L"RevertDlg");
68 m_RevertList.SetConfirmButton((CButton*)GetDlgItem(IDOK));
69 m_RevertList.SetSelectButton(&m_SelectAll);
70 m_RevertList.SetCancelBool(&m_bCancelled);
71 m_RevertList.SetBackgroundImage(IDI_REVERT_BKG);
72 m_RevertList.EnableFileDrop();
74 CString sWindowTitle;
75 GetWindowText(sWindowTitle);
76 CAppUtils::SetWindowTitle(m_hWnd, g_Git.CombinePath(m_pathList.GetCommonRoot()), sWindowTitle);
78 AdjustControlSize(IDC_SELECTALL);
80 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
81 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
82 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
83 AddAnchor(IDOK, BOTTOM_RIGHT);
84 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
85 AddAnchor(IDHELP, BOTTOM_RIGHT);
86 if (hWndExplorer)
87 CenterWindow(CWnd::FromHandle(hWndExplorer));
88 EnableSaveRestore(L"RevertDlg");
90 // first start a thread to obtain the file list with the status without
91 // blocking the dialog
92 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
93 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
94 InterlockedExchange(&m_bThreadRunning, TRUE);
96 return TRUE;
99 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
101 return reinterpret_cast<CRevertDlg*>(pVoid)->RevertThread();
104 UINT CRevertDlg::RevertThread()
106 // get the status of all selected file/folders recursively
107 // and show the ones which can be reverted to the user
108 // in a list control.
109 DialogEnableWindow(IDOK, false);
110 m_bCancelled = false;
112 m_RevertList.StoreScrollPos();
113 m_RevertList.Clear();
115 g_Git.RefreshGitIndex();
117 if (!m_RevertList.GetStatus(&m_pathList))
119 m_RevertList.SetEmptyString(m_RevertList.GetLastErrorMessage());
121 m_RevertList.Show(GITSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWEXTERNALFROMDIFFERENTREPO,
122 // do not select all files, only the ones the user has selected directly
123 GITSLC_SHOWDIRECTFILES|GITSLC_SHOWADDED);
125 if (m_RevertList.HasUnversionedItems())
127 if (DWORD(CRegStdDWORD(L"Software\\TortoiseGit\\UnversionedAsModified", FALSE)))
129 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_SHOW);
131 else
132 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
134 else
135 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
137 InterlockedExchange(&m_bThreadRunning, FALSE);
138 RefreshCursor();
140 return 0;
143 void CRevertDlg::OnOK()
145 if (m_bThreadRunning)
146 return;
147 // save only the files the user has selected into the temporary file
148 m_bRecursive = TRUE;
149 for (int i=0; i<m_RevertList.GetItemCount(); ++i)
151 if (!m_RevertList.GetCheck(i))
152 m_bRecursive = FALSE;
153 else
155 m_selectedPathList.AddPath(*m_RevertList.GetListEntry(i));
156 #if 0
157 CGitStatusListCtrl::FileEntry * entry = m_RevertList.GetListEntry(i);
158 // add all selected entries to the list, except the ones with 'added'
159 // status: we later *delete* all the entries in the list before
160 // the actual revert is done (so the user has the reverted files
161 // still in the trash bin to recover from), but it's not good to
162 // delete added files because they're not restored by the revert.
163 if (entry->status != svn_wc_status_added)
164 m_selectedPathList.AddPath(entry->GetPath());
165 // if an entry inside an external is selected, we can't revert
166 // recursively anymore because the recursive revert stops at the
167 // external boundaries.
168 if (entry->IsInExternal())
169 m_bRecursive = FALSE;
170 #endif
173 if (!m_bRecursive)
174 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
175 m_selectedPathList.SortByPathname();
177 CResizableStandAloneDialog::OnOK();
180 void CRevertDlg::OnCancel()
182 m_bCancelled = true;
183 if (m_bThreadRunning)
184 return;
186 CResizableStandAloneDialog::OnCancel();
189 void CRevertDlg::OnBnClickedSelectall()
191 UINT state = (m_SelectAll.GetState() & 0x0003);
192 if (state == BST_INDETERMINATE)
194 // It is not at all useful to manually place the checkbox into the indeterminate state...
195 // We will force this on to the unchecked state
196 state = BST_UNCHECKED;
197 m_SelectAll.SetCheck(state);
199 theApp.DoWaitCursor(1);
200 m_RevertList.SelectAll(state == BST_CHECKED);
201 theApp.DoWaitCursor(-1);
204 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
206 if (pMsg->message == WM_KEYDOWN)
208 switch (pMsg->wParam)
210 case VK_RETURN:
212 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
214 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
215 PostMessage(WM_COMMAND, IDOK);
216 return TRUE;
219 break;
220 case VK_F5:
222 if (!m_bThreadRunning)
224 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
225 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
226 else
227 InterlockedExchange(&m_bThreadRunning, TRUE);
230 break;
234 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
237 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
239 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
240 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
241 return 0;
244 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
246 BringWindowToTop();
247 SetForegroundWindow();
248 SetActiveWindow();
249 // if multiple files/folders are dropped
250 // this handler is called for every single item
251 // separately.
252 // To avoid creating multiple refresh threads and
253 // causing crashes, we only add the items to the
254 // list control and start a timer.
255 // When the timer expires, we start the refresh thread,
256 // but only if it isn't already running - otherwise we
257 // restart the timer.
258 CTGitPath path;
259 path.SetFromWin((LPCTSTR)lParam);
261 // check whether the dropped file belongs to the very same repository
262 CString projectDir;
263 if (!path.HasAdminDir(&projectDir) || !CTGitPath::ArePathStringsEqual(g_Git.m_CurrentDir, projectDir))
264 return 0;
266 if (!m_RevertList.HasPath(path))
268 if (m_pathList.AreAllPathsFiles())
270 m_pathList.AddPath(path);
271 m_pathList.RemoveDuplicates();
273 else
275 // if the path list contains folders, we have to check whether
276 // our just (maybe) added path is a child of one of those. If it is
277 // a child of a folder already in the list, we must not add it. Otherwise
278 // that path could show up twice in the list.
279 bool bHasParentInList = false;
280 for (int i=0; i<m_pathList.GetCount(); ++i)
282 if (m_pathList[i].IsAncestorOf(path))
284 bHasParentInList = true;
285 break;
288 if (!bHasParentInList)
290 m_pathList.AddPath(path);
291 m_pathList.RemoveDuplicates();
295 m_RevertList.ResetChecked(path);
297 // Always start the timer, since the status of an existing item might have changed
298 SetTimer(REFRESHTIMER, 200, nullptr);
299 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Item %s dropped, timer started\n", path.GetWinPath());
300 return 0;
303 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
305 switch (nIDEvent)
307 case REFRESHTIMER:
308 if (m_bThreadRunning)
310 SetTimer(REFRESHTIMER, 200, nullptr);
311 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
313 else
315 KillTimer(REFRESHTIMER);
316 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
317 OnSVNStatusListCtrlNeedsRefresh(0, 0);
319 break;
321 __super::OnTimer(nIDEvent);