Remember "subfolder" as correctly spelled
[TortoiseGit.git] / src / TortoiseProc / AddDlg.cpp
blob407a64122550fc6d242a2c03fd91ff420beb7cb8
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2021, 2023-2024 - 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 "AddDlg.h"
24 #include "PathUtils.h"
25 #include "Git.h"
26 #include "AppUtils.h"
27 #include "AutoCloakWindow.h"
29 #define REFRESHTIMER 100
31 IMPLEMENT_DYNAMIC(CAddDlg, CResizableStandAloneDialog)
32 CAddDlg::CAddDlg(CWnd* pParent /*=nullptr*/)
33 : CResizableStandAloneDialog(CAddDlg::IDD, pParent)
37 CAddDlg::~CAddDlg()
41 void CAddDlg::DoDataExchange(CDataExchange* pDX)
43 CResizableStandAloneDialog::DoDataExchange(pDX);
44 DDX_Control(pDX, IDC_ADDLIST, m_addListCtrl);
45 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
46 DDX_Check(pDX, IDC_INCLUDE_IGNORED, m_bIncludeIgnored);
49 BEGIN_MESSAGE_MAP(CAddDlg, CResizableStandAloneDialog)
50 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
51 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
52 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
53 ON_WM_TIMER()
54 ON_BN_CLICKED(IDC_INCLUDE_IGNORED, &CAddDlg::OnBnClickedIncludeIgnored)
55 END_MESSAGE_MAP()
58 BOOL CAddDlg::OnInitDialog()
60 CAutoCloakWindow window_cloaker{ GetSafeHwnd() };
61 CResizableStandAloneDialog::OnInitDialog();
62 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
64 // initialize the svn status list control
65 m_addListCtrl.Init(GITSLC_COLEXT, L"AddDlg", GITSLC_POPALL ^ (GITSLC_POPADD | GITSLC_POPCOMMIT | GITSLC_POPCHANGELISTS | GITSLC_POPPREPAREDIFF | GITSLC_POPCHANGELISTS), true, true, GITSLC_COLEXT | GITSLC_COLMODIFICATIONDATE | GITSLC_COLSIZE); // adding and committing is useless in the add dialog
66 m_addListCtrl.SetIgnoreRemoveOnly(); // when ignoring, don't add the parent folder since we're in the add dialog
67 m_addListCtrl.SetSelectButton(&m_SelectAll);
68 m_addListCtrl.SetConfirmButton(static_cast<CButton*>(GetDlgItem(IDOK)));
69 m_addListCtrl.SetEmptyString(IDS_ERR_NOTHINGTOADD);
70 m_addListCtrl.SetCancelBool(&m_bCancelled);
71 m_addListCtrl.SetBackgroundImage(IDI_ADD_BKG);
72 m_addListCtrl.EnableFileDrop();
74 CAppUtils::SetWindowTitle(*this, g_Git.CombinePath(m_pathList.GetCommonRoot().GetUIPathString()));
76 AdjustControlSize(IDC_SELECTALL);
77 AdjustControlSize(IDC_INCLUDE_IGNORED);
79 AddAnchor(IDC_ADDLIST, TOP_LEFT, BOTTOM_RIGHT);
80 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
81 AddAnchor(IDC_INCLUDE_IGNORED, BOTTOM_LEFT);
82 AddAnchor(IDOK, BOTTOM_RIGHT);
83 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
84 AddAnchor(IDHELP, BOTTOM_RIGHT);
86 if (GetExplorerHWND())
87 CenterWindow(CWnd::FromHandle(GetExplorerHWND()));
88 EnableSaveRestore(L"AddDlg");
89 SetTheme(CTheme::Instance().IsDarkTheme());
91 //first start a thread to obtain the file list with the status without
92 //blocking the dialog
93 InterlockedExchange(&m_bThreadRunning, TRUE);
94 if (!AfxBeginThread(AddThreadEntry, this))
96 InterlockedExchange(&m_bThreadRunning, FALSE);
97 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
99 return TRUE;
102 void CAddDlg::OnOK()
104 if (m_bThreadRunning)
105 return;
107 // save only the files the user has selected into the path list
108 m_addListCtrl.WriteCheckedNamesToPathList(m_pathList);
110 CResizableStandAloneDialog::OnOK();
113 void CAddDlg::OnCancel()
115 m_bCancelled = true;
116 if (m_bThreadRunning)
117 return;
119 CResizableStandAloneDialog::OnCancel();
122 void CAddDlg::OnBnClickedSelectall()
124 UINT state = (m_SelectAll.GetState() & 0x0003);
125 if (state == BST_INDETERMINATE)
127 // It is not at all useful to manually place the checkbox into the indeterminate state...
128 // We will force this on to the unchecked state
129 state = BST_UNCHECKED;
130 m_SelectAll.SetCheck(state);
132 theApp.DoWaitCursor(1);
133 m_addListCtrl.SelectAll(state == BST_CHECKED);
134 theApp.DoWaitCursor(-1);
137 UINT CAddDlg::AddThreadEntry(LPVOID pVoid)
139 return static_cast<CAddDlg*>(pVoid)->AddThread();
142 UINT CAddDlg::AddThread()
144 // get the status of all selected file/folders recursively
145 // and show the ones which the user can add (i.e. the unversioned ones)
146 DialogEnableWindow(IDOK, false);
147 m_bCancelled = false;
148 m_addListCtrl.StoreScrollPos();
149 m_addListCtrl.Clear();
150 if (!m_addListCtrl.GetStatus(&m_pathList, false, m_bIncludeIgnored != FALSE, true))
151 m_addListCtrl.SetEmptyString(m_addListCtrl.GetLastErrorMessage());
152 unsigned int dwShow = GITSLC_SHOWUNVERSIONED | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWREMOVEDANDPRESENT;
153 if (m_bIncludeIgnored)
154 dwShow |= GITSLC_SHOWIGNORED;
155 m_addListCtrl.Show(dwShow, GITSLC_SHOWUNVERSIONED);
157 InterlockedExchange(&m_bThreadRunning, FALSE);
158 return 0;
161 BOOL CAddDlg::PreTranslateMessage(MSG* pMsg)
163 if (pMsg->message == WM_KEYDOWN)
165 switch (pMsg->wParam)
167 case VK_RETURN:
169 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
171 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
172 PostMessage(WM_COMMAND, IDOK);
173 return TRUE;
176 break;
177 case VK_F5:
179 Refresh();
181 break;
185 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
188 LRESULT CAddDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
190 if (InterlockedExchange(&m_bThreadRunning, TRUE))
191 return 0;
192 if (!AfxBeginThread(AddThreadEntry, this))
194 InterlockedExchange(&m_bThreadRunning, FALSE);
195 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
197 return 0;
200 LRESULT CAddDlg::OnFileDropped(WPARAM, LPARAM lParam)
202 BringWindowToTop();
203 SetForegroundWindow();
204 SetActiveWindow();
205 // if multiple files/folders are dropped
206 // this handler is called for every single item
207 // separately.
208 // To avoid creating multiple refresh threads and
209 // causing crashes, we only add the items to the
210 // list control and start a timer.
211 // When the timer expires, we start the refresh thread,
212 // but only if it isn't already running - otherwise we
213 // restart the timer.
214 CTGitPath path;
215 path.SetFromWin(reinterpret_cast<LPCWSTR>(lParam));
217 // check whether the dropped file belongs to the very same repository
218 CString projectDir;
219 if (!path.HasAdminDir(&projectDir) || !CPathUtils::ArePathStringsEqual(g_Git.m_CurrentDir, projectDir))
220 return 0;
222 if (!m_addListCtrl.HasPath(path))
224 if (m_pathList.AreAllPathsFiles())
226 m_pathList.AddPath(path);
227 m_pathList.RemoveDuplicates();
229 else
231 // if the path list contains folders, we have to check whether
232 // our just (maybe) added path is a child of one of those. If it is
233 // a child of a folder already in the list, we must not add it. Otherwise
234 // that path could show up twice in the list.
235 if (!m_pathList.IsAnyAncestorOf(path))
237 m_pathList.AddPath(path);
238 m_pathList.RemoveDuplicates();
242 m_addListCtrl.ResetChecked(path);
244 // Always start the timer, since the status of an existing item might have changed
245 SetTimer(REFRESHTIMER, 200, nullptr);
246 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Item %s dropped, timer started\n", path.GetWinPath());
247 return 0;
250 void CAddDlg::Refresh()
252 if (InterlockedExchange(&m_bThreadRunning, TRUE))
253 return;
254 if (!AfxBeginThread(AddThreadEntry, this))
256 InterlockedExchange(&m_bThreadRunning, FALSE);
257 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
261 void CAddDlg::OnTimer(UINT_PTR nIDEvent)
263 switch (nIDEvent)
265 case REFRESHTIMER:
266 if (m_bThreadRunning)
268 SetTimer(REFRESHTIMER, 200, nullptr);
269 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
271 else
273 KillTimer(REFRESHTIMER);
274 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
275 OnSVNStatusListCtrlNeedsRefresh(0, 0);
277 break;
279 __super::OnTimer(nIDEvent);
282 void CAddDlg::OnBnClickedIncludeIgnored()
284 UpdateData();
285 Refresh();