Make broader use of alternative tool selection
[TortoiseGit.git] / src / TortoiseProc / RevisionGraph / RevisionGraphDlgFunc.cpp
blob21594cf71af44d8817fa15f9551c5d38f9a45e45
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2011 - TortoiseSVN
4 // Copyright (C) 2012-2016 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "TortoiseProc.h"
22 #include <gdiplus.h>
23 #include "Revisiongraphdlg.h"
24 #include "MessageBox.h"
25 #include "Git.h"
26 #include "TempFile.h"
27 #include "UnicodeUtils.h"
28 #include "TGitPath.h"
29 //#include "SVNInfo.h"
30 //#include ".\revisiongraphwnd.h"
31 //#include "CachedLogInfo.h"
32 //#include "RevisionGraph/IRevisionGraphLayout.h"
33 //#include "RevisionGraph/FullGraphBuilder.h"
34 //#include "RevisionGraph/FullGraphFinalizer.h"
35 //#include "RevisionGraph/VisibleGraphBuilder.h"
36 //#include "RevisionGraph/StandardLayout.h"
37 //#include "RevisionGraph/ShowWC.h"
38 //#include "RevisionGraph/ShowWCModification.h"
40 #ifdef _DEBUG
41 #define new DEBUG_NEW
42 #undef THIS_FILE
43 static char THIS_FILE[] = __FILE__;
44 #endif
46 using namespace Gdiplus;
47 using namespace ogdf;
49 void CRevisionGraphWnd::InitView()
51 m_bIsCanvasMove = false;
53 SetScrollbars();
56 void CRevisionGraphWnd::BuildPreview()
58 m_Preview.DeleteObject();
59 if (!m_bShowOverview)
60 return;
62 // is there a point in drawing this at all?
64 int nodeCount = this->m_Graph.numberOfNodes();
65 if ((nodeCount > REVGRAPH_PREVIEW_MAX_NODES) || (nodeCount == 0))
66 return;
68 float origZoom = m_fZoomFactor;
70 CRect clientRect = GetClientRect();
71 CSize preViewSize (max (REVGRAPH_PREVIEW_WIDTH, clientRect.Width() / 4)
72 ,max (REVGRAPH_PREVIEW_HEIGHT, clientRect.Height() / 4));
74 // zoom the graph so that it is completely visible in the window
75 CRect graphRect = GetGraphRect();
76 float horzfact = float(graphRect.Width())/float(preViewSize.cx);
77 float vertfact = float(graphRect.Height())/float(preViewSize.cy);
78 m_previewZoom = min (DEFAULT_ZOOM, 1.0f/(max(horzfact, vertfact)));
80 // make sure the preview window has a minimal size
82 m_previewWidth = (int)min (max (graphRect.Width() * m_previewZoom, 30), preViewSize.cx);
83 m_previewHeight = (int)min (max (graphRect.Height() * m_previewZoom, 30), preViewSize.cy);
85 CClientDC ddc(this);
86 CDC dc;
87 if (!dc.CreateCompatibleDC(&ddc))
88 return;
90 m_Preview.CreateCompatibleBitmap(&ddc, m_previewWidth, m_previewHeight);
91 HBITMAP oldbm = (HBITMAP)dc.SelectObject (m_Preview);
93 // paint the whole graph
94 DoZoom (m_previewZoom, false);
95 CRect rect (0, 0, m_previewWidth, m_previewHeight);
96 GraphicsDevice dev;
97 dev.pDC = &dc;
98 DrawGraph(dev, rect, 0, 0, true);
100 // now we have a bitmap the size of the preview window
101 dc.SelectObject(oldbm);
102 dc.DeleteDC();
104 DoZoom (origZoom, false);
107 void CRevisionGraphWnd::SetScrollbar (int bar, int newPos, int clientMax, int graphMax)
109 SCROLLINFO ScrollInfo = {sizeof(SCROLLINFO), SIF_ALL};
110 GetScrollInfo (bar, &ScrollInfo);
112 clientMax = max(1, clientMax);
113 int oldHeight = ScrollInfo.nMax <= 0 ? clientMax : ScrollInfo.nMax;
114 int newHeight = static_cast<int>(graphMax * m_fZoomFactor);
115 int maxPos = max (0, newHeight - clientMax);
116 int pos = min (maxPos, newPos >= 0
117 ? newPos
118 : ScrollInfo.nPos * newHeight / oldHeight);
120 ScrollInfo.nPos = pos;
121 ScrollInfo.nMin = 0;
122 ScrollInfo.nMax = newHeight;
123 ScrollInfo.nPage = clientMax;
124 ScrollInfo.nTrackPos = pos;
126 SetScrollInfo(bar, &ScrollInfo);
129 void CRevisionGraphWnd::SetScrollbars (int nVert, int nHorz)
131 CRect clientrect = GetClientRect();
132 const CRect& pRect = GetGraphRect();
134 SetScrollbar (SB_VERT, nVert, clientrect.Height(), pRect.Height());
135 SetScrollbar (SB_HORZ, nHorz, clientrect.Width(), pRect.Width());
138 CRect CRevisionGraphWnd::GetGraphRect()
140 return m_GraphRect;
143 CRect CRevisionGraphWnd::GetClientRect()
145 CRect clientRect;
146 CWnd::GetClientRect (&clientRect);
147 return clientRect;
150 CRect CRevisionGraphWnd::GetWindowRect()
152 CRect windowRect;
153 CWnd::GetWindowRect (&windowRect);
154 return windowRect;
157 CRect CRevisionGraphWnd::GetViewRect()
159 CRect result;
160 result.UnionRect (GetClientRect(), GetGraphRect());
161 return result;
164 int CRevisionGraphWnd::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
166 UINT num = 0; // number of image encoders
167 UINT size = 0; // size of the image encoder array in bytes
169 if (GetImageEncodersSize(&num, &size)!=Ok)
170 return -1;
171 if(size == 0)
172 return -1; // Failure
174 ImageCodecInfo* pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
175 if (!pImageCodecInfo)
176 return -1; // Failure
178 if (GetImageEncoders(num, size, pImageCodecInfo)==Ok)
180 for(UINT j = 0; j < num; ++j)
182 if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
184 *pClsid = pImageCodecInfo[j].Clsid;
185 free(pImageCodecInfo);
186 return j; // Success
191 free(pImageCodecInfo);
192 return -1; // Failure
195 bool CRevisionGraphWnd::FetchRevisionData
196 ( const CString& /*path*/
197 , CProgressDlg* /*progress*/
198 , ITaskbarList3 * /*pTaskbarList*/
199 , HWND /*hWnd*/)
201 this->m_LogCache.ClearAllParent();
202 this->m_logEntries.ClearAll();
203 CString range;
204 if (!m_ToRev.IsEmpty() && !m_FromRev.IsEmpty())
205 range.Format(_T("%s..%s"), (LPCTSTR)g_Git.FixBranchName(m_FromRev), (LPCTSTR)g_Git.FixBranchName(m_ToRev));
206 else if (!m_ToRev.IsEmpty())
207 range = m_ToRev;
208 else if (!m_FromRev.IsEmpty())
209 range = m_FromRev;
210 DWORD infomask = CGit::LOG_INFO_SIMPILFY_BY_DECORATION | (m_bCurrentBranch ? 0 : m_bLocalBranches ? CGit::LOG_INFO_LOCAL_BRANCHES : CGit::LOG_INFO_ALL_BRANCH);
211 m_logEntries.ParserFromLog(nullptr, 0, infomask, &range);
213 ReloadHashMap();
214 this->m_Graph.clear();
216 CArray<node> nodes;
217 GraphicsDevice dev;
218 dev.pDC = this->GetDC();
219 dev.graphics = Graphics::FromHDC(dev.pDC->m_hDC);
220 dev.graphics->SetPageUnit (UnitPixel);
222 m_HeadNode = nullptr;
224 for (size_t i = 0; i < m_logEntries.size(); ++i)
226 node nd;
227 nd = this->m_Graph.newNode();
228 nodes.Add(nd);
229 m_GraphAttr.width(nd)=100;
230 m_GraphAttr.height(nd)=20;
231 SetNodeRect(dev, &nd, m_logEntries[i], 0);
232 if (m_logEntries[i] == m_HeadHash)
233 m_HeadNode = nd;
236 for (size_t i = 0; i < m_logEntries.size(); ++i)
238 GitRev rev=m_logEntries.GetGitRevAt(i);
239 for (size_t j = 0; j < rev.m_ParentHash.size(); ++j)
241 if(m_logEntries.m_HashMap.find(rev.m_ParentHash[j]) == m_logEntries.m_HashMap.end())
243 TRACE(_T("Can't found parent node"));
244 //new parent node as new node
245 node nd;
246 nd = this->m_Graph.newNode();
247 m_Graph.newEdge(nodes[i], nd);
248 m_logEntries.push_back(rev.m_ParentHash[j]);
249 m_logEntries.m_HashMap[rev.m_ParentHash[j]] = (int)m_logEntries.size() -1;
250 nodes.Add(nd);
251 SetNodeRect(dev, &nd, rev.m_ParentHash[j], 0);
253 }else
255 TRACE(_T("edge %d - %d\n"),i, m_logEntries.m_HashMap[rev.m_ParentHash[j]]);
256 m_Graph.newEdge(nodes[i], nodes[m_logEntries.m_HashMap[rev.m_ParentHash[j]]]);
261 //this->m_OHL.layerDistance(30.0);
262 //this->m_OHL.nodeDistance(25.0);
263 //this->m_OHL.weightBalancing(0.8);
265 m_SugiyamLayout.call(m_GraphAttr);
267 node v;
268 double xmax = 0;
269 double ymax = 0;
270 forall_nodes(v,m_Graph)
272 double x = m_GraphAttr.x(v) + m_GraphAttr.width(v)/2;
273 double y = m_GraphAttr.y(v) + m_GraphAttr.height(v)/2;
274 if(x>xmax)
275 xmax = x;
276 if(y>ymax)
277 ymax = y;
280 this->m_GraphRect.top=m_GraphRect.left=0;
281 m_GraphRect.bottom = (LONG)ymax;
282 m_GraphRect.right = (LONG)xmax;
284 return true;
287 bool CRevisionGraphWnd::AnalyzeRevisionData()
289 #if 0
290 CSyncPointer<const CFullGraph> fullGraph (m_state.GetFullGraph());
291 if (fullGraph.get() != nullptr && (fullGraph->GetNodeCount() > 0))
293 // filter graph
295 CSyncPointer<CAllRevisionGraphOptions> options (m_state.GetOptions());
296 options->Prepare();
298 std::unique_ptr<CVisibleGraph> visibleGraph (new CVisibleGraph());
299 CVisibleGraphBuilder builder ( *fullGraph
300 , *visibleGraph
301 , options->GetCopyFilterOptions());
302 builder.Run();
303 options->GetModificationOptions().Apply (visibleGraph.get());
305 index_t index = 0;
306 for (size_t i = 0, count = visibleGraph->GetRootCount(); i < count; ++i)
307 index = visibleGraph->GetRoot (i)->InitIndex (index);
309 // layout nodes
311 std::unique_ptr<CStandardLayout> newLayout
312 ( new CStandardLayout ( m_state.GetFullHistory()->GetCache()
313 , visibleGraph.get()
314 , m_state.GetFullHistory()->GetWCInfo()));
315 options->GetLayoutOptions().Apply (newLayout.get());
316 newLayout->Finalize();
318 // switch state
320 m_state.SetAnalysisResult (visibleGraph, newLayout);
323 return m_state.GetNodes().get() != nullptr;
324 #endif
325 return true;
328 bool CRevisionGraphWnd::IsUpdateJobRunning() const
330 return (updateJob.get() != nullptr) && !updateJob->IsDone();
333 bool CRevisionGraphWnd::GetShowOverview() const
335 return m_bShowOverview;
338 void CRevisionGraphWnd::SetShowOverview (bool value)
340 m_bShowOverview = value;
341 if (m_bShowOverview)
342 BuildPreview();
344 #if 0
345 void CRevisionGraphWnd::GetSelected
346 ( const CVisibleGraphNode* node
347 , bool head
348 , CTGitPath& path
349 , GitRev& rev
350 , GitRev& peg)
352 CString repoRoot = m_state.GetRepositoryRoot();
354 // get path and revision
356 path.SetFromSVN (repoRoot + CUnicodeUtils::GetUnicode (node->GetPath().GetPath().c_str()));
357 rev = head ? GitRev::REV_HEAD : node->GetRevision();
359 // handle 'modified WC' node
361 if (node->GetClassification().Is (CNodeClassification::IS_MODIFIED_WC))
363 path.SetFromUnknown (m_sPath);
364 rev = GitRev::REV_WC;
366 // don't set peg, if we aren't the first node
367 // (i.e. would not be valid for node1)
369 if (node == m_SelectedEntry1)
370 peg = GitRev::REV_WC;
372 else
374 // set head, if still necessary
376 if (head && !peg.IsValid())
377 peg = node->GetRevision();
380 #endif
382 CString CRevisionGraphWnd::GetFriendRefName(ogdf::node v)
384 if (!v)
385 return CString();
386 CGitHash hash = this->m_logEntries[v->index()];
387 if(this->m_HashMap.find(hash) == m_HashMap.end())
388 return hash.ToString();
389 else if (m_HashMap[hash].empty())
390 return hash.ToString();
391 else if(this->m_HashMap[hash][0].IsEmpty())
392 return hash.ToString();
393 else
394 return m_HashMap[hash][0];
397 STRING_VECTOR CRevisionGraphWnd::GetFriendRefNames(ogdf::node v, CGit::REF_TYPE *refTypes, int refTypeCount)
399 if (!v)
400 return STRING_VECTOR();
401 CGitHash hash = m_logEntries[v->index()];
402 if (m_HashMap.find(hash) == m_HashMap.end())
403 return STRING_VECTOR();
404 else if (m_HashMap[hash].empty())
405 return STRING_VECTOR();
406 else if (m_HashMap[hash][0].IsEmpty())
407 return STRING_VECTOR();
408 else
410 STRING_VECTOR &all = m_HashMap[hash];
411 STRING_VECTOR list;
412 for (size_t i = 0; i < all.size(); ++i)
414 CGit::REF_TYPE refType;
415 CString shortName = CGit::GetShortName(all[i], &refType);
416 if (!refTypes)
417 list.push_back(shortName);
418 else
420 for (int j = 0; j < refTypeCount; ++j)
421 if (refTypes[j] == refType)
422 list.push_back(shortName);
425 return list;
429 void CRevisionGraphWnd::CompareRevs(const CString& revTo)
431 ASSERT(m_SelectedEntry1);
432 ASSERT(!revTo.IsEmpty() || m_SelectedEntry2);
434 bool alternativeTool = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
436 CString sCmd;
438 sCmd.Format(_T("/command:showcompare %s /revision1:%s /revision2:%s"),
439 this->m_sPath.IsEmpty() ? _T("") : (LPCTSTR)(_T("/path:\"") + this->m_sPath + _T("\"")),
440 (LPCTSTR)GetFriendRefName(m_SelectedEntry1),
441 !revTo.IsEmpty() ? (LPCTSTR)revTo : (LPCTSTR)GetFriendRefName(m_SelectedEntry2));
443 if (alternativeTool)
444 sCmd += L" /alternative";
446 CAppUtils::RunTortoiseGitProc(sCmd);
449 void CRevisionGraphWnd::UnifiedDiffRevs(bool bHead)
451 ASSERT(m_SelectedEntry1);
452 ASSERT(bHead || m_SelectedEntry2);
454 bool alternativeTool = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
455 CAppUtils::StartShowUnifiedDiff(m_hWnd, CString(), GetFriendRefName(m_SelectedEntry1), CString(),
456 bHead? _T("HEAD"):GetFriendRefName(m_SelectedEntry2),
457 alternativeTool);
460 void CRevisionGraphWnd::DoZoom (float fZoomFactor, bool updateScrollbars)
462 float oldzoom = m_fZoomFactor;
463 m_fZoomFactor = fZoomFactor;
465 m_nFontSize = max(1, int(DEFAULT_ZOOM_FONT * fZoomFactor));
466 if (m_nFontSize < SMALL_ZOOM_FONT_THRESHOLD)
467 m_nFontSize = min (SMALL_ZOOM_FONT_THRESHOLD, int(SMALL_ZOOM_FONT * fZoomFactor));
469 for (int i = 0; i < MAXFONTS; ++i)
471 if (m_apFonts[i])
473 m_apFonts[i]->DeleteObject();
474 delete m_apFonts[i];
476 m_apFonts[i] = nullptr;
479 if (updateScrollbars)
481 SCROLLINFO si1 = {sizeof(SCROLLINFO), SIF_ALL};
482 GetScrollInfo(SB_VERT, &si1);
483 SCROLLINFO si2 = {sizeof(SCROLLINFO), SIF_ALL};
484 GetScrollInfo(SB_HORZ, &si2);
486 InitView();
488 si1.nPos = int(float(si1.nPos)*m_fZoomFactor/oldzoom);
489 si2.nPos = int(float(si2.nPos)*m_fZoomFactor/oldzoom);
490 SetScrollPos (SB_VERT, si1.nPos);
491 SetScrollPos (SB_HORZ, si2.nPos);
494 Invalidate (FALSE);