OSDN Git Service

BrowseRefs: Made the dialog usable for picking a ref
authorJohan 't Hart <johanthart@gmail.com>
Tue, 26 May 2009 19:46:49 +0000 (21:46 +0200)
committerFrank Li <lznuaa@gmail.com>
Thu, 28 May 2009 05:49:26 +0000 (13:49 +0800)
src/TortoiseProc/BrowseRefsDlg.cpp
src/TortoiseProc/BrowseRefsDlg.h

index 7a02aae..c6862b0 100644 (file)
@@ -41,7 +41,8 @@ CBrowseRefsDlg::CBrowseRefsDlg(CString cmdPath, CWnd* pParent /*=NULL*/)
 :      CResizableStandAloneDialog(CBrowseRefsDlg::IDD, pParent),\r
        m_cmdPath(cmdPath),\r
        m_currSortCol(-1),\r
-       m_currSortDesc(false)\r
+       m_currSortDesc(false),\r
+       m_initialRef(L"HEAD")\r
 {\r
 \r
 }\r
@@ -63,6 +64,8 @@ BEGIN_MESSAGE_MAP(CBrowseRefsDlg, CResizableStandAloneDialog)
        ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_REF, &CBrowseRefsDlg::OnTvnSelchangedTreeRef)\r
        ON_WM_CONTEXTMENU()\r
        ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST_REF_LEAFS, &CBrowseRefsDlg::OnLvnColumnclickListRefLeafs)\r
+       ON_WM_DESTROY()\r
+       ON_NOTIFY(NM_DBLCLK, IDC_LIST_REF_LEAFS, &CBrowseRefsDlg::OnNMDblclkListRefLeafs)\r
 END_MESSAGE_MAP()\r
 \r
 \r
@@ -89,7 +92,7 @@ BOOL CBrowseRefsDlg::OnInitDialog()
        AddAnchor(IDOK,BOTTOM_RIGHT);\r
        AddAnchor(IDCANCEL,BOTTOM_RIGHT);\r
 \r
-       Refresh(true);\r
+       Refresh(m_initialRef);\r
 \r
 \r
        return TRUE;\r
@@ -123,38 +126,48 @@ CShadowTree* CShadowTree::GetNextSub(CString& nameLeft, bool bCreateIfNotExist)
 \r
 typedef std::map<CString,CString> MAP_STRING_STRING;\r
 \r
-void CBrowseRefsDlg::Refresh(bool bSelectCurHead)\r
+CString CBrowseRefsDlg::GetSelectedRef(bool onlyIfLeaf)\r
 {\r
-//     m_RefMap.clear();\r
-//     g_Git.GetMapHashToFriendName(m_RefMap);\r
-               \r
-       CString selectRef;\r
-       if(bSelectCurHead)\r
+       POSITION pos=m_ListRefLeafs.GetFirstSelectedItemPosition();\r
+       //List ctrl selection?\r
+       if(pos)\r
        {\r
-               g_Git.Run(L"git symbolic-ref HEAD",&selectRef,CP_UTF8);\r
-               selectRef.Trim(L"\r\n\t ");\r
+               //A leaf is selected\r
+               CShadowTree* pTree=(CShadowTree*)m_ListRefLeafs.GetItemData(\r
+                               m_ListRefLeafs.GetNextSelectedItem(pos));\r
+               return pTree->GetRefName();\r
        }\r
-       else\r
+       else if(!onlyIfLeaf)\r
        {\r
-               POSITION pos=m_ListRefLeafs.GetFirstSelectedItemPosition();\r
-               //List ctrl selection?\r
-               if(pos)\r
+               //Tree ctrl selection?\r
+               HTREEITEM hTree=m_RefTreeCtrl.GetSelectedItem();\r
+               if(hTree!=NULL)\r
                {\r
-                       CShadowTree* pTree=(CShadowTree*)m_ListRefLeafs.GetItemData(\r
-                                       m_ListRefLeafs.GetNextSelectedItem(pos));\r
-                       selectRef=pTree->GetRefName();\r
+                       CShadowTree* pTree=(CShadowTree*)m_RefTreeCtrl.GetItemData(hTree);\r
+                       return pTree->GetRefName();\r
                }\r
-               else\r
+       }\r
+       return CString();//None\r
+}\r
+\r
+void CBrowseRefsDlg::Refresh(CString selectRef)\r
+{\r
+//     m_RefMap.clear();\r
+//     g_Git.GetMapHashToFriendName(m_RefMap);\r
+               \r
+       if(!selectRef.IsEmpty())\r
+       {\r
+               if(selectRef == "HEAD")\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
+                       selectRef.Empty();\r
+                       g_Git.Run(L"git symbolic-ref HEAD",&selectRef,CP_UTF8);\r
+                       selectRef.Trim(L"\r\n\t ");\r
                }\r
        }\r
+       else\r
+       {\r
+               selectRef = GetSelectedRef(false);\r
+       }\r
 \r
        m_RefTreeCtrl.DeleteAllItems();\r
        m_ListRefLeafs.DeleteAllItems();\r
@@ -467,7 +480,8 @@ void CBrowseRefsDlg::ShowContextMenu(CPoint point, HTREEITEM hTreePos, VectorPSh
                if(pTree->IsFrom(L"refs/remotes"))\r
                {\r
 //                     popupMenu.AppendMenu(MF_STRING,eCmd_AddRemote,L"Add Remote");\r
-                       popupMenu.AppendMenu(MF_STRING,eCmd_ManageRemotes,L"Manage Remotes");\r
+                       if(!m_cmdPath.IsEmpty())\r
+                               popupMenu.AppendMenu(MF_STRING,eCmd_ManageRemotes,L"Manage Remotes");\r
                }\r
                else if(pTree->IsFrom(L"refs/heads"))\r
                        popupMenu.AppendMenu(MF_STRING,eCmd_CreateBranch,L"Create Branch");\r
@@ -623,3 +637,30 @@ void CBrowseRefsDlg::OnLvnColumnclickListRefLeafs(NMHDR *pNMHDR, LRESULT *pResul
        SetSortArrow(&m_ListRefLeafs,m_currSortCol,!m_currSortDesc);\r
 }\r
 \r
+void CBrowseRefsDlg::OnDestroy()\r
+{\r
+       m_pickedRef = GetSelectedRef(true);\r
+\r
+       CResizableStandAloneDialog::OnDestroy();\r
+}\r
+\r
+void CBrowseRefsDlg::OnNMDblclkListRefLeafs(NMHDR *pNMHDR, LRESULT *pResult)\r
+{\r
+       LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);\r
+       *pResult = 0;\r
+\r
+       EndDialog(IDOK);\r
+}\r
+\r
+CString CBrowseRefsDlg::PickRef(bool returnAsHash, CString initialRef)\r
+{\r
+       CBrowseRefsDlg dlg(CString(),NULL);\r
+       \r
+       dlg.m_initialRef = initialRef;\r
+\r
+       if(dlg.DoModal() != IDOK)\r
+               return CString();\r
+\r
+       return dlg.m_pickedRef;\r
+}\r
+\r
index 1f85df2..d139211 100644 (file)
@@ -78,7 +78,9 @@ public:
        afx_msg void OnBnClickedOk();\r
        virtual BOOL OnInitDialog();\r
 \r
-       void                    Refresh(bool bSelectCurHead=false);\r
+       CString                 GetSelectedRef(bool onlyIfLeaf);\r
+\r
+       void                    Refresh(CString selectRef = CString());\r
 \r
        CShadowTree&    GetTreeNode(CString refName, CShadowTree* pTreePos=NULL, bool bCreateIfNotExist=false);\r
 \r
@@ -111,4 +113,13 @@ public:
        void            ShowContextMenu(CPoint point, HTREEITEM hTreePos, VectorPShadowTree& selectedLeafs);\r
        virtual BOOL PreTranslateMessage(MSG* pMsg);\r
        afx_msg void OnLvnColumnclickListRefLeafs(NMHDR *pNMHDR, LRESULT *pResult);\r
+       afx_msg void OnDestroy();\r
+       afx_msg void OnNMDblclkListRefLeafs(NMHDR *pNMHDR, LRESULT *pResult);\r
+\r
+\r
+public:\r
+       CString m_initialRef;\r
+       CString m_pickedRef;\r
+\r
+       static CString  PickRef(bool returnAsHash = false, CString initialRef = CString()); \r
 };\r