1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013 - TortoiseGit
4 // Copyright (C) 2012-2013 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
25 #include "TortoiseProc.h"
26 #include "RepositoryBrowser.h"
30 #include "UnicodeUtils.h"
31 #include "SysImageList.h"
35 #include "PathUtils.h"
36 #include "StringUtils.h"
39 void SetSortArrowA(CListCtrl
* control
, int nColumn
, bool bAscending
)
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
);
56 pHeader
->GetItem(nColumn
, &HeaderItem
);
57 HeaderItem
.fmt
|= (bAscending
? HDF_SORTUP
: HDF_SORTDOWN
);
58 pHeader
->SetItem(nColumn
, &HeaderItem
);
62 class CRepoListCompareFunc
65 CRepoListCompareFunc(CListCtrl
* pList
, int col
, bool desc
)
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
);
84 case CRepositoryBrowser::eCol_Name
:
85 result
= SortStrCmp(pLeft
->m_sName
, pRight
->m_sName
);
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
);
94 case CRepositoryBrowser::eCol_FileSize
:
95 if (pLeft
->m_iSize
> pRight
->m_iSize
)
97 else if (pLeft
->m_iSize
< pRight
->m_iSize
)
100 result
= SortStrCmp(pLeft
->m_sName
, pRight
->m_sName
);
106 if (pLeft
->m_bFolder
!= pRight
->m_bFolder
)
108 if (pRight
->m_bFolder
)
116 int SortStrCmp(CString
&left
, CString
&right
)
118 if (CRepositoryBrowser::s_bSortLogical
)
119 return StrCmpLogicalW(left
, right
);
120 return StrCmpI(left
, right
);
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
)
137 , m_currSortDesc(false)
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
)
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
)
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
);
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);
227 GetDlgItem(IDC_REPOTREE
)->GetClientRect(&rc
);
228 xPos
= rc
.right
- rc
.left
;
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
);
241 m_RepoList
.SetFocus();
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
)
262 LPNMITEMACTIVATE pNmItemActivate
= reinterpret_cast<LPNMITEMACTIVATE
>(pNMHDR
);
263 if (pNmItemActivate
->iItem
< 0)
266 CShadowFilesTree
* pItem
= (CShadowFilesTree
*)m_RepoList
.GetItemData(pNmItemActivate
->iItem
);
270 if (!pItem
->m_bFolder
)
272 OpenFile(pItem
->GetFullName(), OPEN
);
277 FillListCtrlForShadowTree(pItem
);
278 m_RepoTree
.SelectItem(pItem
->m_hTree
);
282 void CRepositoryBrowser::Refresh()
284 m_RepoTree
.DeleteAllItems();
285 m_RepoList
.DeleteAllItems();
286 m_TreeRoot
.m_ShadowTree
.clear();
287 m_TreeRoot
.m_sName
= "";
288 m_TreeRoot
.m_bFolder
= true;
290 TVINSERTSTRUCT tvinsert
= {0};
291 tvinsert
.hParent
= TVI_ROOT
;
292 tvinsert
.hInsertAfter
= TVI_ROOT
;
293 tvinsert
.itemex
.mask
= TVIF_DI_SETITEM
| TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_STATE
;
294 tvinsert
.itemex
.pszText
= L
"/";
295 tvinsert
.itemex
.lParam
= (LPARAM
)&m_TreeRoot
;
296 tvinsert
.itemex
.iImage
= m_nIconFolder
;
297 tvinsert
.itemex
.iSelectedImage
= m_nOpenIconFolder
;
298 m_TreeRoot
.m_hTree
= m_RepoTree
.InsertItem(&tvinsert
);
300 ReadTree(&m_TreeRoot
);
301 m_RepoTree
.Expand(m_TreeRoot
.m_hTree
, TVE_EXPAND
);
302 FillListCtrlForShadowTree(&m_TreeRoot
);
303 m_RepoTree
.SelectItem(m_TreeRoot
.m_hTree
);
306 int CRepositoryBrowser::ReadTreeRecursive(git_repository
&repo
, git_tree
* tree
, CShadowFilesTree
* treeroot
)
308 size_t count
= git_tree_entrycount(tree
);
310 for (int i
= 0; i
< count
; i
++)
312 const git_tree_entry
*entry
= git_tree_entry_byindex(tree
, i
);
315 int mode
= git_tree_entry_filemode(entry
);
317 CString base
= CUnicodeUtils::GetUnicode(git_tree_entry_name(entry
), CP_UTF8
);
319 git_object
*object
= NULL
;
320 git_tree_entry_to_object(&object
, &repo
, entry
);
324 CShadowFilesTree
* pNextTree
= &treeroot
->m_ShadowTree
[base
];
325 pNextTree
->m_sName
= base
;
326 pNextTree
->m_pParent
= treeroot
;
330 pNextTree
->m_bFolder
= true;
332 TVINSERTSTRUCT tvinsert
= {0};
333 tvinsert
.hParent
= treeroot
->m_hTree
;
334 tvinsert
.hInsertAfter
= TVI_SORT
;
335 tvinsert
.itemex
.mask
= TVIF_DI_SETITEM
| TVIF_PARAM
| TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_STATE
;
336 tvinsert
.itemex
.pszText
= base
.GetBuffer(base
.GetLength());
337 tvinsert
.itemex
.lParam
= (LPARAM
)pNextTree
;
338 tvinsert
.itemex
.iImage
= m_nIconFolder
;
339 tvinsert
.itemex
.iSelectedImage
= m_nOpenIconFolder
;
340 pNextTree
->m_hTree
= m_RepoTree
.InsertItem(&tvinsert
);
341 base
.ReleaseBuffer();
343 ReadTreeRecursive(repo
, (git_tree
*)object
, pNextTree
);
347 const git_oid
* oid
= git_object_id(object
);
350 git_blob_lookup(&blob
, &repo
, oid
);
354 pNextTree
->m_iSize
= git_blob_rawsize(blob
);
358 git_object_free(object
);
364 int CRepositoryBrowser::ReadTree(CShadowFilesTree
* treeroot
)
366 CStringA gitdir
= CUnicodeUtils::GetMulti(g_Git
.m_CurrentDir
, CP_UTF8
);
367 git_repository
*repository
= NULL
;
368 git_commit
*commit
= NULL
;
369 git_tree
* tree
= NULL
;
373 ret
= git_repository_open(&repository
, gitdir
.GetBuffer());
376 MessageBox(g_Git
.GetGitLastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_ICONERROR
);
381 if (g_Git
.GetHash(hash
, m_sRevision
))
383 MessageBox(g_Git
.GetGitLastErr(_T("Could not get hash of ") + m_sRevision
+ _T(".")), _T("TortoiseGit"), MB_ICONERROR
);
386 ret
= git_commit_lookup(&commit
, repository
, (git_oid
*) hash
.m_hash
);
389 MessageBox(g_Git
.GetGitLastErr(_T("Could not lookup commit.")), _T("TortoiseGit"), MB_ICONERROR
);
393 ret
= git_commit_tree(&tree
, commit
);
396 MessageBox(g_Git
.GetGitLastErr(_T("Could not get tree of commit.")), _T("TortoiseGit"), MB_ICONERROR
);
400 ReadTreeRecursive(*repository
, tree
, treeroot
);
402 // try to resolve hash to a branch name
403 if (m_sRevision
== hash
.ToString())
406 if (g_Git
.GetMapHashToFriendName(map
))
407 MessageBox(g_Git
.GetGitLastErr(_T("Could not get all refs.")), _T("TortoiseGit"), MB_ICONERROR
);
408 if (!map
[hash
].empty())
409 m_sRevision
= map
[hash
].at(0);
411 this->GetDlgItem(IDC_BUTTON_REVISION
)->SetWindowText(m_sRevision
);
418 git_commit_free(commit
);
421 git_repository_free(repository
);
426 void CRepositoryBrowser::OnTvnSelchangedRepoTree(NMHDR
*pNMHDR
, LRESULT
*pResult
)
428 LPNMTREEVIEW pNMTreeView
= reinterpret_cast<LPNMTREEVIEW
>(pNMHDR
);
431 FillListCtrlForTreeNode(pNMTreeView
->itemNew
.hItem
);
434 void CRepositoryBrowser::FillListCtrlForTreeNode(HTREEITEM treeNode
)
436 m_RepoList
.DeleteAllItems();
438 CShadowFilesTree
* pTree
= (CShadowFilesTree
*)(m_RepoTree
.GetItemData(treeNode
));
445 CString url
= _T("/") + pTree
->GetFullName();
446 GetDlgItem(IDC_REPOBROWSER_URL
)->SetWindowText(url
);
448 FillListCtrlForShadowTree(pTree
);
451 void CRepositoryBrowser::FillListCtrlForShadowTree(CShadowFilesTree
* pTree
)
453 for (TShadowFilesTreeMap::iterator itShadowTree
= pTree
->m_ShadowTree
.begin(); itShadowTree
!= pTree
->m_ShadowTree
.end(); ++itShadowTree
)
455 int icon
= m_nIconFolder
;
456 if (!(*itShadowTree
).second
.m_bFolder
)
457 icon
= SYS_IMAGE_LIST().GetFileIconIndex((*itShadowTree
).second
.m_sName
);
459 int indexItem
= m_RepoList
.InsertItem(m_RepoList
.GetItemCount(), (*itShadowTree
).second
.m_sName
, icon
);
461 m_RepoList
.SetItemData(indexItem
, (DWORD_PTR
)&(*itShadowTree
).second
);
462 if (!(*itShadowTree
).second
.m_bFolder
)
466 temp
= CPathUtils::GetFileExtFromPath((*itShadowTree
).second
.m_sName
);
467 m_RepoList
.SetItemText(indexItem
, eCol_Extension
, temp
);
469 StrFormatByteSize((*itShadowTree
).second
.m_iSize
, temp
.GetBuffer(20), 20);
470 temp
.ReleaseBuffer();
471 m_RepoList
.SetItemText(indexItem
, eCol_FileSize
, temp
);
475 CRepoListCompareFunc
compareFunc(&m_RepoList
, m_currSortCol
, m_currSortDesc
);
476 m_RepoList
.SortItemsEx(&CRepoListCompareFunc::StaticCompare
, (DWORD_PTR
)&compareFunc
);
478 SetSortArrowA(&m_RepoList
, m_currSortCol
, !m_currSortDesc
);
481 void CRepositoryBrowser::OnContextMenu(CWnd
* pWndFrom
, CPoint point
)
483 if (pWndFrom
== &m_RepoList
)
484 OnContextMenu_RepoList(point
);
485 else if (pWndFrom
== &m_RepoTree
)
486 OnContextMenu_RepoTree(point
);
489 void CRepositoryBrowser::OnContextMenu_RepoTree(CPoint point
)
491 CPoint clientPoint
= point
;
492 m_RepoTree
.ScreenToClient(&clientPoint
);
494 HTREEITEM hTreeItem
= m_RepoTree
.HitTest(clientPoint
);
495 if (hTreeItem
== NULL
)
498 TShadowFilesTreeList selectedLeafs
;
499 selectedLeafs
.push_back((CShadowFilesTree
*)m_RepoTree
.GetItemData(hTreeItem
));
501 ShowContextMenu(point
, selectedLeafs
, ONLY_FOLDERS
);
504 void CRepositoryBrowser::OnContextMenu_RepoList(CPoint point
)
506 TShadowFilesTreeList selectedLeafs
;
507 selectedLeafs
.reserve(m_RepoList
.GetSelectedCount());
509 bool folderSelected
= false;
510 bool filesSelected
= false;
512 POSITION pos
= m_RepoList
.GetFirstSelectedItemPosition();
515 CShadowFilesTree
* item
= (CShadowFilesTree
*)m_RepoList
.GetItemData(m_RepoList
.GetNextSelectedItem(pos
));
517 folderSelected
= true;
519 filesSelected
= true;
520 selectedLeafs
.push_back(item
);
523 eSelectionType selType
= ONLY_FILES
;
524 if (folderSelected
&& filesSelected
)
526 else if (folderSelected
)
527 selType
= ONLY_FOLDERS
;
529 ShowContextMenu(point
, selectedLeafs
, selType
);
532 void CRepositoryBrowser::ShowContextMenu(CPoint point
, TShadowFilesTreeList
&selectedLeafs
, eSelectionType selType
)
535 popupMenu
.CreatePopupMenu();
537 bool bAddSeparator
= false;
539 if (selectedLeafs
.size() == 1)
541 popupMenu
.AppendMenuIcon(eCmd_Open
, IDS_REPOBROWSE_OPEN
, IDI_OPEN
);
542 popupMenu
.SetDefaultItem(eCmd_Open
, FALSE
);
543 if (selType
== ONLY_FILES
)
545 popupMenu
.AppendMenuIcon(eCmd_OpenWith
, IDS_LOG_POPUP_OPENWITH
, IDI_OPEN
);
546 popupMenu
.AppendMenuIcon(eCmd_OpenWithAlternativeEditor
, IDS_LOG_POPUP_VIEWREV
);
549 popupMenu
.AppendMenu(MF_SEPARATOR
);
551 if (m_bHasWC
&& selType
== ONLY_FILES
)
553 popupMenu
.AppendMenuIcon(eCmd_CompareWC
, IDS_LOG_POPUP_COMPARE
, IDI_DIFF
);
554 bAddSeparator
= true;
558 popupMenu
.AppendMenu(MF_SEPARATOR
);
559 bAddSeparator
= false;
562 temp
.LoadString(IDS_MENULOG
);
563 popupMenu
.AppendMenuIcon(eCmd_ViewLog
, temp
, IDI_LOG
);
565 if (selType
== ONLY_FILES
)
568 popupMenu
.AppendMenuIcon(eCmd_Blame
, IDS_LOG_POPUP_BLAME
, IDI_BLAME
);
570 popupMenu
.AppendMenu(MF_SEPARATOR
);
571 temp
.LoadString(IDS_LOG_POPUP_SAVE
);
572 popupMenu
.AppendMenuIcon(eCmd_SaveAs
, temp
, IDI_SAVEAS
);
575 bAddSeparator
= true;
578 if (selType
== ONLY_FILES
&& m_bHasWC
)
580 popupMenu
.AppendMenuIcon(eCmd_Revert
, IDS_LOG_POPUP_REVERTTOREV
, IDI_REVERT
);
581 bAddSeparator
= true;
585 popupMenu
.AppendMenu(MF_SEPARATOR
);
586 bAddSeparator
= false;
588 popupMenu
.AppendMenuIcon(eCmd_CopyPath
, IDS_STATUSLIST_CONTEXT_COPY
, IDI_COPYCLIP
);
590 eCmd cmd
= (eCmd
)popupMenu
.TrackPopupMenuEx(TPM_LEFTALIGN
|TPM_RETURNCMD
, point
.x
, point
.y
, this, 0);
596 sCmd
.Format(_T("/command:log /path:\"%s\\%s\""), g_Git
.m_CurrentDir
, selectedLeafs
.at(0)->GetFullName());
597 CAppUtils::RunTortoiseGitProc(sCmd
);
602 CAppUtils::LaunchTortoiseBlame(g_Git
.m_CurrentDir
+ _T("\\") + selectedLeafs
.at(0)->GetFullName(), m_sRevision
);
606 if (selectedLeafs
.at(0)->m_bFolder
)
608 FillListCtrlForTreeNode(selectedLeafs
.at(0)->m_hTree
);
609 m_RepoTree
.SelectItem(selectedLeafs
.at(0)->m_hTree
);
612 OpenFile(selectedLeafs
.at(0)->GetFullName(), OPEN
);
615 OpenFile(selectedLeafs
.at(0)->GetFullName(), OPEN_WITH
);
617 case eCmd_OpenWithAlternativeEditor
:
618 OpenFile(selectedLeafs
.at(0)->GetFullName(), ALTERNATIVEEDITOR
);
622 CTGitPath
file(selectedLeafs
.at(0)->GetFullName());
623 CGitDiff::Diff(&file
, &file
, GIT_REV_ZERO
, m_sRevision
);
629 for (TShadowFilesTreeList::iterator itShadowTree
= selectedLeafs
.begin(); itShadowTree
!= selectedLeafs
.end(); ++itShadowTree
)
631 if (RevertItemToVersion((*itShadowTree
)->GetFullName()))
637 msg
.Format(IDS_STATUSLIST_FILESREVERTED
, count
, m_sRevision
);
638 MessageBox(msg
, _T("TortoiseGit"), MB_OK
);
642 FileSaveAs(selectedLeafs
.at(0)->GetFullName());
647 for (TShadowFilesTreeList::iterator itShadowTree
= selectedLeafs
.begin(); itShadowTree
!= selectedLeafs
.end(); ++itShadowTree
)
649 sClipboard
+= (*itShadowTree
)->m_sName
+ _T("\r\n");
651 CStringUtils::WriteAsciiStringToClipboard(sClipboard
);
657 BOOL
CRepositoryBrowser::PreTranslateMessage(MSG
* pMsg
)
659 if (pMsg
->message
== WM_KEYDOWN
)
661 switch (pMsg
->wParam
)
671 return CResizableStandAloneDialog::PreTranslateMessage(pMsg
);
674 void CRepositoryBrowser::OnLvnColumnclickRepoList(NMHDR
*pNMHDR
, LRESULT
*pResult
)
676 LPNMLISTVIEW pNMLV
= reinterpret_cast<LPNMLISTVIEW
>(pNMHDR
);
680 if (m_currSortCol
== pNMLV
->iSubItem
)
681 m_currSortDesc
= !m_currSortDesc
;
684 m_currSortCol
= pNMLV
->iSubItem
;
685 m_currSortDesc
= false;
688 CRepoListCompareFunc
compareFunc(&m_RepoList
, m_currSortCol
, m_currSortDesc
);
689 m_RepoList
.SortItemsEx(&CRepoListCompareFunc::StaticCompare
, (DWORD_PTR
)&compareFunc
);
691 SetSortArrowA(&m_RepoList
, m_currSortCol
, !m_currSortDesc
);
694 void CRepositoryBrowser::OnBnClickedButtonRevision()
696 // use the git log to allow selection of a version
698 // tell the dialog to use mode for selecting revisions
700 // only one revision must be selected however
701 dlg
.SingleSelection(true);
702 if (dlg
.DoModal() == IDOK
)
704 // get selected hash if any
705 m_sRevision
= dlg
.GetSelectedHash();
710 void CRepositoryBrowser::SaveDividerPosition()
713 GetDlgItem(IDC_REPOTREE
)->GetClientRect(&rc
);
714 CRegDWORD xPos
= CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RepobrowserDivider"));
715 xPos
= rc
.right
- rc
.left
;
718 void CRepositoryBrowser::HandleDividerMove(CPoint point
, bool bDraw
)
720 RECT rect
, tree
, list
, treelist
, treelistclient
;
722 // create an union of the tree and list control rectangle
723 GetDlgItem(IDC_REPOLIST
)->GetWindowRect(&list
);
724 GetDlgItem(IDC_REPOTREE
)->GetWindowRect(&tree
);
725 UnionRect(&treelist
, &tree
, &list
);
726 treelistclient
= treelist
;
727 ScreenToClient(&treelistclient
);
729 ClientToScreen(&point
);
730 GetClientRect(&rect
);
731 ClientToScreen(&rect
);
733 CPoint point2
= point
;
734 if (point2
.x
< treelist
.left
+ REPOBROWSER_CTRL_MIN_WIDTH
)
735 point2
.x
= treelist
.left
+ REPOBROWSER_CTRL_MIN_WIDTH
;
736 if (point2
.x
> treelist
.right
- REPOBROWSER_CTRL_MIN_WIDTH
)
737 point2
.x
= treelist
.right
- REPOBROWSER_CTRL_MIN_WIDTH
;
739 point
.x
-= rect
.left
;
740 point
.y
-= treelist
.top
;
742 OffsetRect(&treelist
, -treelist
.left
, -treelist
.top
);
744 if (point
.x
< treelist
.left
+REPOBROWSER_CTRL_MIN_WIDTH
)
745 point
.x
= treelist
.left
+REPOBROWSER_CTRL_MIN_WIDTH
;
746 if (point
.x
> treelist
.right
-REPOBROWSER_CTRL_MIN_WIDTH
)
747 point
.x
= treelist
.right
-REPOBROWSER_CTRL_MIN_WIDTH
;
752 DrawXorBar(pDC
, oldx
+ 2, treelistclient
.top
, 4, treelistclient
.bottom
- treelistclient
.top
- 2);
759 //position the child controls
760 GetDlgItem(IDC_REPOTREE
)->GetWindowRect(&treelist
);
761 treelist
.right
= point2
.x
- 2;
762 ScreenToClient(&treelist
);
763 RemoveAnchor(IDC_REPOTREE
);
764 GetDlgItem(IDC_REPOTREE
)->MoveWindow(&treelist
);
765 GetDlgItem(IDC_REPOLIST
)->GetWindowRect(&treelist
);
766 treelist
.left
= point2
.x
+ 2;
767 ScreenToClient(&treelist
);
768 RemoveAnchor(IDC_REPOLIST
);
769 GetDlgItem(IDC_REPOLIST
)->MoveWindow(&treelist
);
771 AddAnchor(IDC_REPOTREE
, TOP_LEFT
, BOTTOM_LEFT
);
772 AddAnchor(IDC_REPOLIST
, TOP_LEFT
, BOTTOM_RIGHT
);
775 void CRepositoryBrowser::OnMouseMove(UINT nFlags
, CPoint point
)
777 if (bDragMode
== FALSE
)
780 RECT rect
, tree
, list
, treelist
, treelistclient
;
781 // create an union of the tree and list control rectangle
782 GetDlgItem(IDC_REPOLIST
)->GetWindowRect(&list
);
783 GetDlgItem(IDC_REPOTREE
)->GetWindowRect(&tree
);
784 UnionRect(&treelist
, &tree
, &list
);
785 treelistclient
= treelist
;
786 ScreenToClient(&treelistclient
);
788 //convert the mouse coordinates relative to the top-left of
790 ClientToScreen(&point
);
791 GetClientRect(&rect
);
792 ClientToScreen(&rect
);
793 point
.x
-= rect
.left
;
794 point
.y
-= treelist
.top
;
796 //same for the window coordinates - make them relative to 0,0
797 OffsetRect(&treelist
, -treelist
.left
, -treelist
.top
);
799 if (point
.x
< treelist
.left
+ REPOBROWSER_CTRL_MIN_WIDTH
)
800 point
.x
= treelist
.left
+ REPOBROWSER_CTRL_MIN_WIDTH
;
801 if (point
.x
> treelist
.right
- REPOBROWSER_CTRL_MIN_WIDTH
)
802 point
.x
= treelist
.right
- REPOBROWSER_CTRL_MIN_WIDTH
;
804 if ((nFlags
& MK_LBUTTON
) && (point
.x
!= oldx
))
810 DrawXorBar(pDC
, oldx
+ 2, treelistclient
.top
, 4, treelistclient
.bottom
- treelistclient
.top
- 2);
811 DrawXorBar(pDC
, point
.x
+ 2, treelistclient
.top
, 4, treelistclient
.bottom
- treelistclient
.top
- 2);
820 CStandAloneDialogTmpl
<CResizableDialog
>::OnMouseMove(nFlags
, point
);
823 void CRepositoryBrowser::OnLButtonDown(UINT nFlags
, CPoint point
)
825 RECT rect
, tree
, list
, treelist
, treelistclient
;
827 // create an union of the tree and list control rectangle
828 GetDlgItem(IDC_REPOLIST
)->GetWindowRect(&list
);
829 GetDlgItem(IDC_REPOTREE
)->GetWindowRect(&tree
);
830 UnionRect(&treelist
, &tree
, &list
);
831 treelistclient
= treelist
;
832 ScreenToClient(&treelistclient
);
834 //convert the mouse coordinates relative to the top-left of
836 ClientToScreen(&point
);
837 GetClientRect(&rect
);
838 ClientToScreen(&rect
);
839 point
.x
-= rect
.left
;
840 point
.y
-= treelist
.top
;
842 //same for the window coordinates - make them relative to 0,0
843 OffsetRect(&treelist
, -treelist
.left
, -treelist
.top
);
845 if (point
.x
< treelist
.left
+ REPOBROWSER_CTRL_MIN_WIDTH
)
846 return CStandAloneDialogTmpl
< CResizableDialog
>::OnLButtonDown(nFlags
, point
);
847 if (point
.x
> treelist
.right
- 3)
848 return CStandAloneDialogTmpl
< CResizableDialog
>::OnLButtonDown(nFlags
, point
);
849 if (point
.x
> treelist
.right
- REPOBROWSER_CTRL_MIN_WIDTH
)
850 point
.x
= treelist
.right
- REPOBROWSER_CTRL_MIN_WIDTH
;
852 if ((point
.y
< treelist
.top
+ 3) || (point
.y
> treelist
.bottom
- 3))
853 return CStandAloneDialogTmpl
<CResizableDialog
>::OnLButtonDown(nFlags
, point
);
860 DrawXorBar(pDC
, point
.x
+ 2, treelistclient
.top
, 4, treelistclient
.bottom
- treelistclient
.top
- 2);
866 CStandAloneDialogTmpl
<CResizableDialog
>::OnLButtonDown(nFlags
, point
);
869 void CRepositoryBrowser::OnLButtonUp(UINT nFlags
, CPoint point
)
871 if (bDragMode
== FALSE
)
874 HandleDividerMove(point
, true);
879 CStandAloneDialogTmpl
<CResizableDialog
>::OnLButtonUp(nFlags
, point
);
882 void CRepositoryBrowser::OnCaptureChanged(CWnd
*pWnd
)
886 __super::OnCaptureChanged(pWnd
);
889 void CRepositoryBrowser::DrawXorBar(CDC
* pDC
, int x1
, int y1
, int width
, int height
)
891 static WORD _dotPatternBmp
[8] =
893 0x0055, 0x00aa, 0x0055, 0x00aa,
894 0x0055, 0x00aa, 0x0055, 0x00aa
898 HBRUSH hbr
, hbrushOld
;
900 hbm
= CreateBitmap(8, 8, 1, 1, _dotPatternBmp
);
901 hbr
= CreatePatternBrush(hbm
);
903 pDC
->SetBrushOrg(x1
, y1
);
904 hbrushOld
= (HBRUSH
)pDC
->SelectObject(hbr
);
906 PatBlt(pDC
->GetSafeHdc(), x1
, y1
, width
, height
, PATINVERT
);
908 pDC
->SelectObject(hbrushOld
);
914 BOOL
CRepositoryBrowser::OnSetCursor(CWnd
* pWnd
, UINT nHitTest
, UINT message
)
920 GetClientRect(&rect
);
923 if (PtInRect(&rect
, pt
))
926 // are we right of the tree control?
927 GetDlgItem(IDC_REPOTREE
)->GetWindowRect(&rect
);
928 if ((pt
.x
> rect
.right
) && (pt
.y
>= rect
.top
+ 3) && (pt
.y
<= rect
.bottom
- 3))
930 // but left of the list control?
931 GetDlgItem(IDC_REPOLIST
)->GetWindowRect(&rect
);
932 if (pt
.x
< rect
.left
)
934 HCURSOR hCur
= LoadCursor(NULL
, MAKEINTRESOURCE(IDC_SIZEWE
));
941 return CStandAloneDialogTmpl
<CResizableDialog
>::OnSetCursor(pWnd
, nHitTest
, message
);
944 void CRepositoryBrowser::FileSaveAs(const CString path
)
946 CTGitPath
gitPath(path
);
949 filename
.Format(_T("%s-%s%s"), gitPath
.GetBaseFilename(), CGitHash(m_sRevision
).ToString().Left(g_Git
.GetShortHASHLength()), gitPath
.GetFileExtension());
950 CFileDialog
dlg(FALSE
, NULL
, filename
, OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
, NULL
);
953 INT_PTR ret
= dlg
.DoModal();
954 SetCurrentDirectory(g_Git
.m_CurrentDir
);
957 filename
= dlg
.GetPathName();
958 if (g_Git
.GetOneFile(m_sRevision
, gitPath
, filename
))
960 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, gitPath
.GetGitPathString(), m_sRevision
, filename
);
961 MessageBox(out
, _T("TortoiseGit"), MB_OK
);
967 void CRepositoryBrowser::OpenFile(const CString path
, eOpenType mode
)
969 CTGitPath
gitPath(path
);
973 GetTempPath(temppath
);
974 file
.Format(_T("%s%s_%s%s"), temppath
, gitPath
.GetBaseFilename(), CGitHash(m_sRevision
).ToString().Left(g_Git
.GetShortHASHLength()), gitPath
.GetFileExtension());
977 if(g_Git
.GetOneFile(m_sRevision
, gitPath
, file
))
979 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, gitPath
.GetGitPathString(), m_sRevision
, file
);
980 MessageBox(out
, _T("TortoiseGit"), MB_OK
);
984 if (mode
== ALTERNATIVEEDITOR
)
986 CAppUtils::LaunchAlternativeEditor(file
);
989 else if (mode
== OPEN
)
991 int ret
= HINSTANCE_ERROR
;
992 ret
= (int)ShellExecute(this->m_hWnd
, NULL
, file
, NULL
, NULL
, SW_SHOW
);
994 if (ret
> HINSTANCE_ERROR
)
998 CString cmd
= _T("RUNDLL32 Shell32,OpenAs_RunDLL ") + file
;
999 CAppUtils::LaunchApplication(cmd
, NULL
, false);
1001 bool CRepositoryBrowser::RevertItemToVersion(const CString
&path
)
1004 cmd
.Format(_T("git.exe checkout %s -- \"%s\""), m_sRevision
, path
);
1005 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
1007 if (MessageBox(out
, _T("TortoiseGit"), MB_ICONEXCLAMATION
| MB_OKCANCEL
) == IDCANCEL
)