OSDN Git Service

BrowseRefs: Dont select current HEAD after change
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / BrowseRefsDlg.cpp
index 09c3fc1..a2d21b1 100644 (file)
@@ -65,13 +65,13 @@ BOOL CBrowseRefsDlg::OnInitDialog()
        AddAnchor(IDOK,BOTTOM_RIGHT);\r
        AddAnchor(IDCANCEL,BOTTOM_RIGHT);\r
 \r
-       Refresh();\r
+       Refresh(true);\r
 \r
 \r
        return TRUE;\r
 }\r
 \r
-CShadowTree* CShadowTree::GetNextSub(CString& nameLeft)\r
+CShadowTree* CShadowTree::GetNextSub(CString& nameLeft, bool bCreateIfNotExist)\r
 {\r
        int posSlash=nameLeft.Find('/');\r
        CString nameSub;\r
@@ -88,6 +88,9 @@ CShadowTree* CShadowTree::GetNextSub(CString& nameLeft)
        if(nameSub.IsEmpty())\r
                return NULL;\r
 \r
+       if(!bCreateIfNotExist && m_ShadowTree.find(nameSub)==m_ShadowTree.end())\r
+               return NULL;\r
+\r
        CShadowTree& nextNode=m_ShadowTree[nameSub];\r
        nextNode.m_csRefName=nameSub;\r
        nextNode.m_pParent=this;\r
@@ -96,13 +99,41 @@ CShadowTree* CShadowTree::GetNextSub(CString& nameLeft)
 \r
 typedef std::map<CString,CString> MAP_STRING_STRING;\r
 \r
-void CBrowseRefsDlg::Refresh()\r
+void CBrowseRefsDlg::Refresh(bool bSelectCurHead)\r
 {\r
 //     m_RefMap.clear();\r
 //     g_Git.GetMapHashToFriendName(m_RefMap);\r
                \r
+       CString selectRef;\r
+       if(bSelectCurHead)\r
+       {\r
+               g_Git.Run(L"git symbolic-ref HEAD",&selectRef,CP_UTF8);\r
+               selectRef.Trim(L"\r\n\t ");\r
+       }\r
+       else\r
+       {\r
+               POSITION pos=m_ListRefLeafs.GetFirstSelectedItemPosition();\r
+               //List ctrl selection?\r
+               if(pos)\r
+               {\r
+                       CShadowTree* pTree=(CShadowTree*)m_ListRefLeafs.GetItemData(\r
+                                       m_ListRefLeafs.GetNextSelectedItem(pos));\r
+                       selectRef=pTree->GetRefName();\r
+               }\r
+               else\r
+               {\r
+                       //Tree ctrl selection?\r
+                       HTREEITEM hTree=m_RefTreeCtrl.GetSelectedItem();\r
+                       if(hTree!=NULL)\r
+                       {\r
+                               CShadowTree* pTree=(CShadowTree*)m_RefTreeCtrl.GetItemData(hTree);\r
+                               selectRef=pTree->GetRefName();\r
+                       }\r
+               }\r
+       }\r
 \r
        m_RefTreeCtrl.DeleteAllItems();\r
+       m_ListRefLeafs.DeleteAllItems();\r
        m_TreeRoot.m_ShadowTree.clear();\r
        m_TreeRoot.m_csRefName="refs";\r
 //     m_TreeRoot.m_csShowName="Refs";\r
@@ -141,7 +172,7 @@ void CBrowseRefsDlg::Refresh()
        //Populate ref tree\r
        for(MAP_STRING_STRING::iterator iterRefMap=refMap.begin();iterRefMap!=refMap.end();++iterRefMap)\r
        {\r
-               CShadowTree& treeLeaf=GetTreeNode(iterRefMap->first);\r
+               CShadowTree& treeLeaf=GetTreeNode(iterRefMap->first,NULL,true);\r
                CString values=iterRefMap->second;\r
 \r
                int valuePos=0;\r
@@ -151,12 +182,8 @@ void CBrowseRefsDlg::Refresh()
                treeLeaf.m_csAuthor=  values.Tokenize(L"\04",valuePos);\r
        }\r
 \r
-       CString currHead;\r
-       g_Git.Run(L"git symbolic-ref HEAD",&currHead,CP_UTF8);\r
-\r
-       currHead.Trim(L"\r\n\t ");\r
 \r
-       if(!SelectRef(currHead))\r
+       if(selectRef.IsEmpty() || !SelectRef(selectRef))\r
                //Probably not on a branch. Select root node.\r
                m_RefTreeCtrl.Expand(m_TreeRoot.m_hTree,TVE_EXPAND);\r
 \r
@@ -167,7 +194,14 @@ bool CBrowseRefsDlg::SelectRef(CString refName)
        if(wcsnicmp(refName,L"refs/",5)!=0)\r
                return false; // Not a ref name\r
 \r
-       CShadowTree& treeLeafHead=GetTreeNode(refName);\r
+       CShadowTree& treeLeafHead=GetTreeNode(refName,NULL,false);\r
+       if(treeLeafHead.m_hTree != NULL)\r
+       {\r
+               //Not a leaf. Select tree node and return\r
+               m_RefTreeCtrl.Select(treeLeafHead.m_hTree,TVGN_CARET);\r
+               return true;\r
+       }\r
+\r
        if(treeLeafHead.m_pParent==NULL)\r
                return false; //Weird... should not occur.\r
 \r
@@ -180,13 +214,14 @@ bool CBrowseRefsDlg::SelectRef(CString refName)
                if(pCurrShadowTree == &treeLeafHead)\r
                {\r
                        m_ListRefLeafs.SetItemState(indexPos,LVIS_SELECTED,LVIS_SELECTED);\r
+                       m_ListRefLeafs.EnsureVisible(indexPos,FALSE);\r
                }\r
        }\r
 \r
        return true;\r
 }\r
 \r
-CShadowTree& CBrowseRefsDlg::GetTreeNode(CString refName, CShadowTree* pTreePos)\r
+CShadowTree& CBrowseRefsDlg::GetTreeNode(CString refName, CShadowTree* pTreePos, bool bCreateIfNotExist)\r
 {\r
        if(pTreePos==NULL)\r
        {\r
@@ -197,11 +232,11 @@ CShadowTree& CBrowseRefsDlg::GetTreeNode(CString refName, CShadowTree* pTreePos)
        if(refName.IsEmpty())\r
                return *pTreePos;//Found leaf\r
 \r
-       CShadowTree* pNextTree=pTreePos->GetNextSub(refName);\r
+       CShadowTree* pNextTree=pTreePos->GetNextSub(refName,bCreateIfNotExist);\r
        if(pNextTree==NULL)\r
        {\r
-               //Should not occur when all ref-names are valid.\r
-               ASSERT(FALSE);\r
+               //Should not occur when all ref-names are valid and bCreateIfNotExist is true.\r
+               ASSERT(!bCreateIfNotExist);\r
                return *pTreePos;\r
        }\r
 \r
@@ -217,7 +252,7 @@ CShadowTree& CBrowseRefsDlg::GetTreeNode(CString refName, CShadowTree* pTreePos)
                }\r
        }\r
 \r
-       return GetTreeNode(refName,pNextTree);\r
+       return GetTreeNode(refName, pNextTree, bCreateIfNotExist);\r
 }\r
 \r
 \r