doc: updated images
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blobbb8caf158b55282949c6039dc5f22ecd1fb9a9f8
1 // TortoiseGit - 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 "AppUtils.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::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(0, _T("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 if (m_pathList.GetCount() == 1)
77 CAppUtils::SetWindowTitle(m_hWnd, (g_Git.m_CurrentDir + _T("\\") + m_pathList[0].GetUIPathString()).TrimRight('\\'), sWindowTitle);
78 else
79 CAppUtils::SetWindowTitle(m_hWnd, m_pathList.GetCommonRoot().GetUIPathString(), sWindowTitle);
81 AdjustControlSize(IDC_SELECTALL);
83 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
84 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
85 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
86 AddAnchor(IDOK, BOTTOM_RIGHT);
87 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
88 AddAnchor(IDHELP, BOTTOM_RIGHT);
89 if (hWndExplorer)
90 CenterWindow(CWnd::FromHandle(hWndExplorer));
91 EnableSaveRestore(_T("RevertDlg"));
93 // first start a thread to obtain the file list with the status without
94 // blocking the dialog
95 if (AfxBeginThread(RevertThreadEntry, this)==0)
97 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
99 InterlockedExchange(&m_bThreadRunning, TRUE);
101 return TRUE;
104 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
106 return ((CRevertDlg*)pVoid)->RevertThread();
109 UINT CRevertDlg::RevertThread()
111 // get the status of all selected file/folders recursively
112 // and show the ones which can be reverted to the user
113 // in a list control.
114 DialogEnableWindow(IDOK, false);
115 m_bCancelled = false;
117 m_RevertList.Clear();
119 if (!m_RevertList.GetStatus(&m_pathList))
121 m_RevertList.SetEmptyString(m_RevertList.GetLastErrorMessage());
123 m_RevertList.Show(GITSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWEXTERNALFROMDIFFERENTREPO,
124 // do not select all files, only the ones the user has selected directly
125 GITSLC_SHOWDIRECTFILES|GITSLC_SHOWADDED);
127 if (m_RevertList.HasUnversionedItems())
129 if (DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\UnversionedAsModified"), FALSE)))
131 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_SHOW);
133 else
134 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
136 else
137 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
139 InterlockedExchange(&m_bThreadRunning, FALSE);
140 RefreshCursor();
142 return 0;
145 void CRevertDlg::OnOK()
147 if (m_bThreadRunning)
148 return;
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))
155 m_bRecursive = FALSE;
157 else
159 m_selectedPathList.AddPath(*(CTGitPath*)m_RevertList.GetItemData(i));
160 #if 0
161 CGitStatusListCtrl::FileEntry * entry = m_RevertList.GetListEntry(i);
162 // add all selected entries to the list, except the ones with 'added'
163 // status: we later *delete* all the entries in the list before
164 // the actual revert is done (so the user has the reverted files
165 // still in the trash bin to recover from), but it's not good to
166 // delete added files because they're not restored by the revert.
167 if (entry->status != svn_wc_status_added)
168 m_selectedPathList.AddPath(entry->GetPath());
169 // if an entry inside an external is selected, we can't revert
170 // recursively anymore because the recursive revert stops at the
171 // external boundaries.
172 if (entry->IsInExternal())
173 m_bRecursive = FALSE;
174 #endif
177 if (!m_bRecursive)
179 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
181 m_selectedPathList.SortByPathname();
183 CResizableStandAloneDialog::OnOK();
186 void CRevertDlg::OnCancel()
188 m_bCancelled = true;
189 if (m_bThreadRunning)
190 return;
192 CResizableStandAloneDialog::OnCancel();
195 void CRevertDlg::OnBnClickedHelp()
197 OnHelp();
200 void CRevertDlg::OnBnClickedSelectall()
202 UINT state = (m_SelectAll.GetState() & 0x0003);
203 if (state == BST_INDETERMINATE)
205 // It is not at all useful to manually place the checkbox into the indeterminate state...
206 // We will force this on to the unchecked state
207 state = BST_UNCHECKED;
208 m_SelectAll.SetCheck(state);
210 theApp.DoWaitCursor(1);
211 m_RevertList.SelectAll(state == BST_CHECKED);
212 theApp.DoWaitCursor(-1);
215 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
217 if (pMsg->message == WM_KEYDOWN)
219 switch (pMsg->wParam)
221 case VK_RETURN:
223 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
225 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
227 PostMessage(WM_COMMAND, IDOK);
229 return TRUE;
232 break;
233 case VK_F5:
235 if (!m_bThreadRunning)
237 if (AfxBeginThread(RevertThreadEntry, this)==0)
239 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
241 else
242 InterlockedExchange(&m_bThreadRunning, TRUE);
245 break;
249 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
252 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
254 if (AfxBeginThread(RevertThreadEntry, this)==0)
256 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
258 return 0;
261 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
263 BringWindowToTop();
264 SetForegroundWindow();
265 SetActiveWindow();
266 // if multiple files/folders are dropped
267 // this handler is called for every single item
268 // separately.
269 // To avoid creating multiple refresh threads and
270 // causing crashes, we only add the items to the
271 // list control and start a timer.
272 // When the timer expires, we start the refresh thread,
273 // but only if it isn't already running - otherwise we
274 // restart the timer.
275 CTGitPath path;
276 path.SetFromWin((LPCTSTR)lParam);
278 if (!m_RevertList.HasPath(path))
280 if (m_pathList.AreAllPathsFiles())
282 m_pathList.AddPath(path);
283 m_pathList.RemoveDuplicates();
285 else
287 // if the path list contains folders, we have to check whether
288 // our just (maybe) added path is a child of one of those. If it is
289 // a child of a folder already in the list, we must not add it. Otherwise
290 // that path could show up twice in the list.
291 bool bHasParentInList = false;
292 for (int i=0; i<m_pathList.GetCount(); ++i)
294 if (m_pathList[i].IsAncestorOf(path))
296 bHasParentInList = true;
297 break;
300 if (!bHasParentInList)
302 m_pathList.AddPath(path);
303 m_pathList.RemoveDuplicates();
308 // Always start the timer, since the status of an existing item might have changed
309 SetTimer(REFRESHTIMER, 200, NULL);
310 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
311 return 0;
314 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
316 switch (nIDEvent)
318 case REFRESHTIMER:
319 if (m_bThreadRunning)
321 SetTimer(REFRESHTIMER, 200, NULL);
322 ATLTRACE("Wait some more before refreshing\n");
324 else
326 KillTimer(REFRESHTIMER);
327 ATLTRACE("Refreshing after items dropped\n");
328 OnSVNStatusListCtrlNeedsRefresh(0, 0);
330 break;
332 __super::OnTimer(nIDEvent);