Replace all calls to LoadIcon and LoadImage with calls to LoadIconWithScaleDown
[TortoiseGit.git] / src / TortoiseProc / AddDlg.cpp
blobd44c916cf1a6f6dbbe5006bc9ad27821c69c385e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2018 - 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"
28 #define REFRESHTIMER 100
30 IMPLEMENT_DYNAMIC(CAddDlg, CResizableStandAloneDialog)
31 CAddDlg::CAddDlg(CWnd* pParent /*=nullptr*/)
32 : CResizableStandAloneDialog(CAddDlg::IDD, pParent)
33 , m_bThreadRunning(FALSE)
34 , m_bCancelled(false)
35 , m_bIncludeIgnored(FALSE)
39 CAddDlg::~CAddDlg()
43 void CAddDlg::DoDataExchange(CDataExchange* pDX)
45 CResizableStandAloneDialog::DoDataExchange(pDX);
46 DDX_Control(pDX, IDC_ADDLIST, m_addListCtrl);
47 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
48 DDX_Check(pDX, IDC_INCLUDE_IGNORED, m_bIncludeIgnored);
51 BEGIN_MESSAGE_MAP(CAddDlg, CResizableStandAloneDialog)
52 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
53 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
54 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
55 ON_WM_TIMER()
56 ON_BN_CLICKED(IDC_INCLUDE_IGNORED, &CAddDlg::OnBnClickedIncludeIgnored)
57 END_MESSAGE_MAP()
60 BOOL CAddDlg::OnInitDialog()
62 CResizableStandAloneDialog::OnInitDialog();
63 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
65 // initialize the svn status list control
66 m_addListCtrl.Init(GITSLC_COLEXT, L"AddDlg", GITSLC_POPALL ^ (GITSLC_POPADD | GITSLC_POPCOMMIT | GITSLC_POPCHANGELISTS | GITSLC_POPPREPAREDIFF), true, true, GITSLC_COLEXT | GITSLC_COLMODIFICATIONDATE | GITSLC_COLSIZE); // adding and committing is useless in the add dialog
67 m_addListCtrl.SetIgnoreRemoveOnly(); // when ignoring, don't add the parent folder since we're in the add dialog
68 m_addListCtrl.SetSelectButton(&m_SelectAll);
69 m_addListCtrl.SetConfirmButton((CButton*)GetDlgItem(IDOK));
70 m_addListCtrl.SetEmptyString(IDS_ERR_NOTHINGTOADD);
71 m_addListCtrl.SetCancelBool(&m_bCancelled);
72 m_addListCtrl.SetBackgroundImage(IDI_ADD_BKG);
73 m_addListCtrl.EnableFileDrop();
75 CString sWindowTitle;
76 GetWindowText(sWindowTitle);
77 CAppUtils::SetWindowTitle(m_hWnd, g_Git.CombinePath(m_pathList.GetCommonRoot().GetUIPathString()), sWindowTitle);
79 AdjustControlSize(IDC_SELECTALL);
80 AdjustControlSize(IDC_INCLUDE_IGNORED);
82 AddAnchor(IDC_ADDLIST, TOP_LEFT, BOTTOM_RIGHT);
83 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
84 AddAnchor(IDC_INCLUDE_IGNORED, BOTTOM_LEFT);
85 AddAnchor(IDOK, BOTTOM_RIGHT);
86 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
87 AddAnchor(IDHELP, BOTTOM_RIGHT);
89 if (GetExplorerHWND())
90 CenterWindow(CWnd::FromHandle(GetExplorerHWND()));
91 EnableSaveRestore(L"AddDlg");
93 //first start a thread to obtain the file list with the status without
94 //blocking the dialog
95 if (!AfxBeginThread(AddThreadEntry, this))
96 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
97 InterlockedExchange(&m_bThreadRunning, TRUE);
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 reinterpret_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 (!AfxBeginThread(AddThreadEntry, this))
191 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
192 return 0;
195 LRESULT CAddDlg::OnFileDropped(WPARAM, LPARAM lParam)
197 BringWindowToTop();
198 SetForegroundWindow();
199 SetActiveWindow();
200 // if multiple files/folders are dropped
201 // this handler is called for every single item
202 // separately.
203 // To avoid creating multiple refresh threads and
204 // causing crashes, we only add the items to the
205 // list control and start a timer.
206 // When the timer expires, we start the refresh thread,
207 // but only if it isn't already running - otherwise we
208 // restart the timer.
209 CTGitPath path;
210 path.SetFromWin((LPCTSTR)lParam);
212 // check whether the dropped file belongs to the very same repository
213 CString projectDir;
214 if (!path.HasAdminDir(&projectDir) || !CPathUtils::ArePathStringsEqual(g_Git.m_CurrentDir, projectDir))
215 return 0;
217 if (!m_addListCtrl.HasPath(path))
219 if (m_pathList.AreAllPathsFiles())
221 m_pathList.AddPath(path);
222 m_pathList.RemoveDuplicates();
224 else
226 // if the path list contains folders, we have to check whether
227 // our just (maybe) added path is a child of one of those. If it is
228 // a child of a folder already in the list, we must not add it. Otherwise
229 // that path could show up twice in the list.
230 bool bHasParentInList = false;
231 for (int i=0; i<m_pathList.GetCount(); ++i)
233 if (m_pathList[i].IsAncestorOf(path))
235 bHasParentInList = true;
236 break;
239 if (!bHasParentInList)
241 m_pathList.AddPath(path);
242 m_pathList.RemoveDuplicates();
246 m_addListCtrl.ResetChecked(path);
248 // Always start the timer, since the status of an existing item might have changed
249 SetTimer(REFRESHTIMER, 200, nullptr);
250 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Item %s dropped, timer started\n", path.GetWinPath());
251 return 0;
254 void CAddDlg::Refresh()
256 if (!m_bThreadRunning)
258 if (!AfxBeginThread(AddThreadEntry, this))
259 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
260 else
261 InterlockedExchange(&m_bThreadRunning, TRUE);
265 void CAddDlg::OnTimer(UINT_PTR nIDEvent)
267 switch (nIDEvent)
269 case REFRESHTIMER:
270 if (m_bThreadRunning)
272 SetTimer(REFRESHTIMER, 200, nullptr);
273 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
275 else
277 KillTimer(REFRESHTIMER);
278 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
279 OnSVNStatusListCtrlNeedsRefresh(0, 0);
281 break;
283 __super::OnTimer(nIDEvent);
286 void CAddDlg::OnBnClickedIncludeIgnored()
288 UpdateData();
289 Refresh();