Show selected refs in list ctrl
[TortoiseGit.git] / src / TortoiseProc / AddDlg.cpp
blob7cc113524c6035ea59e64d5becfe5a1661ea4423
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "TortoiseProc.h"
21 #include "messagebox.h"
22 #include "DirFileEnum.h"
23 #include "AddDlg.h"
24 //#include "SVNConfig.h"
25 #include "Registry.h"
26 #include "CommonResource.h"
28 #define REFRESHTIMER 100
30 IMPLEMENT_DYNAMIC(CAddDlg, CResizableStandAloneDialog)
31 CAddDlg::CAddDlg(CWnd* pParent /*=NULL*/)
32 : CResizableStandAloneDialog(CAddDlg::IDD, pParent)
33 , m_bThreadRunning(FALSE)
34 , m_bCancelled(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);
49 BEGIN_MESSAGE_MAP(CAddDlg, CResizableStandAloneDialog)
50 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
51 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
52 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
53 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ADDFILE, OnFileDropped)
54 ON_WM_TIMER()
55 END_MESSAGE_MAP()
58 BOOL CAddDlg::OnInitDialog()
60 CResizableStandAloneDialog::OnInitDialog();
62 // initialize the svn status list control
63 m_addListCtrl.Init(SVNSLC_COLEXT, _T("AddDlg"), SVNSLC_POPALL ^ (SVNSLC_POPADD|SVNSLC_POPCOMMIT|SVNSLC_POPCHANGELISTS)); // adding and committing is useless in the add dialog
64 m_addListCtrl.SetIgnoreRemoveOnly(); // when ignoring, don't add the parent folder since we're in the add dialog
65 m_addListCtrl.SetUnversionedRecurse(true); // recurse into unversioned folders - user might want to add those too
66 m_addListCtrl.SetSelectButton(&m_SelectAll);
67 m_addListCtrl.SetConfirmButton((CButton*)GetDlgItem(IDOK));
68 m_addListCtrl.SetEmptyString(IDS_ERR_NOTHINGTOADD);
69 m_addListCtrl.SetCancelBool(&m_bCancelled);
70 m_addListCtrl.SetBackgroundImage(IDI_ADD_BKG);
71 m_addListCtrl.EnableFileDrop();
73 AdjustControlSize(IDC_SELECTALL);
75 AddAnchor(IDC_ADDLIST, TOP_LEFT, BOTTOM_RIGHT);
76 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
77 AddAnchor(IDOK, BOTTOM_RIGHT);
78 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
79 AddAnchor(IDHELP, BOTTOM_RIGHT);
80 if (hWndExplorer)
81 CenterWindow(CWnd::FromHandle(hWndExplorer));
82 EnableSaveRestore(_T("AddDlg"));
84 //first start a thread to obtain the file list with the status without
85 //blocking the dialog
86 if(AfxBeginThread(AddThreadEntry, this) == NULL)
88 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
90 InterlockedExchange(&m_bThreadRunning, TRUE);
92 return TRUE;
95 void CAddDlg::OnOK()
97 if (m_bThreadRunning)
98 return;
100 // save only the files the user has selected into the path list
101 m_addListCtrl.WriteCheckedNamesToPathList(m_pathList);
103 CResizableStandAloneDialog::OnOK();
106 void CAddDlg::OnCancel()
108 m_bCancelled = true;
109 if (m_bThreadRunning)
110 return;
112 CResizableStandAloneDialog::OnCancel();
115 void CAddDlg::OnBnClickedSelectall()
117 UINT state = (m_SelectAll.GetState() & 0x0003);
118 if (state == BST_INDETERMINATE)
120 // It is not at all useful to manually place the checkbox into the indeterminate state...
121 // We will force this on to the unchecked state
122 state = BST_UNCHECKED;
123 m_SelectAll.SetCheck(state);
125 theApp.DoWaitCursor(1);
126 m_addListCtrl.SelectAll(state == BST_CHECKED);
127 theApp.DoWaitCursor(-1);
130 UINT CAddDlg::AddThreadEntry(LPVOID pVoid)
132 return ((CAddDlg*)pVoid)->AddThread();
135 UINT CAddDlg::AddThread()
137 // get the status of all selected file/folders recursively
138 // and show the ones which the user can add (i.e. the unversioned ones)
139 DialogEnableWindow(IDOK, false);
140 m_bCancelled = false;
141 m_addListCtrl.Clear();
142 if (!m_addListCtrl.GetStatus(&m_pathList,false,false,true,true))
144 m_addListCtrl.SetEmptyString(m_addListCtrl.GetLastErrorMessage());
146 m_addListCtrl.Show(SVNSLC_SHOWUNVERSIONED | SVNSLC_SHOWDIRECTFILES | SVNSLC_SHOWREMOVEDANDPRESENT,
147 SVNSLC_SHOWUNVERSIONED | SVNSLC_SHOWDIRECTFILES | SVNSLC_SHOWREMOVEDANDPRESENT,true,true);
149 InterlockedExchange(&m_bThreadRunning, FALSE);
150 return 0;
153 void CAddDlg::OnBnClickedHelp()
155 OnHelp();
158 BOOL CAddDlg::PreTranslateMessage(MSG* pMsg)
160 if (pMsg->message == WM_KEYDOWN)
162 switch (pMsg->wParam)
164 case VK_RETURN:
166 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
168 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
170 PostMessage(WM_COMMAND, IDOK);
172 return TRUE;
175 break;
176 case VK_F5:
178 if (!m_bThreadRunning)
180 if(AfxBeginThread(AddThreadEntry, this) == NULL)
182 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
184 else
185 InterlockedExchange(&m_bThreadRunning, TRUE);
188 break;
192 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
195 LRESULT CAddDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
197 if(AfxBeginThread(AddThreadEntry, this) == NULL)
199 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
201 return 0;
204 LRESULT CAddDlg::OnFileDropped(WPARAM, LPARAM lParam)
206 BringWindowToTop();
207 SetForegroundWindow();
208 SetActiveWindow();
209 // if multiple files/folders are dropped
210 // this handler is called for every single item
211 // separately.
212 // To avoid creating multiple refresh threads and
213 // causing crashes, we only add the items to the
214 // list control and start a timer.
215 // When the timer expires, we start the refresh thread,
216 // but only if it isn't already running - otherwise we
217 // restart the timer.
218 CTGitPath path;
219 path.SetFromWin((LPCTSTR)lParam);
221 if (!m_addListCtrl.HasPath(path))
223 if (m_pathList.AreAllPathsFiles())
225 m_pathList.AddPath(path);
226 m_pathList.RemoveDuplicates();
228 else
230 // if the path list contains folders, we have to check whether
231 // our just (maybe) added path is a child of one of those. If it is
232 // a child of a folder already in the list, we must not add it. Otherwise
233 // that path could show up twice in the list.
234 bool bHasParentInList = false;
235 for (int i=0; i<m_pathList.GetCount(); ++i)
237 if (m_pathList[i].IsAncestorOf(path))
239 bHasParentInList = true;
240 break;
243 if (!bHasParentInList)
245 m_pathList.AddPath(path);
246 m_pathList.RemoveDuplicates();
251 // Always start the timer, since the status of an existing item might have changed
252 SetTimer(REFRESHTIMER, 200, NULL);
253 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
254 return 0;
257 void CAddDlg::OnTimer(UINT_PTR nIDEvent)
259 switch (nIDEvent)
261 case REFRESHTIMER:
262 if (m_bThreadRunning)
264 SetTimer(REFRESHTIMER, 200, NULL);
265 ATLTRACE("Wait some more before refreshing\n");
267 else
269 KillTimer(REFRESHTIMER);
270 ATLTRACE("Refreshing after items dropped\n");
271 OnSVNStatusListCtrlNeedsRefresh(0, 0);
273 break;
275 __super::OnTimer(nIDEvent);