Centralize Open-With handling
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blobe5adf70d8f75807ed4b63c58eccb8fab980dad1b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013 - 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 /*=NULL*/)
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, _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.CombinePath(m_pathList[0].GetUIPathString()), 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::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 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _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 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
319 else
321 KillTimer(REFRESHTIMER);
322 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
323 OnSVNStatusListCtrlNeedsRefresh(0, 0);
325 break;
327 __super::OnTimer(nIDEvent);