Dropped auto_buffer
[TortoiseGit.git] / src / TortoiseProc / RepositoryBrowser.cpp
blob2d96e009c910feaddfbbbd583bcff49fcbd44921
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2012 - TortoiseGit
4 // Copyright (C) 2012 Sven Strickroth <email@cs-ware.de>
5 // Copyright (C) 2003-2012 - TortoiseSVN
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 // RepositoryBrowser.cpp : implementation file
24 #include "stdafx.h"
25 #include "TortoiseProc.h"
26 #include "RepositoryBrowser.h"
27 #include "LogDlg.h"
28 #include "AppUtils.h"
29 #include "IconMenu.h"
30 #include "UnicodeUtils.h"
31 #include "SysImageList.h"
32 #include <sys/stat.h>
33 #include "SysInfo.h"
34 #include "registry.h"
35 #include "PathUtils.h"
36 #include "StringUtils.h"
37 #include "GitDiff.h"
39 void SetSortArrowA(CListCtrl * control, int nColumn, bool bAscending)
41 if (control == NULL)
42 return;
44 // set the sort arrow
45 CHeaderCtrl * pHeader = control->GetHeaderCtrl();
46 HDITEM HeaderItem = {0};
47 HeaderItem.mask = HDI_FORMAT;
48 for (int i = 0; i < pHeader->GetItemCount(); ++i)
50 pHeader->GetItem(i, &HeaderItem);
51 HeaderItem.fmt &= ~(HDF_SORTDOWN | HDF_SORTUP);
52 pHeader->SetItem(i, &HeaderItem);
54 if (nColumn >= 0)
56 pHeader->GetItem(nColumn, &HeaderItem);
57 HeaderItem.fmt |= (bAscending ? HDF_SORTUP : HDF_SORTDOWN);
58 pHeader->SetItem(nColumn, &HeaderItem);
62 class CRepoListCompareFunc
64 public:
65 CRepoListCompareFunc(CListCtrl* pList, int col, bool desc)
66 : m_col(col)
67 , m_desc(desc)
68 , m_pList(pList)
71 static int CALLBACK StaticCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
73 return ((CRepoListCompareFunc *) lParamSort)->Compare(lParam1, lParam2);
76 int Compare(LPARAM lParam1, LPARAM lParam2)
78 CShadowFilesTree * pLeft = (CShadowFilesTree *)m_pList->GetItemData((int)lParam1);
79 CShadowFilesTree * pRight = (CShadowFilesTree *)m_pList->GetItemData((int)lParam2);
81 int result = 0;
82 switch(m_col)
84 case CRepositoryBrowser::eCol_Name:
85 result = SortStrCmp(pLeft->m_sName, pRight->m_sName);
86 if (result != 0)
87 break;
88 case CRepositoryBrowser::eCol_Extension:
89 result = m_pList->GetItemText(static_cast<int>(lParam1), 1).CompareNoCase(m_pList->GetItemText(static_cast<int>(lParam2), 1));
90 if (result == 0) // if extensions are the same, use the filename to sort
91 result = SortStrCmp(pRight->m_sName, pRight->m_sName);
92 if (result != 0)
93 break;
94 case CRepositoryBrowser::eCol_FileSize:
95 if (pLeft->m_iSize > pRight->m_iSize)
96 result = 1;
97 else if (pLeft->m_iSize < pRight->m_iSize)
98 result = -1;
99 else // fallback
100 result = SortStrCmp(pLeft->m_sName, pRight->m_sName);
103 if (m_desc)
104 result = -result;
106 if (pLeft->m_bFolder != pRight->m_bFolder)
108 if (pRight->m_bFolder)
109 result = 1;
110 else
111 result = -1;
114 return result;
116 int SortStrCmp(CString &left, CString &right)
118 if (CRepositoryBrowser::s_bSortLogical)
119 return StrCmpLogicalW(left, right);
120 return StrCmpI(left, right);
123 int m_col;
124 bool m_desc;
125 CListCtrl* m_pList;
128 // CRepositoryBrowser dialog
130 bool CRepositoryBrowser::s_bSortLogical = true;
132 IMPLEMENT_DYNAMIC(CRepositoryBrowser, CResizableStandAloneDialog)
134 CRepositoryBrowser::CRepositoryBrowser(CString rev, CWnd* pParent /*=NULL*/)
135 : CResizableStandAloneDialog(CRepositoryBrowser::IDD, pParent)
136 , m_currSortCol(0)
137 , m_currSortDesc(false)
138 , m_sRevision(rev)
139 , m_bHasWC(true)
143 CRepositoryBrowser::~CRepositoryBrowser()
147 void CRepositoryBrowser::DoDataExchange(CDataExchange* pDX)
149 CDialog::DoDataExchange(pDX);
150 DDX_Control(pDX, IDC_REPOTREE, m_RepoTree);
151 DDX_Control(pDX, IDC_REPOLIST, m_RepoList);
155 BEGIN_MESSAGE_MAP(CRepositoryBrowser, CResizableStandAloneDialog)
156 ON_NOTIFY(TVN_SELCHANGED, IDC_REPOTREE, &CRepositoryBrowser::OnTvnSelchangedRepoTree)
157 ON_WM_CONTEXTMENU()
158 ON_NOTIFY(LVN_COLUMNCLICK, IDC_REPOLIST, &CRepositoryBrowser::OnLvnColumnclickRepoList)
159 ON_NOTIFY(NM_DBLCLK, IDC_REPOLIST, &CRepositoryBrowser::OnNMDblclk_RepoList)
160 ON_BN_CLICKED(IDC_BUTTON_REVISION, &CRepositoryBrowser::OnBnClickedButtonRevision)
161 ON_WM_SETCURSOR()
162 ON_WM_MOUSEMOVE()
163 ON_WM_LBUTTONDOWN()
164 ON_WM_LBUTTONUP()
165 END_MESSAGE_MAP()
168 // CRepositoryBrowser message handlers
170 BOOL CRepositoryBrowser::OnInitDialog()
172 CResizableStandAloneDialog::OnInitDialog();
173 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
175 AddAnchor(IDC_STATIC_REPOURL, TOP_LEFT);
176 AddAnchor(IDC_REPOBROWSER_URL, TOP_LEFT, TOP_RIGHT);
177 AddAnchor(IDC_STATIC_REF, TOP_RIGHT);
178 AddAnchor(IDC_BUTTON_REVISION, TOP_RIGHT);
179 AddAnchor(IDC_REPOTREE, TOP_LEFT, BOTTOM_LEFT);
180 AddAnchor(IDC_REPOLIST, TOP_LEFT, BOTTOM_RIGHT);
181 AddAnchor(IDHELP, BOTTOM_RIGHT);
182 AddAnchor(IDOK, BOTTOM_RIGHT);
183 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
185 CRepositoryBrowser::s_bSortLogical = !CRegDWORD(L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer\\NoStrCmpLogical", 0, false, HKEY_CURRENT_USER);
186 if (CRepositoryBrowser::s_bSortLogical)
187 CRepositoryBrowser::s_bSortLogical = !CRegDWORD(L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer\\NoStrCmpLogical", 0, false, HKEY_LOCAL_MACHINE);
189 CString temp;
190 temp.LoadString(IDS_STATUSLIST_COLFILENAME);
191 m_RepoList.InsertColumn(eCol_Name, temp, 0, 150);
192 temp.LoadString(IDS_STATUSLIST_COLEXT);
193 m_RepoList.InsertColumn(eCol_Extension, temp, 0, 100);
194 temp.LoadString(IDS_LOG_SIZE);
195 m_RepoList.InsertColumn(eCol_FileSize, temp, 0, 100);
197 // set up the list control
198 // set the extended style of the list control
199 // the style LVS_EX_FULLROWSELECT interferes with the background watermark image but it's more important to be able to select in the whole row.
200 CRegDWORD regFullRowSelect(_T("Software\\TortoiseGit\\FullRowSelect"), TRUE);
201 DWORD exStyle = LVS_EX_HEADERDRAGDROP | LVS_EX_DOUBLEBUFFER | LVS_EX_INFOTIP | LVS_EX_SUBITEMIMAGES;
202 if (DWORD(regFullRowSelect))
203 exStyle |= LVS_EX_FULLROWSELECT;
204 m_RepoList.SetExtendedStyle(exStyle);
205 m_RepoList.SetImageList(&SYS_IMAGE_LIST(), LVSIL_SMALL);
206 CAppUtils::SetListCtrlBackgroundImage(m_RepoList.GetSafeHwnd(), IDI_REPOBROWSER_BKG);
208 m_RepoTree.SetImageList(&SYS_IMAGE_LIST(), TVSIL_NORMAL);
209 if (SysInfo::Instance().IsVistaOrLater())
211 DWORD exStyle = TVS_EX_FADEINOUTEXPANDOS | TVS_EX_AUTOHSCROLL | TVS_EX_DOUBLEBUFFER;
212 m_RepoTree.SetExtendedStyle(exStyle, exStyle);
215 SetWindowTheme(m_RepoTree.GetSafeHwnd(), L"Explorer", NULL);
216 SetWindowTheme(m_RepoList.GetSafeHwnd(), L"Explorer", NULL);
218 m_nIconFolder = SYS_IMAGE_LIST().GetDirIconIndex();
219 m_nOpenIconFolder = SYS_IMAGE_LIST().GetDirOpenIconIndex();
221 EnableSaveRestore(L"Reposbrowser");
223 DWORD xPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RepobrowserDivider"), 0);
224 if (xPos == 0)
226 RECT rc;
227 GetDlgItem(IDC_REPOTREE)->GetClientRect(&rc);
228 xPos = rc.right - rc.left;
230 bDragMode = false;
231 HandleDividerMove(CPoint(xPos + 20, 10), false);
233 CString sWindowTitle;
234 GetWindowText(sWindowTitle);
235 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
237 m_bHasWC = !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir);
239 Refresh();
241 m_RepoList.SetFocus();
243 return FALSE;
246 void CRepositoryBrowser::OnOK()
248 SaveDividerPosition();
249 CResizableStandAloneDialog::OnOK();
252 void CRepositoryBrowser::OnCancel()
254 SaveDividerPosition();
255 CResizableStandAloneDialog::OnCancel();
258 void CRepositoryBrowser::OnNMDblclk_RepoList(NMHDR *pNMHDR, LRESULT *pResult)
260 UNREFERENCED_PARAMETER(pNMHDR);
261 *pResult = 0;
263 LPNMITEMACTIVATE pNmItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
264 if (pNmItemActivate->iItem < 0)
265 return;
267 CShadowFilesTree * pItem = (CShadowFilesTree *)m_RepoList.GetItemData(pNmItemActivate->iItem);
268 if (pItem == NULL)
269 return;
271 if (!pItem->m_bFolder)
273 OpenFile(pItem->GetFullName(), OPEN);
274 return;
276 else
278 FillListCtrlForShadowTree(pItem);
279 m_RepoTree.SelectItem(pItem->m_hTree);
283 void CRepositoryBrowser::Refresh()
285 m_RepoTree.DeleteAllItems();
286 m_RepoList.DeleteAllItems();
287 m_TreeRoot.m_ShadowTree.clear();
288 m_TreeRoot.m_sName = "";
289 m_TreeRoot.m_bFolder = true;
291 TVINSERTSTRUCT tvinsert = {0};
292 tvinsert.hParent = TVI_ROOT;
293 tvinsert.hInsertAfter = TVI_ROOT;
294 tvinsert.itemex.mask = TVIF_DI_SETITEM | TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE;
295 tvinsert.itemex.pszText = L"/";
296 tvinsert.itemex.lParam = (LPARAM)&m_TreeRoot;
297 tvinsert.itemex.iImage = m_nIconFolder;
298 tvinsert.itemex.iSelectedImage = m_nOpenIconFolder;
299 m_TreeRoot.m_hTree= m_RepoTree.InsertItem(&tvinsert);
301 ReadTree(&m_TreeRoot);
302 m_RepoTree.Expand(m_TreeRoot.m_hTree, TVE_EXPAND);
303 FillListCtrlForShadowTree(&m_TreeRoot);
304 m_RepoTree.SelectItem(m_TreeRoot.m_hTree);
307 int CRepositoryBrowser::ReadTreeRecursive(git_repository &repo, git_tree * tree, CShadowFilesTree * treeroot)
309 size_t count = git_tree_entrycount(tree);
311 for (int i = 0; i < count; i++)
313 const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
314 if (entry == NULL)
315 continue;
316 int mode = git_tree_entry_filemode(entry);
318 CString base = CUnicodeUtils::GetUnicode(git_tree_entry_name(entry), CP_UTF8);
320 git_object *object = NULL;
321 git_tree_entry_to_object(&object, &repo, entry);
322 if (object == NULL)
323 continue;
325 CShadowFilesTree * pNextTree = &treeroot->m_ShadowTree[base];
326 pNextTree->m_sName = base;
327 pNextTree->m_pParent = treeroot;
329 if (mode & S_IFDIR)
331 pNextTree->m_bFolder = true;
333 TVINSERTSTRUCT tvinsert = {0};
334 tvinsert.hParent = treeroot->m_hTree;
335 tvinsert.hInsertAfter = TVI_SORT;
336 tvinsert.itemex.mask = TVIF_DI_SETITEM | TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE;
337 tvinsert.itemex.pszText = base.GetBuffer(base.GetLength());
338 tvinsert.itemex.lParam = (LPARAM)pNextTree;
339 tvinsert.itemex.iImage = m_nIconFolder;
340 tvinsert.itemex.iSelectedImage = m_nOpenIconFolder;
341 pNextTree->m_hTree = m_RepoTree.InsertItem(&tvinsert);
342 base.ReleaseBuffer();
344 ReadTreeRecursive(repo, (git_tree*)object, pNextTree);
346 else
348 const git_oid * oid = git_object_id(object);
350 git_blob * blob;
351 git_blob_lookup(&blob, &repo, oid);
352 if (blob == NULL)
353 continue;
355 pNextTree->m_iSize = git_blob_rawsize(blob);
356 git_blob_free(blob);
359 git_object_free(object);
362 return 0;
365 int CRepositoryBrowser::ReadTree(CShadowFilesTree * treeroot)
367 CStringA gitdir = CUnicodeUtils::GetMulti(g_Git.m_CurrentDir, CP_UTF8);
368 git_repository *repository = NULL;
369 git_commit *commit = NULL;
370 git_tree * tree = NULL;
371 int ret = 0;
374 ret = git_repository_open(&repository, gitdir.GetBuffer());
375 if (ret)
377 const git_error * err = giterr_last();
378 MessageBox(_T("Could not open repository.\nlibgit2 reports: ") + CString(err->message), _T("TortoiseGit"), MB_ICONERROR);
379 break;
382 CGitHash hash = g_Git.GetHash(m_sRevision);
383 ret = git_commit_lookup(&commit, repository, (git_oid *) hash.m_hash);
384 if (ret)
386 const git_error * err = giterr_last();
387 MessageBox(_T("Could not lookup commit.\nlibgit2 reports: ") + CString(err->message), _T("TortoiseGit"), MB_ICONERROR);
388 break;
391 ret = git_commit_tree(&tree, commit);
392 if (ret)
394 const git_error * err = giterr_last();
395 MessageBox(_T("Could get tree of commit.\nlibgit2 reports: ") + CString(err->message), _T("TortoiseGit"), MB_ICONERROR);
396 break;
399 ReadTreeRecursive(*repository, tree, treeroot);
401 // try to resolve hash to a branch name
402 if (m_sRevision == hash.ToString())
404 MAP_HASH_NAME map;
405 g_Git.GetMapHashToFriendName(map);
406 if (!map[hash].empty())
407 m_sRevision = map[hash].at(0);
409 this->GetDlgItem(IDC_BUTTON_REVISION)->SetWindowText(m_sRevision);
410 } while(0);
412 if (tree)
413 git_tree_free(tree);
415 if (commit)
416 git_commit_free(commit);
418 if (repository)
419 git_repository_free(repository);
421 return ret;
424 void CRepositoryBrowser::OnTvnSelchangedRepoTree(NMHDR *pNMHDR, LRESULT *pResult)
426 LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
427 *pResult = 0;
429 FillListCtrlForTreeNode(pNMTreeView->itemNew.hItem);
432 void CRepositoryBrowser::FillListCtrlForTreeNode(HTREEITEM treeNode)
434 m_RepoList.DeleteAllItems();
436 CShadowFilesTree* pTree = (CShadowFilesTree*)(m_RepoTree.GetItemData(treeNode));
437 if (pTree == NULL)
439 ASSERT(FALSE);
440 return;
443 CString url = _T("/") + pTree->GetFullName();
444 GetDlgItem(IDC_REPOBROWSER_URL)->SetWindowText(url);
446 FillListCtrlForShadowTree(pTree);
449 void CRepositoryBrowser::FillListCtrlForShadowTree(CShadowFilesTree* pTree)
451 for (TShadowFilesTreeMap::iterator itShadowTree = pTree->m_ShadowTree.begin(); itShadowTree != pTree->m_ShadowTree.end(); ++itShadowTree)
453 int icon = m_nIconFolder;
454 if (!(*itShadowTree).second.m_bFolder)
455 icon = SYS_IMAGE_LIST().GetFileIconIndex((*itShadowTree).second.m_sName);
457 int indexItem = m_RepoList.InsertItem(m_RepoList.GetItemCount(), (*itShadowTree).second.m_sName, icon);
459 m_RepoList.SetItemData(indexItem, (DWORD_PTR)&(*itShadowTree).second);
460 if (!(*itShadowTree).second.m_bFolder)
462 CString temp;
464 temp = CPathUtils::GetFileExtFromPath((*itShadowTree).second.m_sName);
465 m_RepoList.SetItemText(indexItem, eCol_Extension, temp);
467 StrFormatByteSize((*itShadowTree).second.m_iSize, temp.GetBuffer(20), 20);
468 temp.ReleaseBuffer();
469 m_RepoList.SetItemText(indexItem, eCol_FileSize, temp);
473 CRepoListCompareFunc compareFunc(&m_RepoList, m_currSortCol, m_currSortDesc);
474 m_RepoList.SortItemsEx(&CRepoListCompareFunc::StaticCompare, (DWORD_PTR)&compareFunc);
476 SetSortArrowA(&m_RepoList, m_currSortCol, !m_currSortDesc);
479 void CRepositoryBrowser::OnContextMenu(CWnd* pWndFrom, CPoint point)
481 if (pWndFrom == &m_RepoList)
482 OnContextMenu_RepoList(point);
483 else if (pWndFrom == &m_RepoTree)
484 OnContextMenu_RepoTree(point);
487 void CRepositoryBrowser::OnContextMenu_RepoTree(CPoint point)
489 CPoint clientPoint = point;
490 m_RepoTree.ScreenToClient(&clientPoint);
492 HTREEITEM hTreeItem = m_RepoTree.HitTest(clientPoint);
493 if (hTreeItem == NULL)
494 return;
496 TShadowFilesTreeList selectedLeafs;
497 selectedLeafs.push_back((CShadowFilesTree *)m_RepoTree.GetItemData(hTreeItem));
499 ShowContextMenu(point, selectedLeafs, ONLY_FOLDERS);
502 void CRepositoryBrowser::OnContextMenu_RepoList(CPoint point)
504 TShadowFilesTreeList selectedLeafs;
505 selectedLeafs.reserve(m_RepoList.GetSelectedCount());
507 bool folderSelected = false;
508 bool filesSelected = false;
510 POSITION pos = m_RepoList.GetFirstSelectedItemPosition();
511 while (pos)
513 CShadowFilesTree * item = (CShadowFilesTree *)m_RepoList.GetItemData(m_RepoList.GetNextSelectedItem(pos));
514 if (item->m_bFolder)
515 folderSelected = true;
516 else
517 filesSelected = true;
518 selectedLeafs.push_back(item);
521 eSelectionType selType = ONLY_FILES;
522 if (folderSelected && filesSelected)
523 selType = MIXED;
524 else if (folderSelected)
525 selType = ONLY_FOLDERS;
527 ShowContextMenu(point, selectedLeafs, selType);
530 void CRepositoryBrowser::ShowContextMenu(CPoint point, TShadowFilesTreeList &selectedLeafs, eSelectionType selType)
532 CIconMenu popupMenu;
533 popupMenu.CreatePopupMenu();
535 bool bAddSeparator = false;
537 if (selectedLeafs.size() == 1)
539 popupMenu.AppendMenuIcon(eCmd_Open, IDS_REPOBROWSE_OPEN, IDI_OPEN);
540 popupMenu.SetDefaultItem(eCmd_Open, FALSE);
541 if (selType == ONLY_FILES)
543 popupMenu.AppendMenuIcon(eCmd_OpenWith, IDS_LOG_POPUP_OPENWITH, IDI_OPEN);
544 popupMenu.AppendMenuIcon(eCmd_OpenWithAlternativeEditor, IDS_LOG_POPUP_VIEWREV);
547 popupMenu.AppendMenu(MF_SEPARATOR);
549 if (m_bHasWC && selType == ONLY_FILES)
551 popupMenu.AppendMenuIcon(eCmd_CompareWC, IDS_LOG_POPUP_COMPARE, IDI_DIFF);
552 bAddSeparator = true;
555 if (bAddSeparator)
556 popupMenu.AppendMenu(MF_SEPARATOR);
557 bAddSeparator = false;
559 CString temp;
560 temp.LoadString(IDS_MENULOG);
561 popupMenu.AppendMenuIcon(eCmd_ViewLog, temp, IDI_LOG);
563 if (selType == ONLY_FILES)
565 if (m_bHasWC)
566 popupMenu.AppendMenuIcon(eCmd_Blame, IDS_LOG_POPUP_BLAME, IDI_BLAME);
568 popupMenu.AppendMenu(MF_SEPARATOR);
569 temp.LoadString(IDS_LOG_POPUP_SAVE);
570 popupMenu.AppendMenuIcon(eCmd_SaveAs, temp, IDI_SAVEAS);
573 bAddSeparator = true;
576 if (selType == ONLY_FILES && m_bHasWC)
578 popupMenu.AppendMenuIcon(eCmd_Revert, IDS_LOG_POPUP_REVERTTOREV, IDI_REVERT);
579 bAddSeparator = true;
582 if (bAddSeparator)
583 popupMenu.AppendMenu(MF_SEPARATOR);
584 bAddSeparator = false;
586 popupMenu.AppendMenuIcon(eCmd_CopyPath, IDS_STATUSLIST_CONTEXT_COPY, IDI_COPYCLIP);
588 eCmd cmd = (eCmd)popupMenu.TrackPopupMenuEx(TPM_LEFTALIGN|TPM_RETURNCMD, point.x, point.y, this, 0);
589 switch(cmd)
591 case eCmd_ViewLog:
593 CString sCmd;
594 sCmd.Format(_T("/command:log /path:\"%s\\%s\""), g_Git.m_CurrentDir, selectedLeafs.at(0)->GetFullName());
595 CAppUtils::RunTortoiseGitProc(sCmd);
597 break;
598 case eCmd_Blame:
600 CAppUtils::LaunchTortoiseBlame(g_Git.m_CurrentDir + _T("\\") + selectedLeafs.at(0)->GetFullName(), m_sRevision);
602 break;
603 case eCmd_Open:
604 if (selectedLeafs.at(0)->m_bFolder)
606 FillListCtrlForTreeNode(selectedLeafs.at(0)->m_hTree);
607 m_RepoTree.SelectItem(selectedLeafs.at(0)->m_hTree);
608 return;
610 OpenFile(selectedLeafs.at(0)->GetFullName(), OPEN);
611 break;
612 case eCmd_OpenWith:
613 OpenFile(selectedLeafs.at(0)->GetFullName(), OPEN_WITH);
614 break;
615 case eCmd_OpenWithAlternativeEditor:
616 OpenFile(selectedLeafs.at(0)->GetFullName(), ALTERNATIVEEDITOR);
617 break;
618 case eCmd_CompareWC:
620 CTGitPath file(selectedLeafs.at(0)->GetFullName());
621 CGitDiff::Diff(&file, &file, GIT_REV_ZERO, m_sRevision);
623 break;
624 case eCmd_Revert:
626 int count = 0;
627 for (TShadowFilesTreeList::iterator itShadowTree = selectedLeafs.begin(); itShadowTree != selectedLeafs.end(); ++itShadowTree)
629 if (RevertItemToVersion((*itShadowTree)->GetFullName()))
630 count++;
631 else
632 break;
634 CString msg;
635 msg.Format(IDS_STATUSLIST_FILESREVERTED, count, m_sRevision);
636 MessageBox(msg, _T("TortoiseGit"), MB_OK);
638 break;
639 case eCmd_SaveAs:
640 FileSaveAs(selectedLeafs.at(0)->GetFullName());
641 break;
642 case eCmd_CopyPath:
644 CString sClipboard;
645 for (TShadowFilesTreeList::iterator itShadowTree = selectedLeafs.begin(); itShadowTree != selectedLeafs.end(); ++itShadowTree)
647 sClipboard += (*itShadowTree)->m_sName + _T("\r\n");
649 CStringUtils::WriteAsciiStringToClipboard(sClipboard);
651 break;
655 BOOL CRepositoryBrowser::PreTranslateMessage(MSG* pMsg)
657 if (pMsg->message == WM_KEYDOWN)
659 switch (pMsg->wParam)
661 case VK_F5:
663 Refresh();
665 break;
669 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
672 void CRepositoryBrowser::OnLvnColumnclickRepoList(NMHDR *pNMHDR, LRESULT *pResult)
674 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
675 if (pResult)
676 *pResult = 0;
678 if (m_currSortCol == pNMLV->iSubItem)
679 m_currSortDesc = !m_currSortDesc;
680 else
682 m_currSortCol = pNMLV->iSubItem;
683 m_currSortDesc = false;
686 CRepoListCompareFunc compareFunc(&m_RepoList, m_currSortCol, m_currSortDesc);
687 m_RepoList.SortItemsEx(&CRepoListCompareFunc::StaticCompare, (DWORD_PTR)&compareFunc);
689 SetSortArrowA(&m_RepoList, m_currSortCol, !m_currSortDesc);
692 void CRepositoryBrowser::OnBnClickedButtonRevision()
694 // use the git log to allow selection of a version
695 CLogDlg dlg;
696 // tell the dialog to use mode for selecting revisions
697 dlg.SetSelect(true);
698 // only one revision must be selected however
699 dlg.SingleSelection(true);
700 if (dlg.DoModal() == IDOK)
702 // get selected hash if any
703 m_sRevision = dlg.GetSelectedHash();
704 Refresh();
708 void CRepositoryBrowser::SaveDividerPosition()
710 RECT rc;
711 GetDlgItem(IDC_REPOTREE)->GetClientRect(&rc);
712 CRegDWORD xPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RepobrowserDivider"));
713 xPos = rc.right - rc.left;
716 void CRepositoryBrowser::HandleDividerMove(CPoint point, bool bDraw)
718 RECT rect, tree, list, treelist, treelistclient;
720 // create an union of the tree and list control rectangle
721 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
722 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
723 UnionRect(&treelist, &tree, &list);
724 treelistclient = treelist;
725 ScreenToClient(&treelistclient);
727 ClientToScreen(&point);
728 GetClientRect(&rect);
729 ClientToScreen(&rect);
731 CPoint point2 = point;
732 if (point2.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
733 point2.x = treelist.left + REPOBROWSER_CTRL_MIN_WIDTH;
734 if (point2.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
735 point2.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
737 point.x -= rect.left;
738 point.y -= treelist.top;
740 OffsetRect(&treelist, -treelist.left, -treelist.top);
742 if (point.x < treelist.left+REPOBROWSER_CTRL_MIN_WIDTH)
743 point.x = treelist.left+REPOBROWSER_CTRL_MIN_WIDTH;
744 if (point.x > treelist.right-REPOBROWSER_CTRL_MIN_WIDTH)
745 point.x = treelist.right-REPOBROWSER_CTRL_MIN_WIDTH;
747 if (bDraw)
749 CDC * pDC = GetDC();
750 DrawXorBar(pDC, oldx + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
751 ReleaseDC(pDC);
754 oldx = point.x;
755 oldy = point.y;
757 //position the child controls
758 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&treelist);
759 treelist.right = point2.x - 2;
760 ScreenToClient(&treelist);
761 RemoveAnchor(IDC_REPOTREE);
762 GetDlgItem(IDC_REPOTREE)->MoveWindow(&treelist);
763 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&treelist);
764 treelist.left = point2.x + 2;
765 ScreenToClient(&treelist);
766 RemoveAnchor(IDC_REPOLIST);
767 GetDlgItem(IDC_REPOLIST)->MoveWindow(&treelist);
769 AddAnchor(IDC_REPOTREE, TOP_LEFT, BOTTOM_LEFT);
770 AddAnchor(IDC_REPOLIST, TOP_LEFT, BOTTOM_RIGHT);
773 void CRepositoryBrowser::OnMouseMove(UINT nFlags, CPoint point)
775 if (bDragMode == FALSE)
776 return;
778 RECT rect, tree, list, treelist, treelistclient;
779 // create an union of the tree and list control rectangle
780 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
781 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
782 UnionRect(&treelist, &tree, &list);
783 treelistclient = treelist;
784 ScreenToClient(&treelistclient);
786 //convert the mouse coordinates relative to the top-left of
787 //the window
788 ClientToScreen(&point);
789 GetClientRect(&rect);
790 ClientToScreen(&rect);
791 point.x -= rect.left;
792 point.y -= treelist.top;
794 //same for the window coordinates - make them relative to 0,0
795 OffsetRect(&treelist, -treelist.left, -treelist.top);
797 if (point.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
798 point.x = treelist.left + REPOBROWSER_CTRL_MIN_WIDTH;
799 if (point.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
800 point.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
802 if ((nFlags & MK_LBUTTON) && (point.x != oldx))
804 CDC * pDC = GetDC();
806 if (pDC)
808 DrawXorBar(pDC, oldx + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
809 DrawXorBar(pDC, point.x + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
811 ReleaseDC(pDC);
814 oldx = point.x;
815 oldy = point.y;
818 CStandAloneDialogTmpl<CResizableDialog>::OnMouseMove(nFlags, point);
821 void CRepositoryBrowser::OnLButtonDown(UINT nFlags, CPoint point)
823 RECT rect, tree, list, treelist, treelistclient;
825 // create an union of the tree and list control rectangle
826 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
827 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
828 UnionRect(&treelist, &tree, &list);
829 treelistclient = treelist;
830 ScreenToClient(&treelistclient);
832 //convert the mouse coordinates relative to the top-left of
833 //the window
834 ClientToScreen(&point);
835 GetClientRect(&rect);
836 ClientToScreen(&rect);
837 point.x -= rect.left;
838 point.y -= treelist.top;
840 //same for the window coordinates - make them relative to 0,0
841 OffsetRect(&treelist, -treelist.left, -treelist.top);
843 if (point.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
844 return CStandAloneDialogTmpl < CResizableDialog>::OnLButtonDown(nFlags, point);
845 if (point.x > treelist.right - 3)
846 return CStandAloneDialogTmpl < CResizableDialog>::OnLButtonDown(nFlags, point);
847 if (point.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
848 point.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
850 if ((point.y < treelist.top + 3) || (point.y > treelist.bottom - 3))
851 return CStandAloneDialogTmpl<CResizableDialog>::OnLButtonDown(nFlags, point);
853 bDragMode = true;
855 SetCapture();
857 CDC * pDC = GetDC();
858 DrawXorBar(pDC, point.x + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
859 ReleaseDC(pDC);
861 oldx = point.x;
862 oldy = point.y;
864 CStandAloneDialogTmpl<CResizableDialog>::OnLButtonDown(nFlags, point);
867 void CRepositoryBrowser::OnLButtonUp(UINT nFlags, CPoint point)
869 if (bDragMode == FALSE)
870 return;
872 HandleDividerMove(point, true);
874 bDragMode = false;
875 ReleaseCapture();
877 CStandAloneDialogTmpl<CResizableDialog>::OnLButtonUp(nFlags, point);
880 void CRepositoryBrowser::OnCaptureChanged(CWnd *pWnd)
882 bDragMode = false;
884 __super::OnCaptureChanged(pWnd);
887 void CRepositoryBrowser::DrawXorBar(CDC * pDC, int x1, int y1, int width, int height)
889 static WORD _dotPatternBmp[8] =
891 0x0055, 0x00aa, 0x0055, 0x00aa,
892 0x0055, 0x00aa, 0x0055, 0x00aa
895 HBITMAP hbm;
896 HBRUSH hbr, hbrushOld;
898 hbm = CreateBitmap(8, 8, 1, 1, _dotPatternBmp);
899 hbr = CreatePatternBrush(hbm);
901 pDC->SetBrushOrg(x1, y1);
902 hbrushOld = (HBRUSH)pDC->SelectObject(hbr);
904 PatBlt(pDC->GetSafeHdc(), x1, y1, width, height, PATINVERT);
906 pDC->SelectObject(hbrushOld);
908 DeleteObject(hbr);
909 DeleteObject(hbm);
912 BOOL CRepositoryBrowser::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
914 if (pWnd == this)
916 RECT rect;
917 POINT pt;
918 GetClientRect(&rect);
919 GetCursorPos(&pt);
920 ScreenToClient(&pt);
921 if (PtInRect(&rect, pt))
923 ClientToScreen(&pt);
924 // are we right of the tree control?
925 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&rect);
926 if ((pt.x > rect.right) && (pt.y >= rect.top + 3) && (pt.y <= rect.bottom - 3))
928 // but left of the list control?
929 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&rect);
930 if (pt.x < rect.left)
932 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE));
933 SetCursor(hCur);
934 return TRUE;
939 return CStandAloneDialogTmpl<CResizableDialog>::OnSetCursor(pWnd, nHitTest, message);
942 void CRepositoryBrowser::FileSaveAs(const CString path)
944 CTGitPath gitPath(path);
946 CString filename;
947 filename.Format(_T("%s-%s%s"), gitPath.GetBaseFilename(), CGitHash(m_sRevision).ToString().Left(g_Git.GetShortHASHLength()), gitPath.GetFileExtension());
948 CFileDialog dlg(FALSE, NULL, filename, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, NULL);
950 CString cmd, out;
951 INT_PTR ret = dlg.DoModal();
952 SetCurrentDirectory(g_Git.m_CurrentDir);
953 if (ret == IDOK)
955 filename = dlg.GetPathName();
956 if (g_Git.GetOneFile(m_sRevision, gitPath, filename))
958 out.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED, gitPath.GetGitPathString(), m_sRevision, filename);
959 MessageBox(out, _T("TortoiseGit"), MB_OK);
960 return;
965 void CRepositoryBrowser::OpenFile(const CString path, eOpenType mode)
967 CTGitPath gitPath(path);
969 CString temppath;
970 CString file;
971 GetTempPath(temppath);
972 file.Format(_T("%s%s_%s%s"), temppath, gitPath.GetBaseFilename(), CGitHash(m_sRevision).ToString().Left(g_Git.GetShortHASHLength()), gitPath.GetFileExtension());
974 CString out;
975 if(g_Git.GetOneFile(m_sRevision, gitPath, file))
977 out.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED, gitPath.GetGitPathString(), m_sRevision, file);
978 MessageBox(out, _T("TortoiseGit"), MB_OK);
979 return;
982 if (mode == ALTERNATIVEEDITOR)
984 CAppUtils::LaunchAlternativeEditor(file);
985 return;
987 else if (mode == OPEN)
989 int ret = HINSTANCE_ERROR;
990 ret = (int)ShellExecute(this->m_hWnd, NULL, file, NULL, NULL, SW_SHOW);
992 if (ret > HINSTANCE_ERROR)
993 return;
996 CString cmd = _T("RUNDLL32 Shell32,OpenAs_RunDLL ") + file;
997 CAppUtils::LaunchApplication(cmd, NULL, false);
999 bool CRepositoryBrowser::RevertItemToVersion(const CString &path)
1001 CString cmd, out;
1002 cmd.Format(_T("git.exe checkout %s -- \"%s\""), m_sRevision, path);
1003 if (g_Git.Run(cmd, &out, CP_UTF8))
1005 if (MessageBox(out, _T("TortoiseGit"), MB_ICONEXCLAMATION | MB_OKCANCEL) == IDCANCEL)
1006 return false;
1009 return true;