Extend static functions in CAppUtils with a window handle parameter
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blobe9b90a789f0acdc6893f0d9afc1c80ff83952d51
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2016-2017 - 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 "PathUtils.h"
26 #include "registry.h"
27 #include "AppUtils.h"
29 #define REFRESHTIMER 100
31 IMPLEMENT_DYNAMIC(CRevertDlg, CResizableStandAloneDialog)
32 CRevertDlg::CRevertDlg(CWnd* pParent /*=nullptr*/)
33 : CResizableStandAloneDialog(CRevertDlg::IDD, pParent)
34 , m_bSelectAll(TRUE)
35 , m_bThreadRunning(FALSE)
36 , m_bCancelled(false)
37 , m_bRecursive(FALSE)
41 CRevertDlg::~CRevertDlg()
45 void CRevertDlg::DoDataExchange(CDataExchange* pDX)
47 CResizableStandAloneDialog::DoDataExchange(pDX);
48 DDX_Control(pDX, IDC_REVERTLIST, m_RevertList);
49 DDX_Check(pDX, IDC_SELECTALL, m_bSelectAll);
50 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
54 BEGIN_MESSAGE_MAP(CRevertDlg, CResizableStandAloneDialog)
55 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
56 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
57 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
58 ON_WM_TIMER()
59 END_MESSAGE_MAP()
63 BOOL CRevertDlg::OnInitDialog()
65 CResizableStandAloneDialog::OnInitDialog();
66 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
68 m_RevertList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD| GITSLC_COLDEL, L"RevertDlg");
69 m_RevertList.SetConfirmButton((CButton*)GetDlgItem(IDOK));
70 m_RevertList.SetSelectButton(&m_SelectAll);
71 m_RevertList.SetCancelBool(&m_bCancelled);
72 m_RevertList.SetBackgroundImage(IDI_REVERT_BKG);
73 m_RevertList.EnableFileDrop();
75 CString sWindowTitle;
76 GetWindowText(sWindowTitle);
77 CAppUtils::SetWindowTitle(m_hWnd, g_Git.CombinePath(m_pathList.GetCommonRoot()), sWindowTitle);
79 AdjustControlSize(IDC_SELECTALL);
81 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
82 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
83 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
84 AddAnchor(IDOK, BOTTOM_RIGHT);
85 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
86 AddAnchor(IDHELP, BOTTOM_RIGHT);
87 if (hWndExplorer)
88 CenterWindow(CWnd::FromHandle(hWndExplorer));
89 EnableSaveRestore(L"RevertDlg");
91 // first start a thread to obtain the file list with the status without
92 // blocking the dialog
93 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
94 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
95 InterlockedExchange(&m_bThreadRunning, TRUE);
97 return TRUE;
100 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
102 return reinterpret_cast<CRevertDlg*>(pVoid)->RevertThread();
105 UINT CRevertDlg::RevertThread()
107 // get the status of all selected file/folders recursively
108 // and show the ones which can be reverted to the user
109 // in a list control.
110 DialogEnableWindow(IDOK, false);
111 m_bCancelled = false;
113 m_RevertList.StoreScrollPos();
114 m_RevertList.Clear();
116 g_Git.RefreshGitIndex();
118 if (!m_RevertList.GetStatus(&m_pathList))
120 m_RevertList.SetEmptyString(m_RevertList.GetLastErrorMessage());
122 m_RevertList.Show(GITSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWEXTERNALFROMDIFFERENTREPO,
123 // do not select all files, only the ones the user has selected directly
124 GITSLC_SHOWDIRECTFILES|GITSLC_SHOWADDED);
126 if (m_RevertList.HasUnversionedItems())
128 if (DWORD(CRegStdDWORD(L"Software\\TortoiseGit\\UnversionedAsModified", FALSE)))
130 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_SHOW);
132 else
133 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
135 else
136 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
138 InterlockedExchange(&m_bThreadRunning, FALSE);
139 RefreshCursor();
141 return 0;
144 void CRevertDlg::OnOK()
146 if (m_bThreadRunning)
147 return;
148 auto locker(m_RevertList.AcquireReadLock());
149 // save only the files the user has selected into the temporary file
150 m_bRecursive = TRUE;
151 for (int i=0; i<m_RevertList.GetItemCount(); ++i)
153 if (!m_RevertList.GetCheck(i))
154 m_bRecursive = FALSE;
155 else
157 m_selectedPathList.AddPath(*m_RevertList.GetListEntry(i));
158 #if 0
159 CGitStatusListCtrl::FileEntry * entry = m_RevertList.GetListEntry(i);
160 // add all selected entries to the list, except the ones with 'added'
161 // status: we later *delete* all the entries in the list before
162 // the actual revert is done (so the user has the reverted files
163 // still in the trash bin to recover from), but it's not good to
164 // delete added files because they're not restored by the revert.
165 if (entry->status != svn_wc_status_added)
166 m_selectedPathList.AddPath(entry->GetPath());
167 // if an entry inside an external is selected, we can't revert
168 // recursively anymore because the recursive revert stops at the
169 // external boundaries.
170 if (entry->IsInExternal())
171 m_bRecursive = FALSE;
172 #endif
175 if (!m_bRecursive)
176 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
177 m_selectedPathList.SortByPathname();
179 CResizableStandAloneDialog::OnOK();
182 void CRevertDlg::OnCancel()
184 m_bCancelled = true;
185 if (m_bThreadRunning)
186 return;
188 CResizableStandAloneDialog::OnCancel();
191 void CRevertDlg::OnBnClickedSelectall()
193 UINT state = (m_SelectAll.GetState() & 0x0003);
194 if (state == BST_INDETERMINATE)
196 // It is not at all useful to manually place the checkbox into the indeterminate state...
197 // We will force this on to the unchecked state
198 state = BST_UNCHECKED;
199 m_SelectAll.SetCheck(state);
201 theApp.DoWaitCursor(1);
202 m_RevertList.SelectAll(state == BST_CHECKED);
203 theApp.DoWaitCursor(-1);
206 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
208 if (pMsg->message == WM_KEYDOWN)
210 switch (pMsg->wParam)
212 case VK_RETURN:
214 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
216 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
217 PostMessage(WM_COMMAND, IDOK);
218 return TRUE;
221 break;
222 case VK_F5:
224 if (!m_bThreadRunning)
226 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
227 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
228 else
229 InterlockedExchange(&m_bThreadRunning, TRUE);
232 break;
236 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
239 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
241 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
242 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
243 return 0;
246 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
248 BringWindowToTop();
249 SetForegroundWindow();
250 SetActiveWindow();
251 // if multiple files/folders are dropped
252 // this handler is called for every single item
253 // separately.
254 // To avoid creating multiple refresh threads and
255 // causing crashes, we only add the items to the
256 // list control and start a timer.
257 // When the timer expires, we start the refresh thread,
258 // but only if it isn't already running - otherwise we
259 // restart the timer.
260 CTGitPath path;
261 path.SetFromWin((LPCTSTR)lParam);
263 // check whether the dropped file belongs to the very same repository
264 CString projectDir;
265 if (!path.HasAdminDir(&projectDir) || !CPathUtils::ArePathStringsEqual(g_Git.m_CurrentDir, projectDir))
266 return 0;
268 if (!m_RevertList.HasPath(path))
270 if (m_pathList.AreAllPathsFiles())
272 m_pathList.AddPath(path);
273 m_pathList.RemoveDuplicates();
275 else
277 // if the path list contains folders, we have to check whether
278 // our just (maybe) added path is a child of one of those. If it is
279 // a child of a folder already in the list, we must not add it. Otherwise
280 // that path could show up twice in the list.
281 bool bHasParentInList = false;
282 for (int i=0; i<m_pathList.GetCount(); ++i)
284 if (m_pathList[i].IsAncestorOf(path))
286 bHasParentInList = true;
287 break;
290 if (!bHasParentInList)
292 m_pathList.AddPath(path);
293 m_pathList.RemoveDuplicates();
297 m_RevertList.ResetChecked(path);
299 // Always start the timer, since the status of an existing item might have changed
300 SetTimer(REFRESHTIMER, 200, nullptr);
301 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Item %s dropped, timer started\n", path.GetWinPath());
302 return 0;
305 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
307 switch (nIDEvent)
309 case REFRESHTIMER:
310 if (m_bThreadRunning)
312 SetTimer(REFRESHTIMER, 200, nullptr);
313 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
315 else
317 KillTimer(REFRESHTIMER);
318 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
319 OnSVNStatusListCtrlNeedsRefresh(0, 0);
321 break;
323 __super::OnTimer(nIDEvent);