Use correct length for buffer
[TortoiseGit.git] / src / TortoiseProc / AddDlg.cpp
blob983f7efb86ad1609f9809f94225fa72f2ed6f611
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - 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 "Git.h"
25 #include "AppUtils.h"
27 #define REFRESHTIMER 100
29 IMPLEMENT_DYNAMIC(CAddDlg, CResizableStandAloneDialog)
30 CAddDlg::CAddDlg(CWnd* pParent /*=nullptr*/)
31 : CResizableStandAloneDialog(CAddDlg::IDD, pParent)
32 , m_bThreadRunning(FALSE)
33 , m_bCancelled(false)
34 , m_bIncludeIgnored(FALSE)
38 CAddDlg::~CAddDlg()
42 void CAddDlg::DoDataExchange(CDataExchange* pDX)
44 CResizableStandAloneDialog::DoDataExchange(pDX);
45 DDX_Control(pDX, IDC_ADDLIST, m_addListCtrl);
46 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
47 DDX_Check(pDX, IDC_INCLUDE_IGNORED, m_bIncludeIgnored);
50 BEGIN_MESSAGE_MAP(CAddDlg, CResizableStandAloneDialog)
51 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
52 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
53 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
54 ON_WM_TIMER()
55 ON_BN_CLICKED(IDC_INCLUDE_IGNORED, &CAddDlg::OnBnClickedIncludeIgnored)
56 END_MESSAGE_MAP()
59 BOOL CAddDlg::OnInitDialog()
61 CResizableStandAloneDialog::OnInitDialog();
62 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
64 // initialize the svn status list control
65 m_addListCtrl.Init(GITSLC_COLEXT, _T("AddDlg"), GITSLC_POPALL ^ (GITSLC_POPADD | GITSLC_POPCOMMIT | GITSLC_POPCHANGELISTS | GITSLC_PREPAREDIFF), 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((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 CString sWindowTitle;
75 GetWindowText(sWindowTitle);
76 CAppUtils::SetWindowTitle(m_hWnd, g_Git.CombinePath(m_pathList.GetCommonRoot().GetUIPathString()), sWindowTitle);
78 AdjustControlSize(IDC_SELECTALL);
79 AdjustControlSize(IDC_INCLUDE_IGNORED);
81 AddAnchor(IDC_ADDLIST, TOP_LEFT, BOTTOM_RIGHT);
82 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
83 AddAnchor(IDC_INCLUDE_IGNORED, BOTTOM_LEFT);
84 AddAnchor(IDOK, BOTTOM_RIGHT);
85 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
86 AddAnchor(IDHELP, BOTTOM_RIGHT);
88 if (hWndExplorer)
89 CenterWindow(CWnd::FromHandle(hWndExplorer));
90 EnableSaveRestore(_T("AddDlg"));
92 //first start a thread to obtain the file list with the status without
93 //blocking the dialog
94 if (!AfxBeginThread(AddThreadEntry, this))
95 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
96 InterlockedExchange(&m_bThreadRunning, TRUE);
98 return TRUE;
101 void CAddDlg::OnOK()
103 if (m_bThreadRunning)
104 return;
106 // save only the files the user has selected into the path list
107 m_addListCtrl.WriteCheckedNamesToPathList(m_pathList);
109 CResizableStandAloneDialog::OnOK();
112 void CAddDlg::OnCancel()
114 m_bCancelled = true;
115 if (m_bThreadRunning)
116 return;
118 CResizableStandAloneDialog::OnCancel();
121 void CAddDlg::OnBnClickedSelectall()
123 UINT state = (m_SelectAll.GetState() & 0x0003);
124 if (state == BST_INDETERMINATE)
126 // It is not at all useful to manually place the checkbox into the indeterminate state...
127 // We will force this on to the unchecked state
128 state = BST_UNCHECKED;
129 m_SelectAll.SetCheck(state);
131 theApp.DoWaitCursor(1);
132 m_addListCtrl.SelectAll(state == BST_CHECKED);
133 theApp.DoWaitCursor(-1);
136 UINT CAddDlg::AddThreadEntry(LPVOID pVoid)
138 return ((CAddDlg*)pVoid)->AddThread();
141 UINT CAddDlg::AddThread()
143 // get the status of all selected file/folders recursively
144 // and show the ones which the user can add (i.e. the unversioned ones)
145 DialogEnableWindow(IDOK, false);
146 m_bCancelled = false;
147 m_addListCtrl.Clear();
148 if (!m_addListCtrl.GetStatus(&m_pathList, false, m_bIncludeIgnored != FALSE, true))
149 m_addListCtrl.SetEmptyString(m_addListCtrl.GetLastErrorMessage());
150 unsigned int dwShow = GITSLC_SHOWUNVERSIONED | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWREMOVEDANDPRESENT;
151 if (m_bIncludeIgnored)
152 dwShow |= GITSLC_SHOWIGNORED;
153 m_addListCtrl.Show(dwShow);
155 InterlockedExchange(&m_bThreadRunning, FALSE);
156 return 0;
159 BOOL CAddDlg::PreTranslateMessage(MSG* pMsg)
161 if (pMsg->message == WM_KEYDOWN)
163 switch (pMsg->wParam)
165 case VK_RETURN:
167 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
169 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
170 PostMessage(WM_COMMAND, IDOK);
171 return TRUE;
174 break;
175 case VK_F5:
177 Refresh();
179 break;
183 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
186 LRESULT CAddDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
188 if (!AfxBeginThread(AddThreadEntry, this))
189 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
190 return 0;
193 LRESULT CAddDlg::OnFileDropped(WPARAM, LPARAM lParam)
195 BringWindowToTop();
196 SetForegroundWindow();
197 SetActiveWindow();
198 // if multiple files/folders are dropped
199 // this handler is called for every single item
200 // separately.
201 // To avoid creating multiple refresh threads and
202 // causing crashes, we only add the items to the
203 // list control and start a timer.
204 // When the timer expires, we start the refresh thread,
205 // but only if it isn't already running - otherwise we
206 // restart the timer.
207 CTGitPath path;
208 path.SetFromWin((LPCTSTR)lParam);
210 if (!m_addListCtrl.HasPath(path))
212 if (m_pathList.AreAllPathsFiles())
214 m_pathList.AddPath(path);
215 m_pathList.RemoveDuplicates();
217 else
219 // if the path list contains folders, we have to check whether
220 // our just (maybe) added path is a child of one of those. If it is
221 // a child of a folder already in the list, we must not add it. Otherwise
222 // that path could show up twice in the list.
223 bool bHasParentInList = false;
224 for (int i=0; i<m_pathList.GetCount(); ++i)
226 if (m_pathList[i].IsAncestorOf(path))
228 bHasParentInList = true;
229 break;
232 if (!bHasParentInList)
234 m_pathList.AddPath(path);
235 m_pathList.RemoveDuplicates();
240 // Always start the timer, since the status of an existing item might have changed
241 SetTimer(REFRESHTIMER, 200, nullptr);
242 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Item %s dropped, timer started\n"), path.GetWinPath());
243 return 0;
246 void CAddDlg::Refresh()
248 if (!m_bThreadRunning)
250 if (!AfxBeginThread(AddThreadEntry, this))
251 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
252 else
253 InterlockedExchange(&m_bThreadRunning, TRUE);
257 void CAddDlg::OnTimer(UINT_PTR nIDEvent)
259 switch (nIDEvent)
261 case REFRESHTIMER:
262 if (m_bThreadRunning)
264 SetTimer(REFRESHTIMER, 200, nullptr);
265 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
267 else
269 KillTimer(REFRESHTIMER);
270 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
271 OnSVNStatusListCtrlNeedsRefresh(0, 0);
273 break;
275 __super::OnTimer(nIDEvent);
278 void CAddDlg::OnBnClickedIncludeIgnored()
280 UpdateData();
281 Refresh();