BrowseRefs: Context menu enhancements
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blobb37d25481b28f8b30625070557197df5792fefcd
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 "Revertdlg.h"
23 #include "Git.h"
24 #include "Registry.h"
25 #include ".\revertdlg.h"
26 #include "CommonResource.h"
28 #define REFRESHTIMER 100
30 IMPLEMENT_DYNAMIC(CRevertDlg, CResizableStandAloneDialog)
31 CRevertDlg::CRevertDlg(CWnd* pParent /*=NULL*/)
32 : CResizableStandAloneDialog(CRevertDlg::IDD, pParent)
33 , m_bSelectAll(TRUE)
34 , m_bThreadRunning(FALSE)
35 , m_bCancelled(false)
39 CRevertDlg::~CRevertDlg()
43 void CRevertDlg::DoDataExchange(CDataExchange* pDX)
45 CResizableStandAloneDialog::DoDataExchange(pDX);
46 DDX_Control(pDX, IDC_REVERTLIST, m_RevertList);
47 DDX_Check(pDX, IDC_SELECTALL, m_bSelectAll);
48 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
52 BEGIN_MESSAGE_MAP(CRevertDlg, CResizableStandAloneDialog)
53 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
54 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
55 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
56 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ADDFILE, OnFileDropped)
57 ON_WM_TIMER()
58 END_MESSAGE_MAP()
62 BOOL CRevertDlg::OnInitDialog()
64 CResizableStandAloneDialog::OnInitDialog();
66 m_RevertList.Init(SVNSLC_COLTEXTSTATUS | SVNSLC_COLPROPSTATUS, _T("RevertDlg"));
67 m_RevertList.SetConfirmButton((CButton*)GetDlgItem(IDOK));
68 m_RevertList.SetSelectButton(&m_SelectAll);
69 m_RevertList.SetCancelBool(&m_bCancelled);
70 m_RevertList.SetBackgroundImage(IDI_REVERT_BKG);
71 m_RevertList.EnableFileDrop();
73 GetWindowText(m_sWindowTitle);
75 AdjustControlSize(IDC_SELECTALL);
77 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
78 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
79 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
80 AddAnchor(IDOK, BOTTOM_RIGHT);
81 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
82 AddAnchor(IDHELP, BOTTOM_RIGHT);
83 if (hWndExplorer)
84 CenterWindow(CWnd::FromHandle(hWndExplorer));
85 EnableSaveRestore(_T("RevertDlg"));
87 // first start a thread to obtain the file list with the status without
88 // blocking the dialog
89 if (AfxBeginThread(RevertThreadEntry, this)==0)
91 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
93 InterlockedExchange(&m_bThreadRunning, TRUE);
95 return TRUE;
98 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
100 return ((CRevertDlg*)pVoid)->RevertThread();
103 UINT CRevertDlg::RevertThread()
105 // get the status of all selected file/folders recursively
106 // and show the ones which can be reverted to the user
107 // in a list control.
108 DialogEnableWindow(IDOK, false);
109 m_bCancelled = false;
111 if (!m_RevertList.GetStatus(&m_pathList))
113 m_RevertList.SetEmptyString(m_RevertList.GetLastErrorMessage());
115 m_RevertList.Show(SVNSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | SVNSLC_SHOWDIRECTFILES | SVNSLC_SHOWEXTERNALFROMDIFFERENTREPO,
116 // do not select all files, only the ones the user has selected directly
117 SVNSLC_SHOWDIRECTFILES|SVNSLC_SHOWADDED);
119 CTGitPath commonDir = m_RevertList.GetCommonDirectory(false);
120 SetWindowText(m_sWindowTitle + _T(" - ") + commonDir.GetWinPathString());
122 if (m_RevertList.HasUnversionedItems())
124 if (DWORD(CRegStdWORD(_T("Software\\TortoiseGit\\UnversionedAsModified"), FALSE)))
126 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_SHOW);
128 else
129 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
131 else
132 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
134 InterlockedExchange(&m_bThreadRunning, FALSE);
135 RefreshCursor();
137 return 0;
140 void CRevertDlg::OnOK()
142 if (m_bThreadRunning)
143 return;
144 // save only the files the user has selected into the temporary file
145 m_bRecursive = TRUE;
146 for (int i=0; i<m_RevertList.GetItemCount(); ++i)
148 if (!m_RevertList.GetCheck(i))
150 m_bRecursive = FALSE;
152 else
154 m_selectedPathList.AddPath(*(CTGitPath*)m_RevertList.GetItemData(i));
155 #if 0
156 CGitStatusListCtrl::FileEntry * entry = m_RevertList.GetListEntry(i);
157 // add all selected entries to the list, except the ones with 'added'
158 // status: we later *delete* all the entries in the list before
159 // the actual revert is done (so the user has the reverted files
160 // still in the trash bin to recover from), but it's not good to
161 // delete added files because they're not restored by the revert.
162 if (entry->status != svn_wc_status_added)
163 m_selectedPathList.AddPath(entry->GetPath());
164 // if an entry inside an external is selected, we can't revert
165 // recursively anymore because the recursive revert stops at the
166 // external boundaries.
167 if (entry->IsInExternal())
168 m_bRecursive = FALSE;
169 #endif
172 if (!m_bRecursive)
174 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
176 m_selectedPathList.SortByPathname();
178 CResizableStandAloneDialog::OnOK();
181 void CRevertDlg::OnCancel()
183 m_bCancelled = true;
184 if (m_bThreadRunning)
185 return;
187 CResizableStandAloneDialog::OnCancel();
190 void CRevertDlg::OnBnClickedHelp()
192 OnHelp();
195 void CRevertDlg::OnBnClickedSelectall()
197 UINT state = (m_SelectAll.GetState() & 0x0003);
198 if (state == BST_INDETERMINATE)
200 // It is not at all useful to manually place the checkbox into the indeterminate state...
201 // We will force this on to the unchecked state
202 state = BST_UNCHECKED;
203 m_SelectAll.SetCheck(state);
205 theApp.DoWaitCursor(1);
206 m_RevertList.SelectAll(state == BST_CHECKED);
207 theApp.DoWaitCursor(-1);
210 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
212 if (pMsg->message == WM_KEYDOWN)
214 switch (pMsg->wParam)
216 case VK_RETURN:
218 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
220 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
222 PostMessage(WM_COMMAND, IDOK);
224 return TRUE;
227 break;
228 case VK_F5:
230 if (!m_bThreadRunning)
232 if (AfxBeginThread(RevertThreadEntry, this)==0)
234 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
236 else
237 InterlockedExchange(&m_bThreadRunning, TRUE);
240 break;
244 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
247 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
249 if (AfxBeginThread(RevertThreadEntry, this)==0)
251 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
253 return 0;
256 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
258 BringWindowToTop();
259 SetForegroundWindow();
260 SetActiveWindow();
261 // if multiple files/folders are dropped
262 // this handler is called for every single item
263 // separately.
264 // To avoid creating multiple refresh threads and
265 // causing crashes, we only add the items to the
266 // list control and start a timer.
267 // When the timer expires, we start the refresh thread,
268 // but only if it isn't already running - otherwise we
269 // restart the timer.
270 CTGitPath path;
271 path.SetFromWin((LPCTSTR)lParam);
273 if (!m_RevertList.HasPath(path))
275 if (m_pathList.AreAllPathsFiles())
277 m_pathList.AddPath(path);
278 m_pathList.RemoveDuplicates();
280 else
282 // if the path list contains folders, we have to check whether
283 // our just (maybe) added path is a child of one of those. If it is
284 // a child of a folder already in the list, we must not add it. Otherwise
285 // that path could show up twice in the list.
286 bool bHasParentInList = false;
287 for (int i=0; i<m_pathList.GetCount(); ++i)
289 if (m_pathList[i].IsAncestorOf(path))
291 bHasParentInList = true;
292 break;
295 if (!bHasParentInList)
297 m_pathList.AddPath(path);
298 m_pathList.RemoveDuplicates();
303 // Always start the timer, since the status of an existing item might have changed
304 SetTimer(REFRESHTIMER, 200, NULL);
305 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
306 return 0;
309 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
311 switch (nIDEvent)
313 case REFRESHTIMER:
314 if (m_bThreadRunning)
316 SetTimer(REFRESHTIMER, 200, NULL);
317 ATLTRACE("Wait some more before refreshing\n");
319 else
321 KillTimer(REFRESHTIMER);
322 ATLTRACE("Refreshing after items dropped\n");
323 OnSVNStatusListCtrlNeedsRefresh(0, 0);
325 break;
327 __super::OnTimer(nIDEvent);