Replace var++ by ++var and use size_t where necessary
[TortoiseGit.git] / src / TortoiseProc / RefLogDlg.cpp
blobb8a55798b081cb28f448e3a26caa2acabf9ce152
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013 - TortoiseGit
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 // RefLogDlg.cpp : implementation file
22 #include "stdafx.h"
23 #include "resource.h"
24 #include "RefLogDlg.h"
25 #include "git.h"
26 #include "AppUtils.h"
27 #include "MessageBox.h"
29 // CRefLogDlg dialog
31 IMPLEMENT_DYNAMIC(CRefLogDlg, CResizableStandAloneDialog)
33 UINT CRefLogDlg::m_FindDialogMessage = ::RegisterWindowMessage(FINDMSGSTRING);
35 CRefLogDlg::CRefLogDlg(CWnd* pParent /*=NULL*/)
36 : CResizableStandAloneDialog(CRefLogDlg::IDD, pParent)
37 , m_pFindDialog(NULL)
38 , m_nSearchLine(0)
43 CRefLogDlg::~CRefLogDlg()
47 void CRefLogDlg::DoDataExchange(CDataExchange* pDX)
49 CDialog::DoDataExchange(pDX);
50 DDX_Control(pDX, IDC_COMBOBOXEX_REF, m_ChooseRef);
51 DDX_Control(pDX, IDC_REFLOG_LIST, m_RefList);
55 BEGIN_MESSAGE_MAP(CRefLogDlg, CResizableStandAloneDialog)
56 ON_BN_CLICKED(IDOK, &CRefLogDlg::OnBnClickedOk)
57 ON_BN_CLICKED(IDC_REFLOG_BUTTONCLEARSTASH, &CRefLogDlg::OnBnClickedClearStash)
58 ON_CBN_SELCHANGE(IDC_COMBOBOXEX_REF, &CRefLogDlg::OnCbnSelchangeRef)
59 ON_MESSAGE(MSG_REFLOG_CHANGED,OnRefLogChanged)
60 ON_REGISTERED_MESSAGE(m_FindDialogMessage, OnFindDialogMessage)
61 END_MESSAGE_MAP()
63 LRESULT CRefLogDlg::OnRefLogChanged(WPARAM /*wParam*/, LPARAM /*lParam*/)
65 m_RefList.m_RefMap.clear();
66 OnCbnSelchangeRef();
67 return 0;
70 BOOL CRefLogDlg::OnInitDialog()
72 CResizableStandAloneDialog::OnInitDialog();
73 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
75 AddAnchor(IDOK,BOTTOM_RIGHT);
76 AddAnchor(IDCANCEL,BOTTOM_RIGHT);
77 AddAnchor(IDC_REFLOG_BUTTONCLEARSTASH, BOTTOM_LEFT);
78 AddAnchor(IDC_REFLOG_LIST,TOP_LEFT,BOTTOM_RIGHT);
79 AddAnchor(IDHELP, BOTTOM_RIGHT);
80 AddAnchor(IDC_COMBOBOXEX_REF, TOP_LEFT, TOP_RIGHT);
82 AddOthersToAnchor();
83 this->EnableSaveRestore(_T("RefLogDlg"));
85 CString sWindowTitle;
86 GetWindowText(sWindowTitle);
87 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
89 m_ChooseRef.SetMaxHistoryItems(0x7FFFFFFF);
91 m_RefList.m_hasWC = !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir);
93 this->m_RefList.InsertRefLogColumn();
95 Refresh();
97 return TRUE;
99 // CRefLogDlg message handlers
101 void CRefLogDlg::OnBnClickedOk()
103 if (m_RefList.GetSelectedCount() == 1)
105 // get the selected row
106 POSITION pos = m_RefList.GetFirstSelectedItemPosition();
107 int selIndex = m_RefList.GetNextSelectedItem(pos);
108 if (selIndex < m_RefList.m_arShownList.GetCount())
110 // all ok, pick up the revision
111 GitRev* pLogEntry = reinterpret_cast<GitRev *>(m_RefList.m_arShownList.GetAt(selIndex));
112 // extract the hash
113 m_SelectedHash = pLogEntry->m_CommitHash;
117 OnOK();
119 void CRefLogDlg::OnBnClickedClearStash()
121 if (CMessageBox::Show(this->GetSafeHwnd(), IDS_PROC_DELETEALLSTASH, IDS_APPNAME, 2, IDI_QUESTION, IDS_DELETEBUTTON, IDS_ABORTBUTTON) == 1)
123 CString cmdOut;
124 if (g_Git.Run(_T("git.exe stash clear"), &cmdOut, CP_UTF8))
126 MessageBox(cmdOut, _T("TortoiseGit"), MB_ICONERROR);
127 return;
130 m_RefList.m_RefMap.clear();
132 OnCbnSelchangeRef();
136 void CRefLogDlg::OnCbnSelchangeRef()
138 CString ref=m_ChooseRef.GetString();
139 if(m_RefList.m_RefMap.find(ref) == m_RefList.m_RefMap.end())
141 m_RefList.m_RefMap[ref].m_pLogCache = &m_RefList.m_LogCache;
142 m_RefList.m_RefMap[ref].ParserFromRefLog(ref);
144 m_RefList.ClearText();
146 //this->m_logEntries.ParserFromLog();
147 m_RefList.SetRedraw(false);
149 CLogDataVector *plog;
150 plog = &m_RefList.m_RefMap[ref];
152 m_RefList.SetItemCountEx((int)plog->size());
154 this->m_RefList.m_arShownList.RemoveAll();
156 for (unsigned int i = 0; i < m_RefList.m_RefMap[ref].size(); ++i)
158 plog->GetGitRevAt(i).m_IsFull=TRUE;
159 this->m_RefList.m_arShownList.Add(&(plog->GetGitRevAt(i)));
163 m_RefList.SetRedraw(true);
165 m_RefList.Invalidate();
167 if (ref == _T("refs/stash"))
169 GetDlgItem(IDC_REFLOG_BUTTONCLEARSTASH)->ShowWindow(SW_SHOW);
170 GetDlgItem(IDC_REFLOG_BUTTONCLEARSTASH)->EnableWindow((m_RefList.m_arShownList.GetSize() > 0));
172 else
173 GetDlgItem(IDC_REFLOG_BUTTONCLEARSTASH)->ShowWindow(SW_HIDE);
176 BOOL CRefLogDlg::PreTranslateMessage(MSG* pMsg)
178 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_F5)
179 Refresh();
180 else if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_F3 || (pMsg->wParam == 'F' && (GetAsyncKeyState(VK_CONTROL) & 0x8000))))
181 OnFind();
183 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
186 void CRefLogDlg::Refresh()
188 STRING_VECTOR list;
189 list.push_back(_T("HEAD"));
190 if (g_Git.GetRefList(list))
191 MessageBox(g_Git.GetGitLastErr(_T("Could not get all refs.")), _T("TortoiseGit"), MB_ICONERROR);
193 m_ChooseRef.AddString(list);
195 if (m_CurrentBranch.IsEmpty())
197 m_CurrentBranch.Format(_T("refs/heads/%s"), g_Git.GetCurrentBranch());
198 m_ChooseRef.SetCurSel(0); /* Choose HEAD */
200 else
202 bool found = false;
203 for (int i = 0; i < list.size(); ++i)
205 if(list[i] == m_CurrentBranch)
207 m_ChooseRef.SetCurSel(i);
208 found = true;
209 break;
212 if (!found)
213 m_ChooseRef.SetCurSel(0);
216 m_RefList.m_RefMap.clear();
218 OnCbnSelchangeRef();
221 void CRefLogDlg::OnFind()
223 m_nSearchLine = 0;
224 m_pFindDialog = new CFindReplaceDialog();
225 m_pFindDialog->Create(TRUE, _T(""), NULL, FR_DOWN | FR_HIDEWHOLEWORD | FR_HIDEUPDOWN, this);
228 LRESULT CRefLogDlg::OnFindDialogMessage(WPARAM /*wParam*/, LPARAM /*lParam*/)
230 ASSERT(m_pFindDialog != NULL);
232 if (m_RefList.m_arShownList.IsEmpty())
233 return 0;
235 // If the FR_DIALOGTERM flag is set,
236 // invalidate the handle identifying the dialog box.
237 if (m_pFindDialog->IsTerminating())
239 m_pFindDialog = NULL;
240 return 0;
243 // If the FR_FINDNEXT flag is set,
244 // call the application-defined search routine
245 // to search for the requested string.
246 if (m_pFindDialog->FindNext())
248 //read data from dialog
249 CString findString = m_pFindDialog->GetFindString();
251 bool bFound = false;
252 bool bCaseSensitive = !!(m_pFindDialog->m_nFlags & FR_MATCHCASE);
254 if (!bCaseSensitive)
255 findString.MakeLower();
257 int i = m_nSearchLine;
258 if (i < 0 || i >= m_RefList.m_arShownList.GetCount())
259 i = 0;
263 GitRev * data = (GitRev*)m_RefList.m_arShownList.SafeGetAt(i);
265 CString str;
266 str += data->m_Ref;
267 str += _T("\n");
268 str += data->m_RefAction;
269 str += _T("\n");
270 str += data->m_CommitHash.ToString();
271 str += _T("\n");
272 str += data->GetSubject();
273 str += _T("\n");
274 str += data->GetBody();
275 str += _T("\n");
277 if (!bCaseSensitive)
278 str.MakeLower();
280 if (str.Find(findString) >= 0)
281 bFound = true;
283 ++i;
284 if(!bFound && i >= m_RefList.m_arShownList.GetCount())
285 i=0;
286 } while (i != m_nSearchLine && (!bFound));
288 if (bFound)
290 m_RefList.SetHotItem(i - 1);
291 m_RefList.EnsureVisible(i - 1, FALSE);
292 m_nSearchLine = i;
294 else
295 MessageBox(_T("\"") + findString + _T("\" ") + CString(MAKEINTRESOURCE(IDS_NOTFOUND)), _T("TortoiseGit"), MB_ICONINFORMATION);
298 return 0;