RepoBrowser: Report errors to user
[TortoiseGit.git] / src / TortoiseProc / RepositoryBrowser.cpp
blob2a1df28391db21fcd359c07e44e5a2f5d46e8ac2
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(lParam1);
79 CShadowFilesTree * pRight = (CShadowFilesTree *)m_pList->GetItemData(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_attributes(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 this->GetDlgItem(IDC_BUTTON_REVISION)->SetWindowText(m_sRevision);
402 } while(0);
404 if (tree)
405 git_tree_free(tree);
407 if (commit)
408 git_commit_free(commit);
410 if (repository)
411 git_repository_free(repository);
413 return ret;
416 void CRepositoryBrowser::OnTvnSelchangedRepoTree(NMHDR *pNMHDR, LRESULT *pResult)
418 LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
419 *pResult = 0;
421 FillListCtrlForTreeNode(pNMTreeView->itemNew.hItem);
424 void CRepositoryBrowser::FillListCtrlForTreeNode(HTREEITEM treeNode)
426 m_RepoList.DeleteAllItems();
428 CShadowFilesTree* pTree = (CShadowFilesTree*)(m_RepoTree.GetItemData(treeNode));
429 if (pTree == NULL)
431 ASSERT(FALSE);
432 return;
435 CString url = _T("/") + pTree->GetFullName();
436 GetDlgItem(IDC_REPOBROWSER_URL)->SetWindowText(url);
438 FillListCtrlForShadowTree(pTree);
441 void CRepositoryBrowser::FillListCtrlForShadowTree(CShadowFilesTree* pTree)
443 for (TShadowFilesTreeMap::iterator itShadowTree = pTree->m_ShadowTree.begin(); itShadowTree != pTree->m_ShadowTree.end(); ++itShadowTree)
445 int icon = m_nIconFolder;
446 if (!(*itShadowTree).second.m_bFolder)
447 icon = SYS_IMAGE_LIST().GetFileIconIndex((*itShadowTree).second.m_sName);
449 int indexItem = m_RepoList.InsertItem(m_RepoList.GetItemCount(), (*itShadowTree).second.m_sName, icon);
451 m_RepoList.SetItemData(indexItem, (DWORD_PTR)&(*itShadowTree).second);
452 if (!(*itShadowTree).second.m_bFolder)
454 CString temp;
456 temp = CPathUtils::GetFileExtFromPath((*itShadowTree).second.m_sName);
457 m_RepoList.SetItemText(indexItem, eCol_Extension, temp);
459 StrFormatByteSize((*itShadowTree).second.m_iSize, temp.GetBuffer(20), 20);
460 temp.ReleaseBuffer();
461 m_RepoList.SetItemText(indexItem, eCol_FileSize, temp);
465 CRepoListCompareFunc compareFunc(&m_RepoList, m_currSortCol, m_currSortDesc);
466 m_RepoList.SortItemsEx(&CRepoListCompareFunc::StaticCompare, (DWORD_PTR)&compareFunc);
468 SetSortArrowA(&m_RepoList, m_currSortCol, !m_currSortDesc);
471 void CRepositoryBrowser::OnContextMenu(CWnd* pWndFrom, CPoint point)
473 if (pWndFrom == &m_RepoList)
474 OnContextMenu_RepoList(point);
475 else if (pWndFrom == &m_RepoTree)
476 OnContextMenu_RepoTree(point);
479 void CRepositoryBrowser::OnContextMenu_RepoTree(CPoint point)
481 CPoint clientPoint = point;
482 m_RepoTree.ScreenToClient(&clientPoint);
484 HTREEITEM hTreeItem = m_RepoTree.HitTest(clientPoint);
485 if (hTreeItem == NULL)
486 return;
488 TShadowFilesTreeList selectedLeafs;
489 selectedLeafs.push_back((CShadowFilesTree *)m_RepoTree.GetItemData(hTreeItem));
491 ShowContextMenu(point, selectedLeafs, ONLY_FOLDERS);
494 void CRepositoryBrowser::OnContextMenu_RepoList(CPoint point)
496 TShadowFilesTreeList selectedLeafs;
497 selectedLeafs.reserve(m_RepoList.GetSelectedCount());
499 bool folderSelected = false;
500 bool filesSelected = false;
502 POSITION pos = m_RepoList.GetFirstSelectedItemPosition();
503 while (pos)
505 CShadowFilesTree * item = (CShadowFilesTree *)m_RepoList.GetItemData(m_RepoList.GetNextSelectedItem(pos));
506 if (item->m_bFolder)
507 folderSelected = true;
508 else
509 filesSelected = true;
510 selectedLeafs.push_back(item);
513 eSelectionType selType = ONLY_FILES;
514 if (folderSelected && filesSelected)
515 selType = MIXED;
516 else if (folderSelected)
517 selType = ONLY_FOLDERS;
519 ShowContextMenu(point, selectedLeafs, selType);
522 void CRepositoryBrowser::ShowContextMenu(CPoint point, TShadowFilesTreeList &selectedLeafs, eSelectionType selType)
524 CIconMenu popupMenu;
525 popupMenu.CreatePopupMenu();
527 bool bAddSeparator = false;
529 if (selectedLeafs.size() == 1)
531 popupMenu.AppendMenuIcon(eCmd_Open, IDS_REPOBROWSE_OPEN, IDI_OPEN);
532 popupMenu.SetDefaultItem(eCmd_Open, FALSE);
533 if (selType == ONLY_FILES)
535 popupMenu.AppendMenuIcon(eCmd_OpenWith, IDS_LOG_POPUP_OPENWITH, IDI_OPEN);
536 popupMenu.AppendMenuIcon(eCmd_OpenWithAlternativeEditor, IDS_LOG_POPUP_VIEWREV);
539 popupMenu.AppendMenu(MF_SEPARATOR);
541 if (m_bHasWC && selType == ONLY_FILES)
543 popupMenu.AppendMenuIcon(eCmd_CompareWC, IDS_LOG_POPUP_COMPARE, IDI_DIFF);
544 bAddSeparator = true;
547 if (bAddSeparator)
548 popupMenu.AppendMenu(MF_SEPARATOR);
549 bAddSeparator = false;
551 CString temp;
552 temp.LoadString(IDS_MENULOG);
553 popupMenu.AppendMenuIcon(eCmd_ViewLog, temp, IDI_LOG);
555 if (selType == ONLY_FILES)
557 if (m_bHasWC)
558 popupMenu.AppendMenuIcon(eCmd_Blame, IDS_LOG_POPUP_BLAME, IDI_BLAME);
560 popupMenu.AppendMenu(MF_SEPARATOR);
561 temp.LoadString(IDS_LOG_POPUP_SAVE);
562 popupMenu.AppendMenuIcon(eCmd_SaveAs, temp, IDI_SAVEAS);
565 bAddSeparator = true;
568 if (selType == ONLY_FILES && m_bHasWC)
570 popupMenu.AppendMenuIcon(eCmd_Revert, IDS_LOG_POPUP_REVERTTOREV, IDI_REVERT);
571 bAddSeparator = true;
574 if (bAddSeparator)
575 popupMenu.AppendMenu(MF_SEPARATOR);
576 bAddSeparator = false;
578 popupMenu.AppendMenuIcon(eCmd_CopyPath, IDS_STATUSLIST_CONTEXT_COPY, IDI_COPYCLIP);
580 eCmd cmd = (eCmd)popupMenu.TrackPopupMenuEx(TPM_LEFTALIGN|TPM_RETURNCMD, point.x, point.y, this, 0);
581 switch(cmd)
583 case eCmd_ViewLog:
585 CString sCmd;
586 sCmd.Format(_T("/command:log /path:\"%s\\%s\""), g_Git.m_CurrentDir, selectedLeafs.at(0)->GetFullName());
587 CAppUtils::RunTortoiseProc(sCmd);
589 break;
590 case eCmd_Blame:
592 CAppUtils::LaunchTortoiseBlame(g_Git.m_CurrentDir + _T("\\") + selectedLeafs.at(0)->GetFullName(), m_sRevision);
594 break;
595 case eCmd_Open:
596 if (selectedLeafs.at(0)->m_bFolder)
598 FillListCtrlForTreeNode(selectedLeafs.at(0)->m_hTree);
599 m_RepoTree.SelectItem(selectedLeafs.at(0)->m_hTree);
600 return;
602 OpenFile(selectedLeafs.at(0)->GetFullName(), OPEN);
603 break;
604 case eCmd_OpenWith:
605 OpenFile(selectedLeafs.at(0)->GetFullName(), OPEN_WITH);
606 break;
607 case eCmd_OpenWithAlternativeEditor:
608 OpenFile(selectedLeafs.at(0)->GetFullName(), ALTERNATIVEEDITOR);
609 break;
610 case eCmd_CompareWC:
612 CTGitPath file(selectedLeafs.at(0)->GetFullName());
613 CGitDiff::Diff(&file, &file, GIT_REV_ZERO, m_sRevision);
615 break;
616 case eCmd_Revert:
618 int count = 0;
619 for (TShadowFilesTreeList::iterator itShadowTree = selectedLeafs.begin(); itShadowTree != selectedLeafs.end(); ++itShadowTree)
621 if (RevertItemToVersion((*itShadowTree)->GetFullName()))
622 count++;
623 else
624 break;
626 CString msg;
627 msg.Format(IDS_STATUSLIST_FILESREVERTED, count, m_sRevision);
628 MessageBox(msg, _T("TortoiseGit"), MB_OK);
630 break;
631 case eCmd_SaveAs:
632 FileSaveAs(selectedLeafs.at(0)->GetFullName());
633 break;
634 case eCmd_CopyPath:
636 CString sClipboard;
637 for (TShadowFilesTreeList::iterator itShadowTree = selectedLeafs.begin(); itShadowTree != selectedLeafs.end(); ++itShadowTree)
639 sClipboard += (*itShadowTree)->m_sName + _T("\r\n");
641 CStringUtils::WriteAsciiStringToClipboard(sClipboard);
643 break;
647 BOOL CRepositoryBrowser::PreTranslateMessage(MSG* pMsg)
649 if (pMsg->message == WM_KEYDOWN)
651 switch (pMsg->wParam)
653 case VK_F5:
655 Refresh();
657 break;
661 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
664 void CRepositoryBrowser::OnLvnColumnclickRepoList(NMHDR *pNMHDR, LRESULT *pResult)
666 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
667 if (pResult)
668 *pResult = 0;
670 if (m_currSortCol == pNMLV->iSubItem)
671 m_currSortDesc = !m_currSortDesc;
672 else
674 m_currSortCol = pNMLV->iSubItem;
675 m_currSortDesc = false;
678 CRepoListCompareFunc compareFunc(&m_RepoList, m_currSortCol, m_currSortDesc);
679 m_RepoList.SortItemsEx(&CRepoListCompareFunc::StaticCompare, (DWORD_PTR)&compareFunc);
681 SetSortArrowA(&m_RepoList, m_currSortCol, !m_currSortDesc);
684 void CRepositoryBrowser::OnBnClickedButtonRevision()
686 // use the git log to allow selection of a version
687 CLogDlg dlg;
688 // tell the dialog to use mode for selecting revisions
689 dlg.SetSelect(true);
690 // only one revision must be selected however
691 dlg.SingleSelection(true);
692 if (dlg.DoModal() == IDOK)
694 // get selected hash if any
695 m_sRevision = dlg.GetSelectedHash();
696 Refresh();
700 void CRepositoryBrowser::SaveDividerPosition()
702 RECT rc;
703 GetDlgItem(IDC_REPOTREE)->GetClientRect(&rc);
704 CRegDWORD xPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RepobrowserDivider"));
705 xPos = rc.right - rc.left;
708 void CRepositoryBrowser::HandleDividerMove(CPoint point, bool bDraw)
710 RECT rect, tree, list, treelist, treelistclient;
712 // create an union of the tree and list control rectangle
713 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
714 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
715 UnionRect(&treelist, &tree, &list);
716 treelistclient = treelist;
717 ScreenToClient(&treelistclient);
719 ClientToScreen(&point);
720 GetClientRect(&rect);
721 ClientToScreen(&rect);
723 CPoint point2 = point;
724 if (point2.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
725 point2.x = treelist.left + REPOBROWSER_CTRL_MIN_WIDTH;
726 if (point2.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
727 point2.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
729 point.x -= rect.left;
730 point.y -= treelist.top;
732 OffsetRect(&treelist, -treelist.left, -treelist.top);
734 if (point.x < treelist.left+REPOBROWSER_CTRL_MIN_WIDTH)
735 point.x = treelist.left+REPOBROWSER_CTRL_MIN_WIDTH;
736 if (point.x > treelist.right-REPOBROWSER_CTRL_MIN_WIDTH)
737 point.x = treelist.right-REPOBROWSER_CTRL_MIN_WIDTH;
739 if (bDraw)
741 CDC * pDC = GetDC();
742 DrawXorBar(pDC, oldx + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
743 ReleaseDC(pDC);
746 oldx = point.x;
747 oldy = point.y;
749 //position the child controls
750 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&treelist);
751 treelist.right = point2.x - 2;
752 ScreenToClient(&treelist);
753 RemoveAnchor(IDC_REPOTREE);
754 GetDlgItem(IDC_REPOTREE)->MoveWindow(&treelist);
755 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&treelist);
756 treelist.left = point2.x + 2;
757 ScreenToClient(&treelist);
758 RemoveAnchor(IDC_REPOLIST);
759 GetDlgItem(IDC_REPOLIST)->MoveWindow(&treelist);
761 AddAnchor(IDC_REPOTREE, TOP_LEFT, BOTTOM_LEFT);
762 AddAnchor(IDC_REPOLIST, TOP_LEFT, BOTTOM_RIGHT);
765 void CRepositoryBrowser::OnMouseMove(UINT nFlags, CPoint point)
767 if (bDragMode == FALSE)
768 return;
770 RECT rect, tree, list, treelist, treelistclient;
771 // create an union of the tree and list control rectangle
772 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
773 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
774 UnionRect(&treelist, &tree, &list);
775 treelistclient = treelist;
776 ScreenToClient(&treelistclient);
778 //convert the mouse coordinates relative to the top-left of
779 //the window
780 ClientToScreen(&point);
781 GetClientRect(&rect);
782 ClientToScreen(&rect);
783 point.x -= rect.left;
784 point.y -= treelist.top;
786 //same for the window coordinates - make them relative to 0,0
787 OffsetRect(&treelist, -treelist.left, -treelist.top);
789 if (point.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
790 point.x = treelist.left + REPOBROWSER_CTRL_MIN_WIDTH;
791 if (point.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
792 point.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
794 if ((nFlags & MK_LBUTTON) && (point.x != oldx))
796 CDC * pDC = GetDC();
798 if (pDC)
800 DrawXorBar(pDC, oldx + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
801 DrawXorBar(pDC, point.x + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
803 ReleaseDC(pDC);
806 oldx = point.x;
807 oldy = point.y;
810 CStandAloneDialogTmpl<CResizableDialog>::OnMouseMove(nFlags, point);
813 void CRepositoryBrowser::OnLButtonDown(UINT nFlags, CPoint point)
815 RECT rect, tree, list, treelist, treelistclient;
817 // create an union of the tree and list control rectangle
818 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
819 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
820 UnionRect(&treelist, &tree, &list);
821 treelistclient = treelist;
822 ScreenToClient(&treelistclient);
824 //convert the mouse coordinates relative to the top-left of
825 //the window
826 ClientToScreen(&point);
827 GetClientRect(&rect);
828 ClientToScreen(&rect);
829 point.x -= rect.left;
830 point.y -= treelist.top;
832 //same for the window coordinates - make them relative to 0,0
833 OffsetRect(&treelist, -treelist.left, -treelist.top);
835 if (point.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
836 return CStandAloneDialogTmpl < CResizableDialog>::OnLButtonDown(nFlags, point);
837 if (point.x > treelist.right - 3)
838 return CStandAloneDialogTmpl < CResizableDialog>::OnLButtonDown(nFlags, point);
839 if (point.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
840 point.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
842 if ((point.y < treelist.top + 3) || (point.y > treelist.bottom - 3))
843 return CStandAloneDialogTmpl<CResizableDialog>::OnLButtonDown(nFlags, point);
845 bDragMode = true;
847 SetCapture();
849 CDC * pDC = GetDC();
850 DrawXorBar(pDC, point.x + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
851 ReleaseDC(pDC);
853 oldx = point.x;
854 oldy = point.y;
856 CStandAloneDialogTmpl<CResizableDialog>::OnLButtonDown(nFlags, point);
859 void CRepositoryBrowser::OnLButtonUp(UINT nFlags, CPoint point)
861 if (bDragMode == FALSE)
862 return;
864 HandleDividerMove(point, true);
866 bDragMode = false;
867 ReleaseCapture();
869 CStandAloneDialogTmpl<CResizableDialog>::OnLButtonUp(nFlags, point);
872 void CRepositoryBrowser::OnCaptureChanged(CWnd *pWnd)
874 bDragMode = false;
876 __super::OnCaptureChanged(pWnd);
879 void CRepositoryBrowser::DrawXorBar(CDC * pDC, int x1, int y1, int width, int height)
881 static WORD _dotPatternBmp[8] =
883 0x0055, 0x00aa, 0x0055, 0x00aa,
884 0x0055, 0x00aa, 0x0055, 0x00aa
887 HBITMAP hbm;
888 HBRUSH hbr, hbrushOld;
890 hbm = CreateBitmap(8, 8, 1, 1, _dotPatternBmp);
891 hbr = CreatePatternBrush(hbm);
893 pDC->SetBrushOrg(x1, y1);
894 hbrushOld = (HBRUSH)pDC->SelectObject(hbr);
896 PatBlt(pDC->GetSafeHdc(), x1, y1, width, height, PATINVERT);
898 pDC->SelectObject(hbrushOld);
900 DeleteObject(hbr);
901 DeleteObject(hbm);
904 BOOL CRepositoryBrowser::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
906 if (pWnd == this)
908 RECT rect;
909 POINT pt;
910 GetClientRect(&rect);
911 GetCursorPos(&pt);
912 ScreenToClient(&pt);
913 if (PtInRect(&rect, pt))
915 ClientToScreen(&pt);
916 // are we right of the tree control?
917 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&rect);
918 if ((pt.x > rect.right) && (pt.y >= rect.top + 3) && (pt.y <= rect.bottom - 3))
920 // but left of the list control?
921 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&rect);
922 if (pt.x < rect.left)
924 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE));
925 SetCursor(hCur);
926 return TRUE;
931 return CStandAloneDialogTmpl<CResizableDialog>::OnSetCursor(pWnd, nHitTest, message);
934 void CRepositoryBrowser::FileSaveAs(const CString path)
936 CTGitPath gitPath(path);
938 CString filename;
939 filename.Format(_T("%s-%s%s"), gitPath.GetBaseFilename(), m_sRevision.Left(g_Git.GetShortHASHLength()), gitPath.GetFileExtension());
940 CFileDialog dlg(FALSE, NULL, filename, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, NULL);
942 CString cmd, out;
943 if (dlg.DoModal() == IDOK)
945 filename = dlg.GetPathName();
946 if (g_Git.GetOneFile(m_sRevision, gitPath, filename))
948 out.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED, gitPath.GetGitPathString(), m_sRevision, filename);
949 MessageBox(out, _T("TortoiseGit"), MB_OK);
950 return;
955 void CRepositoryBrowser::OpenFile(const CString path, eOpenType mode)
957 CTGitPath gitPath(path);
959 CString temppath;
960 CString file;
961 GetTempPath(temppath);
962 file.Format(_T("%s%s_%s%s"), temppath, gitPath.GetBaseFilename(), m_sRevision.Left(g_Git.GetShortHASHLength()), gitPath.GetFileExtension());
964 CString out;
965 if(g_Git.GetOneFile(m_sRevision, gitPath, file))
967 out.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED, gitPath.GetGitPathString(), m_sRevision, file);
968 MessageBox(out, _T("TortoiseGit"), MB_OK);
969 return;
972 if (mode == ALTERNATIVEEDITOR)
974 CAppUtils::LaunchAlternativeEditor(file);
975 return;
977 else if (mode == OPEN)
979 int ret = HINSTANCE_ERROR;
980 ret = (int)ShellExecute(this->m_hWnd, NULL, file, NULL, NULL, SW_SHOW);
982 if (ret > HINSTANCE_ERROR)
983 return;
986 CString cmd = _T("RUNDLL32 Shell32,OpenAs_RunDLL ") + file;
987 CAppUtils::LaunchApplication(cmd, NULL, false);
989 bool CRepositoryBrowser::RevertItemToVersion(const CString &path)
991 CString cmd, out;
992 cmd.Format(_T("git.exe checkout %s -- \"%s\""), m_sRevision, path);
993 if (g_Git.Run(cmd, &out, CP_UTF8))
995 if (MessageBox(out, _T("TortoiseGit"), MB_ICONEXCLAMATION | MB_OKCANCEL) == IDCANCEL)
996 return false;
999 return true;