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.
21 #include "TortoiseProc.h"
22 #include "MessageBox.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
)
34 , m_bIncludeIgnored(FALSE
)
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
)
55 ON_BN_CLICKED(IDC_INCLUDE_IGNORED
, &CAddDlg::OnBnClickedIncludeIgnored
)
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();
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
);
89 CenterWindow(CWnd::FromHandle(hWndExplorer
));
90 EnableSaveRestore(_T("AddDlg"));
92 //first start a thread to obtain the file list with the status without
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
);
103 if (m_bThreadRunning
)
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()
115 if (m_bThreadRunning
)
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
);
159 BOOL
CAddDlg::PreTranslateMessage(MSG
* pMsg
)
161 if (pMsg
->message
== WM_KEYDOWN
)
163 switch (pMsg
->wParam
)
167 if (GetAsyncKeyState(VK_CONTROL
)&0x8000)
169 if ( GetDlgItem(IDOK
)->IsWindowEnabled() )
170 PostMessage(WM_COMMAND
, IDOK
);
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
);
193 LRESULT
CAddDlg::OnFileDropped(WPARAM
, LPARAM lParam
)
196 SetForegroundWindow();
198 // if multiple files/folders are dropped
199 // this handler is called for every single item
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.
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();
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;
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());
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
);
253 InterlockedExchange(&m_bThreadRunning
, TRUE
);
257 void CAddDlg::OnTimer(UINT_PTR nIDEvent
)
262 if (m_bThreadRunning
)
264 SetTimer(REFRESHTIMER
, 200, nullptr);
265 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Wait some more before refreshing\n");
269 KillTimer(REFRESHTIMER
);
270 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Refreshing after items dropped\n");
271 OnSVNStatusListCtrlNeedsRefresh(0, 0);
275 __super::OnTimer(nIDEvent
);
278 void CAddDlg::OnBnClickedIncludeIgnored()