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.
21 #include "TortoiseProc.h"
22 #include "MessageBox.h"
24 #include "PathUtils.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
)
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
)
54 ON_BN_CLICKED(IDC_INCLUDE_IGNORED
, &CAddDlg::OnBnClickedIncludeIgnored
)
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
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
);
104 if (m_bThreadRunning
)
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()
116 if (m_bThreadRunning
)
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
);
161 BOOL
CAddDlg::PreTranslateMessage(MSG
* pMsg
)
163 if (pMsg
->message
== WM_KEYDOWN
)
165 switch (pMsg
->wParam
)
169 if (GetAsyncKeyState(VK_CONTROL
)&0x8000)
171 if ( GetDlgItem(IDOK
)->IsWindowEnabled() )
172 PostMessage(WM_COMMAND
, IDOK
);
185 return CResizableStandAloneDialog::PreTranslateMessage(pMsg
);
188 LRESULT
CAddDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM
, LPARAM
)
190 if (InterlockedExchange(&m_bThreadRunning
, TRUE
))
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
);
200 LRESULT
CAddDlg::OnFileDropped(WPARAM
, LPARAM lParam
)
203 SetForegroundWindow();
205 // if multiple files/folders are dropped
206 // this handler is called for every single item
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.
215 path
.SetFromWin(reinterpret_cast<LPCWSTR
>(lParam
));
217 // check whether the dropped file belongs to the very same repository
219 if (!path
.HasAdminDir(&projectDir
) || !CPathUtils::ArePathStringsEqual(g_Git
.m_CurrentDir
, projectDir
))
222 if (!m_addListCtrl
.HasPath(path
))
224 if (m_pathList
.AreAllPathsFiles())
226 m_pathList
.AddPath(path
);
227 m_pathList
.RemoveDuplicates();
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());
250 void CAddDlg::Refresh()
252 if (InterlockedExchange(&m_bThreadRunning
, TRUE
))
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
)
266 if (m_bThreadRunning
)
268 SetTimer(REFRESHTIMER
, 200, nullptr);
269 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Wait some more before refreshing\n");
273 KillTimer(REFRESHTIMER
);
274 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Refreshing after items dropped\n");
275 OnSVNStatusListCtrlNeedsRefresh(0, 0);
279 __super::OnTimer(nIDEvent
);
282 void CAddDlg::OnBnClickedIncludeIgnored()