Revision Graph: Add filter to walk from Only Local Branches
[TortoiseGit.git] / src / TortoiseProc / RevisionGraph / RevisionGraphWnd.cpp
blob94513761b11587cfdd28228a881c79e2e4b5dd5b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2012 - TortoiseSVN
4 // Copyright (C) 2012-2014 - 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 "Revisiongraphwnd.h"
23 #include "MessageBox.h"
24 #include "Git.h"
25 #include "AppUtils.h"
26 #include "PathUtils.h"
27 #include "StringUtils.h"
28 #include "TempFile.h"
29 #include "UnicodeUtils.h"
30 #include "TGitPath.h"
31 #include "RevisionGraphDlg.h"
32 #include "BrowseFolder.h"
33 #include "GitProgressDlg.h"
34 #include "ChangedDlg.h"
35 //#include "RevisionGraph/StandardLayout.h"
36 //#include "RevisionGraph/UpsideDownLayout.h"
37 #include "SysInfo.h"
38 #include "FormatMessageWrapper.h"
40 #pragma warning(push)
41 #pragma warning(disable: 4100) // unreferenced formal parameter
42 #include <ogdf/planarity/PlanarizationLayout.h>
43 #include <ogdf/planarity/VariableEmbeddingInserter.h>
44 #include <ogdf/planarity/FastPlanarSubgraph.h>
45 #include <ogdf/orthogonal/OrthoLayout.h>
46 #include <ogdf/planarity/EmbedderMinDepthMaxFaceLayers.h>
47 #pragma warning(pop)
49 #ifdef _DEBUG
50 #define new DEBUG_NEW
51 #undef THIS_FILE
52 static char THIS_FILE[] = __FILE__;
53 #endif
55 using namespace Gdiplus;
56 using namespace ogdf;
58 enum RevisionGraphContextMenuCommands
60 // needs to start with 1, since 0 is the return value if *nothing* is clicked on in the context menu
61 GROUP_MASK = 0xff00,
62 ID_SHOWLOG = 1,
63 ID_CFM = 2,
64 ID_BROWSEREPO,
65 ID_COMPAREREVS = 0x100,
66 ID_COMPAREHEADS,
67 ID_COMPAREWT,
68 ID_UNIDIFFREVS,
69 ID_UNIDIFFHEADS,
70 ID_MERGETO = 0x300,
71 ID_UPDATE,
72 ID_SWITCHTOHEAD,
73 ID_SWITCH,
74 ID_COPYREFS = 0x400,
75 ID_EXPAND_ALL = 0x500,
76 ID_JOIN_ALL,
77 ID_GRAPH_EXPANDCOLLAPSE_ABOVE = 0x600,
78 ID_GRAPH_EXPANDCOLLAPSE_RIGHT,
79 ID_GRAPH_EXPANDCOLLAPSE_BELOW,
80 ID_GRAPH_SPLITJOIN_ABOVE,
81 ID_GRAPH_SPLITJOIN_RIGHT,
82 ID_GRAPH_SPLITJOIN_BELOW,
85 CRevisionGraphWnd::CRevisionGraphWnd()
86 : CWnd()
87 , m_SelectedEntry1(NULL)
88 , m_SelectedEntry2(NULL)
89 , m_HeadNode(nullptr)
90 , m_pDlgTip(NULL)
91 , m_nFontSize(12)
92 , m_bTweakTrunkColors(true)
93 , m_bTweakTagsColors(true)
94 , m_fZoomFactor(DEFAULT_ZOOM)
95 , m_ptRubberEnd(0,0)
96 , m_ptMoveCanvas(0,0)
97 , m_bShowOverview(false)
98 , m_parent (NULL)
99 , m_hoverIndex (NULL)
100 , m_hoverGlyphs (0)
101 , m_tooltipIndex(nullptr)
102 , m_showHoverGlyphs (false)
103 , m_bIsCanvasMove(false)
104 , m_previewWidth(0)
105 , m_previewHeight(0)
106 , m_previewZoom(1)
107 , m_dwTicks(0)
108 , m_logEntries(&m_LogCache)
109 , m_bCurrentBranch(false)
110 , m_bLocalBranches(FALSE)
112 memset(&m_lfBaseFont, 0, sizeof(LOGFONT));
113 std::fill_n(m_apFonts, MAXFONTS, (CFont*)NULL);
115 WNDCLASS wndcls;
116 HINSTANCE hInst = AfxGetInstanceHandle();
117 #define REVGRAPH_CLASSNAME _T("Revgraph_windowclass")
118 if (!(::GetClassInfo(hInst, REVGRAPH_CLASSNAME, &wndcls)))
120 // otherwise we need to register a new class
121 wndcls.style = CS_DBLCLKS | CS_OWNDC;
122 wndcls.lpfnWndProc = ::DefWindowProc;
123 wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
124 wndcls.hInstance = hInst;
125 wndcls.hIcon = NULL;
126 wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
127 wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
128 wndcls.lpszMenuName = NULL;
129 wndcls.lpszClassName = REVGRAPH_CLASSNAME;
131 RegisterClass(&wndcls);
134 m_bTweakTrunkColors = CRegDWORD(_T("Software\\TortoiseGit\\RevisionGraph\\TweakTrunkColors"), TRUE) != FALSE;
135 m_bTweakTagsColors = CRegDWORD(_T("Software\\TortoiseGit\\RevisionGraph\\TweakTagsColors"), TRUE) != FALSE;
136 m_szTip[0] = 0;
137 m_wszTip[0] = 0;
139 m_GraphAttr.init(this->m_Graph, ogdf::GraphAttributes::nodeGraphics | ogdf::GraphAttributes::edgeGraphics |
140 ogdf:: GraphAttributes::nodeLabel | ogdf::GraphAttributes::nodeColor |
141 ogdf::GraphAttributes::edgeColor | ogdf::GraphAttributes::edgeStyle |
142 ogdf::GraphAttributes::nodeStyle | ogdf::GraphAttributes::nodeTemplate);
144 m_SugiyamLayout.setRanking(::new ogdf::OptimalRanking());
145 m_SugiyamLayout.setCrossMin(::new ogdf::MedianHeuristic());
147 double pi = 3.1415926;
148 m_ArrowCos = cos(pi/8);
149 m_ArrowSin = sin(pi/8);
150 this->m_ArrowSize = 8;
151 #if 0
152 ogdf::node one = this->m_Graph.newNode();
153 ogdf::node two = this->m_Graph.newNode();
154 ogdf::node three = this->m_Graph.newNode();
155 ogdf::node four = this->m_Graph.newNode();
158 m_GraphAttr.width(one)=100;
159 m_GraphAttr.height(one)=200;
160 m_GraphAttr.width(two)=100;
161 m_GraphAttr.height(two)=100;
162 m_GraphAttr.width(three)=100;
163 m_GraphAttr.height(three)=20;
164 m_GraphAttr.width(four)=100;
165 m_GraphAttr.height(four)=20;
167 m_GraphAttr.labelNode(one)="One";
168 m_GraphAttr.labelNode(two)="Two";
169 m_GraphAttr.labelNode(three)="three";
171 this->m_Graph.newEdge(one, two);
172 this->m_Graph.newEdge(one, three);
173 this->m_Graph.newEdge(two, four);
174 this->m_Graph.newEdge(three, four);
176 #endif
177 FastHierarchyLayout *pOHL = ::new FastHierarchyLayout;
178 //It will auto delte when m_SugiyamLayout destory
180 pOHL->layerDistance(30.0);
181 pOHL->nodeDistance(25.0);
183 m_SugiyamLayout.setLayout(pOHL);
185 #if 0
186 //this->m_OHL.layerDistance(30.0);
187 //this->m_OHL.nodeDistance(25.0);
188 //this->m_OHL.weightBalancing(0.8);
189 m_SugiyamLayout.setLayout(&m_OHL);
190 m_SugiyamLayout.call(m_GraphAttr);
191 #endif
192 #if 0
193 PlanarizationLayout pl;
195 FastPlanarSubgraph *ps = ::new FastPlanarSubgraph;
196 ps->runs(100);
197 VariableEmbeddingInserter *ves = ::new VariableEmbeddingInserter;
198 ves->removeReinsert(EdgeInsertionModule::rrAll);
199 pl.setSubgraph(ps);
200 pl.setInserter(ves);
202 EmbedderMinDepthMaxFaceLayers *emb = ::new EmbedderMinDepthMaxFaceLayers;
203 pl.setEmbedder(emb);
205 OrthoLayout *ol =::new OrthoLayout;
206 ol->separation(20.0);
207 ol->cOverhang(0.4);
208 ol->setOptions(2+4);
209 ol->preferedDir(OrthoDir::odEast);
210 pl.setPlanarLayouter(ol);
212 pl.call(m_GraphAttr);
214 node v;
215 forall_nodes(v,m_Graph) {
217 TRACE(_T("node x %f y %f %f %f\n"),/* m_GraphAttr.idNode(v), */
218 m_GraphAttr.x(v),
219 m_GraphAttr.y(v),
220 m_GraphAttr.width(v),
221 m_GraphAttr.height(v)
225 edge e;
226 forall_edges(e, m_Graph)
228 // get connection and point position
229 const DPolyline &dpl = this->m_GraphAttr.bends(e);
231 ListConstIterator<DPoint> it;
232 for(it = dpl.begin(); it.valid(); ++it)
234 TRACE(_T("edge %f %f\n"), (*it).m_x, (*it).m_y);
237 m_GraphAttr.writeGML("test.gml");
238 #endif
241 CRevisionGraphWnd::~CRevisionGraphWnd()
243 for (int i = 0; i < MAXFONTS; ++i)
245 if (m_apFonts[i] != NULL)
247 m_apFonts[i]->DeleteObject();
248 delete m_apFonts[i];
250 m_apFonts[i] = NULL;
252 delete m_pDlgTip;
253 m_Graph.clear();
256 void CRevisionGraphWnd::DoDataExchange(CDataExchange* pDX)
258 CWnd::DoDataExchange(pDX);
262 BEGIN_MESSAGE_MAP(CRevisionGraphWnd, CWnd)
263 ON_WM_PAINT()
264 ON_WM_ERASEBKGND()
265 ON_WM_HSCROLL()
266 ON_WM_VSCROLL()
267 ON_WM_SIZE()
268 ON_WM_LBUTTONDOWN()
269 ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
270 ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
271 ON_WM_MOUSEWHEEL()
272 ON_WM_MOUSEHWHEEL()
273 ON_WM_CONTEXTMENU()
274 ON_WM_MOUSEMOVE()
275 ON_WM_LBUTTONUP()
276 ON_WM_SETCURSOR()
277 ON_WM_TIMER()
278 ON_MESSAGE(WM_WORKERTHREADDONE,OnWorkerThreadDone)
279 ON_WM_CAPTURECHANGED()
280 END_MESSAGE_MAP()
282 void CRevisionGraphWnd::Init(CWnd * pParent, LPRECT rect)
284 WNDCLASS wndcls;
285 HINSTANCE hInst = AfxGetInstanceHandle();
286 #define REVGRAPH_CLASSNAME _T("Revgraph_windowclass")
287 if (!(::GetClassInfo(hInst, REVGRAPH_CLASSNAME, &wndcls)))
289 // otherwise we need to register a new class
290 wndcls.style = CS_DBLCLKS | CS_OWNDC;
291 wndcls.lpfnWndProc = ::DefWindowProc;
292 wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
293 wndcls.hInstance = hInst;
294 wndcls.hIcon = NULL;
295 wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
296 wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
297 wndcls.lpszMenuName = NULL;
298 wndcls.lpszClassName = REVGRAPH_CLASSNAME;
300 RegisterClass(&wndcls);
303 if (!IsWindow(m_hWnd))
304 CreateEx(WS_EX_CLIENTEDGE, REVGRAPH_CLASSNAME, _T("RevGraph"), WS_CHILD|WS_VISIBLE|WS_TABSTOP, *rect, pParent, 0);
305 m_pDlgTip = new CToolTipCtrl;
306 if(!m_pDlgTip->Create(this))
308 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Unable to add tooltip!\n");
310 EnableToolTips();
312 memset(&m_lfBaseFont, 0, sizeof(m_lfBaseFont));
313 m_lfBaseFont.lfHeight = 0;
314 m_lfBaseFont.lfWeight = FW_NORMAL;
315 m_lfBaseFont.lfItalic = FALSE;
316 m_lfBaseFont.lfCharSet = DEFAULT_CHARSET;
317 m_lfBaseFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
318 m_lfBaseFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
319 m_lfBaseFont.lfQuality = DEFAULT_QUALITY;
320 m_lfBaseFont.lfPitchAndFamily = DEFAULT_PITCH;
322 m_dwTicks = GetTickCount();
324 m_parent = dynamic_cast<CRevisionGraphDlg*>(pParent);
327 CPoint CRevisionGraphWnd::GetLogCoordinates (CPoint point) const
329 // translate point into logical coordinates
331 int nVScrollPos = GetScrollPos(SB_VERT);
332 int nHScrollPos = GetScrollPos(SB_HORZ);
334 return CPoint ( (int)((point.x + nHScrollPos) / m_fZoomFactor)
335 , (int)((point.y + nVScrollPos) / m_fZoomFactor));
338 node CRevisionGraphWnd::GetHitNode (CPoint point, CSize /*border*/) const
340 #if 0
341 // any nodes at all?
343 CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
344 if (!nodeList)
345 return index_t(NO_INDEX);
347 // search the nodes for one at that grid position
349 return nodeList->GetAt (GetLogCoordinates (point), border);
350 #endif
352 node v;
353 forall_nodes(v,m_Graph)
355 RectF noderect (GetNodeRect (v, CPoint(GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT))));
356 if(point.x>noderect.X && point.x <(noderect.X+noderect.Width) &&
357 point.y>noderect.Y && point.y <(noderect.Y+noderect.Height))
359 return v;
362 return NULL;
365 DWORD CRevisionGraphWnd::GetHoverGlyphs (CPoint /*point*/) const
367 // if there is no layout, there will be no nodes,
368 // hence, no glyphs
369 DWORD result = 0;
370 #if 0
371 CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
372 if (!nodeList)
373 return 0;
375 // get node at point or node that is close enough
376 // so that point may hit a glyph area
378 index_t nodeIndex = GetHitNode(point);
379 if (nodeIndex == NO_INDEX)
380 nodeIndex = GetHitNode(point, CSize (GLYPH_SIZE, GLYPH_SIZE / 2));
382 if (nodeIndex >= nodeList->GetCount())
383 return 0;
385 ILayoutNodeList::SNode node = nodeList->GetNode (nodeIndex);
386 const CVisibleGraphNode* base = node.node;
388 // what glyphs should be shown depending on position of point
389 // relative to the node rect?
391 CPoint logCoordinates = GetLogCoordinates (point);
392 CRect r = node.rect;
393 CPoint center = r.CenterPoint();
395 CRect rightGlyphArea ( r.right - GLYPH_SIZE, center.y - GLYPH_SIZE / 2
396 , r.right + GLYPH_SIZE, center.y + GLYPH_SIZE / 2);
397 CRect topGlyphArea ( center.x - GLYPH_SIZE, r.top - GLYPH_SIZE / 2
398 , center.x + GLYPH_SIZE, r.top + GLYPH_SIZE / 2);
399 CRect bottomGlyphArea ( center.x - GLYPH_SIZE, r.bottom - GLYPH_SIZE / 2
400 , center.x + GLYPH_SIZE, r.bottom + GLYPH_SIZE / 2);
402 bool upsideDown
403 = m_state.GetOptions()->GetOption<CUpsideDownLayout>()->IsActive();
405 if (upsideDown)
407 std::swap (topGlyphArea.top, bottomGlyphArea.top);
408 std::swap (topGlyphArea.bottom, bottomGlyphArea.bottom);
412 if (rightGlyphArea.PtInRect (logCoordinates))
413 result = base->GetFirstCopyTarget() != NULL
414 ? CGraphNodeStates::COLLAPSED_RIGHT | CGraphNodeStates::SPLIT_RIGHT
415 : 0;
417 if (topGlyphArea.PtInRect (logCoordinates))
418 result = base->GetSource() != NULL
419 ? CGraphNodeStates::COLLAPSED_ABOVE | CGraphNodeStates::SPLIT_ABOVE
420 : 0;
422 if (bottomGlyphArea.PtInRect (logCoordinates))
423 result = base->GetNext() != NULL
424 ? CGraphNodeStates::COLLAPSED_BELOW | CGraphNodeStates::SPLIT_BELOW
425 : 0;
427 // if some nodes have already been split, don't allow collapsing etc.
429 CSyncPointer<const CGraphNodeStates> nodeStates (m_state.GetNodeStates());
430 if (result & nodeStates->GetFlags (base))
431 result = 0;
432 #endif
433 return result;
435 #if 0
436 const CRevisionGraphState::SVisibleGlyph* CRevisionGraphWnd::GetHitGlyph (CPoint point) const
438 float glyphSize = GLYPH_SIZE * m_fZoomFactor;
440 CSyncPointer<const CRevisionGraphState::TVisibleGlyphs>
441 visibleGlyphs (m_state.GetVisibleGlyphs());
443 for (size_t i = 0, count = visibleGlyphs->size(); i < count; ++i)
445 const CRevisionGraphState::SVisibleGlyph* entry = &(*visibleGlyphs)[i];
447 float xRel = point.x - entry->leftTop.X;
448 float yRel = point.y - entry->leftTop.Y;
450 if ( (xRel >= 0) && (xRel < glyphSize)
451 && (yRel >= 0) && (yRel < glyphSize))
453 return entry;
457 return NULL;
459 #endif
460 void CRevisionGraphWnd::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
462 SCROLLINFO sinfo = {0};
463 sinfo.cbSize = sizeof(SCROLLINFO);
464 GetScrollInfo(SB_HORZ, &sinfo);
466 // Determine the new position of scroll box.
467 switch (nSBCode)
469 case SB_LEFT: // Scroll to far left.
470 sinfo.nPos = sinfo.nMin;
471 break;
472 case SB_RIGHT: // Scroll to far right.
473 sinfo.nPos = sinfo.nMax;
474 break;
475 case SB_ENDSCROLL: // End scroll.
476 break;
477 case SB_LINELEFT: // Scroll left.
478 if (sinfo.nPos > sinfo.nMin)
479 sinfo.nPos--;
480 break;
481 case SB_LINERIGHT: // Scroll right.
482 if (sinfo.nPos < sinfo.nMax)
483 ++sinfo.nPos;
484 break;
485 case SB_PAGELEFT: // Scroll one page left.
487 if (sinfo.nPos > sinfo.nMin)
488 sinfo.nPos = max(sinfo.nMin, sinfo.nPos - (int) sinfo.nPage);
490 break;
491 case SB_PAGERIGHT: // Scroll one page right.
493 if (sinfo.nPos < sinfo.nMax)
494 sinfo.nPos = min(sinfo.nMax, sinfo.nPos + (int) sinfo.nPage);
496 break;
497 case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
498 sinfo.nPos = sinfo.nTrackPos; // of the scroll box at the end of the drag operation.
499 break;
500 case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
501 sinfo.nPos = sinfo.nTrackPos; // position that the scroll box has been dragged to.
502 break;
504 SetScrollInfo(SB_HORZ, &sinfo);
505 Invalidate (FALSE);
506 __super::OnHScroll(nSBCode, nPos, pScrollBar);
509 void CRevisionGraphWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
511 SCROLLINFO sinfo = {0};
512 sinfo.cbSize = sizeof(SCROLLINFO);
513 GetScrollInfo(SB_VERT, &sinfo);
515 // Determine the new position of scroll box.
516 switch (nSBCode)
518 case SB_LEFT: // Scroll to far left.
519 sinfo.nPos = sinfo.nMin;
520 break;
521 case SB_RIGHT: // Scroll to far right.
522 sinfo.nPos = sinfo.nMax;
523 break;
524 case SB_ENDSCROLL: // End scroll.
525 break;
526 case SB_LINELEFT: // Scroll left.
527 if (sinfo.nPos > sinfo.nMin)
528 sinfo.nPos--;
529 break;
530 case SB_LINERIGHT: // Scroll right.
531 if (sinfo.nPos < sinfo.nMax)
532 ++sinfo.nPos;
533 break;
534 case SB_PAGELEFT: // Scroll one page left.
536 if (sinfo.nPos > sinfo.nMin)
537 sinfo.nPos = max(sinfo.nMin, sinfo.nPos - (int) sinfo.nPage);
539 break;
540 case SB_PAGERIGHT: // Scroll one page right.
542 if (sinfo.nPos < sinfo.nMax)
543 sinfo.nPos = min(sinfo.nMax, sinfo.nPos + (int) sinfo.nPage);
545 break;
546 case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
547 sinfo.nPos = sinfo.nTrackPos; // of the scroll box at the end of the drag operation.
548 break;
549 case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
550 sinfo.nPos = sinfo.nTrackPos; // position that the scroll box has been dragged to.
551 break;
553 SetScrollInfo(SB_VERT, &sinfo);
554 Invalidate(FALSE);
555 __super::OnVScroll(nSBCode, nPos, pScrollBar);
558 void CRevisionGraphWnd::OnSize(UINT nType, int cx, int cy)
560 __super::OnSize(nType, cx, cy);
561 SetScrollbars(GetScrollPos(SB_VERT), GetScrollPos(SB_HORZ));
562 Invalidate(FALSE);
565 void CRevisionGraphWnd::OnLButtonDown(UINT nFlags, CPoint point)
568 if (IsUpdateJobRunning())
569 return __super::OnLButtonDown(nFlags, point);
571 // CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
573 SetFocus();
574 bool bHit = false;
575 bool bControl = !!(GetKeyState(VK_CONTROL)&0x8000);
576 bool bOverview = m_bShowOverview && m_OverviewRect.PtInRect(point);
577 if (! bOverview)
579 #if 0
580 const CRevisionGraphState::SVisibleGlyph* hitGlyph
581 = GetHitGlyph (point);
583 if (hitGlyph != NULL)
585 ToggleNodeFlag (hitGlyph->node, hitGlyph->state);
586 return __super::OnLButtonDown(nFlags, point);
588 #endif
589 node nodeIndex = GetHitNode (point);
590 if (nodeIndex != NULL)
592 if (bControl)
594 if (m_SelectedEntry1 == nodeIndex)
596 if (m_SelectedEntry2)
598 m_SelectedEntry1 = m_SelectedEntry2;
599 m_SelectedEntry2 = NULL;
601 else
602 m_SelectedEntry1 = NULL;
604 else if (m_SelectedEntry2 == nodeIndex)
605 m_SelectedEntry2 = NULL;
606 else if (m_SelectedEntry1)
607 m_SelectedEntry2 = nodeIndex;
608 else
609 m_SelectedEntry1 = nodeIndex;
611 else
613 if (m_SelectedEntry1 == nodeIndex)
614 m_SelectedEntry1 = NULL;
615 else
616 m_SelectedEntry1 = nodeIndex;
617 m_SelectedEntry2 = NULL;
619 bHit = true;
620 Invalidate(FALSE);
624 if ((!bHit)&&(!bControl)&&(!bOverview))
626 m_SelectedEntry1 = NULL;
627 m_SelectedEntry2 = NULL;
628 m_bIsCanvasMove = true;
629 Invalidate(FALSE);
630 if (m_bShowOverview && m_OverviewRect.PtInRect(point))
631 m_bIsCanvasMove = false;
633 m_ptMoveCanvas = point;
635 UINT uEnable = MF_BYCOMMAND;
636 if ((m_SelectedEntry1 != NULL)&&(m_SelectedEntry2 != NULL))
637 uEnable |= MF_ENABLED;
638 else
639 uEnable |= MF_GRAYED;
641 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_COMPAREREVISIONS, uEnable);
642 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_UNIFIEDDIFF, uEnable);
644 uEnable = MF_BYCOMMAND;
645 if ((m_SelectedEntry1 != NULL)&&(m_SelectedEntry2 == NULL))
646 uEnable |= MF_ENABLED;
647 else
648 uEnable |= MF_GRAYED;
650 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_UNIFIEDDIFFOFHEADREVISIONS, uEnable);
651 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_COMPAREHEADREVISIONS, uEnable);
653 __super::OnLButtonDown(nFlags, point);
656 void CRevisionGraphWnd::OnCaptureChanged(CWnd *pWnd)
658 __super::OnCaptureChanged(pWnd);
661 void CRevisionGraphWnd::OnLButtonUp(UINT nFlags, CPoint point)
663 if (!m_bIsCanvasMove)
664 return; // we don't have a rubberband, so no zooming necessary
666 m_bIsCanvasMove = false;
667 ReleaseCapture();
668 if (IsUpdateJobRunning())
669 return __super::OnLButtonUp(nFlags, point);
671 // zooming is finished
672 m_ptRubberEnd = CPoint(0,0);
673 CRect rect = GetClientRect();
674 int x = abs(m_ptMoveCanvas.x - point.x);
675 int y = abs(m_ptMoveCanvas.y - point.y);
677 if ((x < 20)&&(y < 20))
679 // too small zoom rectangle
680 // assume zooming by accident
681 Invalidate(FALSE);
682 __super::OnLButtonUp(nFlags, point);
683 return;
686 float xfact = float(rect.Width())/float(x);
687 float yfact = float(rect.Height())/float(y);
688 float fact = max(yfact, xfact);
690 // find out where to scroll to
691 x = min(m_ptMoveCanvas.x, point.x) + GetScrollPos(SB_HORZ);
692 y = min(m_ptMoveCanvas.y, point.y) + GetScrollPos(SB_VERT);
694 float fZoomfactor = m_fZoomFactor*fact;
695 if (fZoomfactor > 10 * MAX_ZOOM)
697 // with such a big zoomfactor, the user
698 // most likely zoomed by accident
699 Invalidate(FALSE);
700 __super::OnLButtonUp(nFlags, point);
701 return;
703 if (fZoomfactor > MAX_ZOOM)
705 fZoomfactor = MAX_ZOOM;
706 fact = fZoomfactor/m_fZoomFactor;
709 CRevisionGraphDlg * pDlg = (CRevisionGraphDlg*)GetParent();
710 if (pDlg)
712 m_fZoomFactor = fZoomfactor;
713 pDlg->DoZoom (m_fZoomFactor);
714 SetScrollbars(int(float(y)*fact), int(float(x)*fact));
716 __super::OnLButtonUp(nFlags, point);
719 bool CRevisionGraphWnd::CancelMouseZoom()
721 bool bRet = m_bIsCanvasMove;
722 ReleaseCapture();
723 if (m_bIsCanvasMove)
724 Invalidate(FALSE);
725 m_bIsCanvasMove = false;
726 m_ptRubberEnd = CPoint(0,0);
727 return bRet;
730 INT_PTR CRevisionGraphWnd::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
733 if (IsUpdateJobRunning())
734 return -1;
736 node nodeIndex = GetHitNode (point);
737 if (m_tooltipIndex != nodeIndex)
739 // force tooltip to be updated
741 m_tooltipIndex = nodeIndex;
742 return -1;
745 if (nodeIndex == NULL)
746 return -1;
748 // if ((GetHoverGlyphs (point) != 0) || (GetHitGlyph (point) != NULL))
749 // return -1;
751 pTI->hwnd = this->m_hWnd;
752 CWnd::GetClientRect(&pTI->rect);
753 pTI->uFlags |= TTF_ALWAYSTIP | TTF_IDISHWND;
754 pTI->uId = (UINT_PTR)m_hWnd;
755 pTI->lpszText = LPSTR_TEXTCALLBACK;
757 return 1;
760 BOOL CRevisionGraphWnd::OnToolTipNotify(UINT /*id*/, NMHDR *pNMHDR, LRESULT *pResult)
762 if (pNMHDR->idFrom != (UINT_PTR)m_hWnd)
763 return FALSE;
765 POINT point;
766 DWORD ptW = GetMessagePos();
767 point.x = GET_X_LPARAM(ptW);
768 point.y = GET_Y_LPARAM(ptW);
769 ScreenToClient(&point);
771 CString strTipText = TooltipText (GetHitNode (point));
773 *pResult = 0;
774 if (strTipText.IsEmpty())
775 return TRUE;
777 CSize tooltipSize = UsableTooltipRect();
778 strTipText = DisplayableText (strTipText, tooltipSize);
780 // need to handle both ANSI and UNICODE versions of the message
781 if (pNMHDR->code == TTN_NEEDTEXTA)
783 TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
784 ::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, tooltipSize.cx);
785 pTTTA->lpszText = m_szTip;
786 WideCharToMultiByte(CP_ACP, 0, strTipText, -1, m_szTip, strTipText.GetLength()+1, 0, 0);
788 else
790 TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
791 ::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, tooltipSize.cx);
792 lstrcpyn(m_wszTip, strTipText, strTipText.GetLength()+1);
793 pTTTW->lpszText = m_wszTip;
796 // show the tooltip for 32 seconds. A higher value than 32767 won't work
797 // even though it's nowhere documented!
798 ::SendMessage(pNMHDR->hwndFrom, TTM_SETDELAYTIME, TTDT_AUTOPOP, 32767);
799 return TRUE; // message was handled
802 CSize CRevisionGraphWnd::UsableTooltipRect()
804 // get screen size
806 int screenWidth = GetSystemMetrics(SM_CXSCREEN);
807 int screenHeight = GetSystemMetrics(SM_CYSCREEN);
809 // get current mouse position
811 CPoint cursorPos;
812 if (GetCursorPos (&cursorPos) == FALSE)
814 // we could not determine the mouse position
815 // use screen / 2 minus some safety margin
817 return CSize (screenWidth / 2 - 20, screenHeight / 2 - 20);
820 // tool tip will display in the biggest sector beside the cursor
821 // deduct some safety margin (for the mouse cursor itself
823 CSize biggestSector
824 ( max (screenWidth - cursorPos.x - 40, cursorPos.x - 24)
825 , max (screenHeight - cursorPos.y - 40, cursorPos.y - 24));
827 return biggestSector;
830 CString CRevisionGraphWnd::DisplayableText ( const CString& wholeText
831 , const CSize& tooltipSize)
833 CDC* dc = GetDC();
834 if (dc == NULL)
836 // no access to the device context -> truncate hard at 1000 chars
838 return wholeText.GetLength() >= MAX_TT_LENGTH_DEFAULT
839 ? wholeText.Left (MAX_TT_LENGTH_DEFAULT-4) + _T(" ...")
840 : wholeText;
843 // select the tooltip font
845 NONCLIENTMETRICS metrics;
846 metrics.cbSize = sizeof (metrics);
847 if (!SysInfo::Instance().IsVistaOrLater())
849 metrics.cbSize -= sizeof(int); // subtract the size of the iPaddedBorderWidth member which is not available on XP
851 SystemParametersInfo (SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
853 CFont font;
854 font.CreateFontIndirect(&metrics.lfStatusFont);
855 CFont* pOldFont = dc->SelectObject (&font);
857 // split into lines and fill the tooltip rect
859 CString result;
861 int remainingHeight = tooltipSize.cy;
862 int pos = 0;
863 while (pos < wholeText.GetLength())
865 // extract a whole line
867 int nextPos = wholeText.Find ('\n', pos);
868 if (nextPos < 0)
869 nextPos = wholeText.GetLength();
871 CString line = wholeText.Mid (pos, nextPos-pos+1);
873 // find a way to make it fit
875 CSize size = dc->GetTextExtent (line);
876 while (size.cx > tooltipSize.cx)
878 line.Delete (line.GetLength()-1);
879 int nextPos2 = line.ReverseFind (' ');
880 if (nextPos2 < 0)
881 break;
883 line.Delete (nextPos2+1, line.GetLength() - pos-1);
884 size = dc->GetTextExtent (line);
887 // enough room for the new line?
889 remainingHeight -= size.cy;
890 if (remainingHeight <= size.cy)
892 result += _T("...");
893 break;
896 // add the line
898 result += line;
899 pos += line.GetLength();
902 // relase temp. resources
904 dc->SelectObject (pOldFont);
905 ReleaseDC(dc);
907 // ready
909 return result;
912 CString CRevisionGraphWnd::TooltipText(node index)
914 if(index)
916 CString str;
917 CGitHash hash = m_logEntries[index->index()];
918 GitRev *rev = this->m_LogCache.GetCacheData(hash);
919 str += rev->m_CommitHash.ToString();
920 str += _T("\n");
921 str += rev->GetAuthorName() +_T(" ") + rev->GetAuthorEmail();
922 str += _T(" ");
923 str += rev->GetAuthorDate().Format(_T("%Y-%m-%d %H:%M"));
924 str += _T("\n\n")+rev->GetSubject();
925 str += _T("\n");
926 str += rev->GetBody();
927 return str;
929 }else
930 return CString();
933 void CRevisionGraphWnd::SaveGraphAs(CString sSavePath)
936 CString extension = CPathUtils::GetFileExtFromPath(sSavePath);
937 if (extension.CompareNoCase(_T(".wmf"))==0)
939 // save the graph as an enhanced metafile
940 CMetaFileDC wmfDC;
941 wmfDC.CreateEnhanced(NULL, sSavePath, NULL, _T("TortoiseGit\0Revision Graph\0\0"));
942 float fZoom = m_fZoomFactor;
943 m_fZoomFactor = DEFAULT_ZOOM;
944 DoZoom(m_fZoomFactor);
945 CRect rect;
946 rect = GetViewRect();
947 GraphicsDevice dev;
948 dev.pDC = &wmfDC;
949 DrawGraph(dev, rect, 0, 0, true);
950 HENHMETAFILE hemf = wmfDC.CloseEnhanced();
951 DeleteEnhMetaFile(hemf);
952 m_fZoomFactor = fZoom;
953 DoZoom(m_fZoomFactor);
955 else if (extension.CompareNoCase(_T(".svg"))==0)
957 // save the graph as a scalable vector graphic
958 SVG svg;
959 float fZoom = m_fZoomFactor;
960 m_fZoomFactor = DEFAULT_ZOOM;
961 DoZoom(m_fZoomFactor);
962 CRect rect;
963 rect = GetViewRect();
964 svg.SetViewSize(rect.Width(), rect.Height());
965 GraphicsDevice dev;
966 dev.pSVG = &svg;
967 DrawGraph(dev, rect, 0, 0, true);
968 svg.Save(sSavePath);
969 m_fZoomFactor = fZoom;
970 DoZoom(m_fZoomFactor);
972 else if (extension.CompareNoCase(_T(".gv")) == 0)
974 Graphviz graphviz;
975 float fZoom = m_fZoomFactor;
976 m_fZoomFactor = DEFAULT_ZOOM;
977 DoZoom(m_fZoomFactor);
978 CRect rect;
979 rect = GetViewRect();
980 GraphicsDevice dev;
981 dev.pGraphviz = &graphviz;
982 DrawGraph(dev, rect, 0, 0, true);
983 graphviz.Save(sSavePath);
984 m_fZoomFactor = fZoom;
985 DoZoom(m_fZoomFactor);
987 else
989 // save the graph as a pixel picture instead of a vector picture
990 // create dc to paint on
993 CString sErrormessage;
994 CWindowDC ddc(this);
995 CDC dc;
996 if (!dc.CreateCompatibleDC(&ddc))
998 CFormatMessageWrapper errorDetails;
999 if( errorDetails )
1000 MessageBox( errorDetails, _T("Error"), MB_OK | MB_ICONINFORMATION );
1002 return;
1004 CRect rect;
1005 rect = GetGraphRect();
1006 rect.bottom = (LONG)(float(rect.Height()) * m_fZoomFactor);
1007 rect.right = (LONG)(float(rect.Width()) * m_fZoomFactor);
1008 BITMAPINFO bmi;
1009 HBITMAP hbm;
1010 LPBYTE pBits;
1011 // Initialize header to 0s.
1012 SecureZeroMemory(&bmi, sizeof(bmi));
1013 // Fill out the fields you care about.
1014 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1015 bmi.bmiHeader.biWidth = rect.Width();
1016 bmi.bmiHeader.biHeight = rect.Height();
1017 bmi.bmiHeader.biPlanes = 1;
1018 bmi.bmiHeader.biBitCount = 24;
1019 bmi.bmiHeader.biCompression = BI_RGB;
1021 // Create the surface.
1022 hbm = CreateDIBSection(ddc.m_hDC, &bmi, DIB_RGB_COLORS,(void **)&pBits, NULL, 0);
1023 if (hbm==0)
1025 CMessageBox::Show(m_hWnd, IDS_REVGRAPH_ERR_NOMEMORY, IDS_APPNAME, MB_ICONERROR);
1026 return;
1028 HBITMAP oldbm = (HBITMAP)dc.SelectObject(hbm);
1029 // paint the whole graph
1030 GraphicsDevice dev;
1031 dev.pDC = &dc;
1032 DrawGraph(dev, rect, 0, 0, true);
1033 // now use GDI+ to save the picture
1034 CLSID encoderClsid;
1036 Bitmap bitmap(hbm, NULL);
1037 if (bitmap.GetLastStatus()==Ok)
1039 // Get the CLSID of the encoder.
1040 int ret = 0;
1041 if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".png"))==0)
1042 ret = GetEncoderClsid(L"image/png", &encoderClsid);
1043 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".jpg"))==0)
1044 ret = GetEncoderClsid(L"image/jpeg", &encoderClsid);
1045 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".jpeg"))==0)
1046 ret = GetEncoderClsid(L"image/jpeg", &encoderClsid);
1047 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".bmp"))==0)
1048 ret = GetEncoderClsid(L"image/bmp", &encoderClsid);
1049 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".gif"))==0)
1050 ret = GetEncoderClsid(L"image/gif", &encoderClsid);
1051 else
1053 sSavePath += _T(".jpg");
1054 ret = GetEncoderClsid(L"image/jpeg", &encoderClsid);
1056 if (ret >= 0)
1058 CStringW tfile = CStringW(sSavePath);
1059 bitmap.Save(tfile, &encoderClsid, NULL);
1061 else
1063 sErrormessage.Format(IDS_REVGRAPH_ERR_NOENCODER, (LPCTSTR)CPathUtils::GetFileExtFromPath(sSavePath));
1066 else
1068 sErrormessage.LoadString(IDS_REVGRAPH_ERR_NOBITMAP);
1071 dc.SelectObject(oldbm);
1072 DeleteObject(hbm);
1073 dc.DeleteDC();
1074 if (!sErrormessage.IsEmpty())
1076 ::MessageBox(m_hWnd, sErrormessage, _T("TortoiseGit"), MB_ICONERROR);
1079 catch (CException * pE)
1081 TCHAR szErrorMsg[2048] = { 0 };
1082 pE->GetErrorMessage(szErrorMsg, 2048);
1083 pE->Delete();
1084 ::MessageBox(m_hWnd, szErrorMsg, _T("TortoiseGit"), MB_ICONERROR);
1090 BOOL CRevisionGraphWnd::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
1092 if (IsUpdateJobRunning())
1093 return __super::OnMouseWheel(nFlags, zDelta, pt);
1095 if (GetKeyState(VK_CONTROL)&0x8000)
1097 float newZoom = m_fZoomFactor * (zDelta < 0 ? ZOOM_STEP : 1.0f/ZOOM_STEP);
1098 DoZoom (max (MIN_ZOOM, min (MAX_ZOOM, newZoom)));
1100 else
1102 int orientation = GetKeyState(VK_SHIFT)&0x8000 ? SB_HORZ : SB_VERT;
1103 int pos = GetScrollPos(orientation);
1104 pos -= (zDelta);
1105 SetScrollPos(orientation, pos);
1106 Invalidate(FALSE);
1108 return __super::OnMouseWheel(nFlags, zDelta, pt);
1111 void CRevisionGraphWnd::OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt)
1113 if (IsUpdateJobRunning())
1114 return __super::OnMouseHWheel(nFlags, zDelta, pt);
1116 int orientation = GetKeyState(VK_SHIFT)&0x8000 ? SB_VERT : SB_HORZ;
1117 int pos = GetScrollPos(orientation);
1118 pos -= (zDelta);
1119 SetScrollPos(orientation, pos);
1120 Invalidate(FALSE);
1122 return __super::OnMouseHWheel(nFlags, zDelta, pt);
1125 bool CRevisionGraphWnd::UpdateSelectedEntry (node clickedentry)
1127 if ((m_SelectedEntry1 == NULL)&&(clickedentry == NULL))
1128 return false;
1130 if (m_SelectedEntry1 == NULL)
1132 m_SelectedEntry1 = clickedentry;
1133 Invalidate(FALSE);
1135 if ((m_SelectedEntry2 == NULL)&&(clickedentry != m_SelectedEntry1))
1137 m_SelectedEntry1 = clickedentry;
1138 Invalidate(FALSE);
1140 if (m_SelectedEntry1 && m_SelectedEntry2)
1142 if ((m_SelectedEntry2 != clickedentry)&&(m_SelectedEntry1 != clickedentry))
1143 return false;
1145 if (m_SelectedEntry1 == NULL)
1146 return false;
1148 return true;
1151 void CRevisionGraphWnd::AppendMenu
1152 ( CMenu& popup
1153 , UINT title
1154 , UINT command
1155 , UINT flags)
1157 // separate different groups / section within the context menu
1159 if (popup.GetMenuItemCount() > 0)
1161 UINT lastCommand = popup.GetMenuItemID (popup.GetMenuItemCount()-1);
1162 if ((lastCommand & GROUP_MASK) != (command & GROUP_MASK))
1163 popup.AppendMenu(MF_SEPARATOR, NULL);
1166 // actually add the new item
1168 CString titleString;
1169 titleString.LoadString (title);
1170 popup.AppendMenu (MF_STRING | flags, command, titleString);
1173 void CRevisionGraphWnd::AppendMenu(CMenu &popup, CString title, UINT command, CString *extra, CMenu *submenu)
1175 // separate different groups / section within the context menu
1176 if (popup.GetMenuItemCount() > 0)
1178 UINT lastCommand = popup.GetMenuItemID(popup.GetMenuItemCount() - 1);
1179 if ((lastCommand & GROUP_MASK) != (command & GROUP_MASK))
1180 popup.AppendMenu(MF_SEPARATOR, NULL);
1183 // actually add the new item
1184 MENUITEMINFO mii;
1185 memset(&mii, 0, sizeof(mii));
1186 mii.cbSize = sizeof(MENUITEMINFO);
1187 mii.fMask = MIIM_STRING | MIIM_ID | (extra ? MIIM_DATA : 0) | (submenu ? MIIM_SUBMENU : 0);
1188 mii.wID = command;
1189 mii.hSubMenu = submenu ? submenu->m_hMenu : NULL;
1190 mii.dwItemData = (ULONG_PTR)extra;
1191 mii.dwTypeData = title.GetBuffer();
1192 InsertMenuItem(popup, popup.GetMenuItemCount(), TRUE, &mii);
1193 title.ReleaseBuffer();
1196 void CRevisionGraphWnd::AddGraphOps (CMenu& /*popup*/, const CVisibleGraphNode * /*node*/)
1198 #if 0
1199 CSyncPointer<CGraphNodeStates> nodeStates (m_state.GetNodeStates());
1201 if (node == NULL)
1203 DWORD state = nodeStates->GetCombinedFlags();
1204 if (state != 0)
1206 if (state & CGraphNodeStates::COLLAPSED_ALL)
1207 AppendMenu (popup, IDS_REVGRAPH_POPUP_EXPAND_ALL, ID_EXPAND_ALL);
1209 if (state & CGraphNodeStates::SPLIT_ALL)
1210 AppendMenu (popup, IDS_REVGRAPH_POPUP_JOIN_ALL, ID_JOIN_ALL);
1213 else
1215 DWORD state = nodeStates->GetFlags (node);
1217 if (node->GetSource() || (state & CGraphNodeStates::COLLAPSED_ABOVE))
1218 AppendMenu ( popup
1219 , (state & CGraphNodeStates::COLLAPSED_ABOVE)
1220 ? IDS_REVGRAPH_POPUP_EXPAND_ABOVE
1221 : IDS_REVGRAPH_POPUP_COLLAPSE_ABOVE
1222 , ID_GRAPH_EXPANDCOLLAPSE_ABOVE);
1224 if (node->GetFirstCopyTarget() || (state & CGraphNodeStates::COLLAPSED_RIGHT))
1225 AppendMenu ( popup
1226 , (state & CGraphNodeStates::COLLAPSED_RIGHT)
1227 ? IDS_REVGRAPH_POPUP_EXPAND_RIGHT
1228 : IDS_REVGRAPH_POPUP_COLLAPSE_RIGHT
1229 , ID_GRAPH_EXPANDCOLLAPSE_RIGHT);
1231 if (node->GetNext() || (state & CGraphNodeStates::COLLAPSED_BELOW))
1232 AppendMenu ( popup
1233 , (state & CGraphNodeStates::COLLAPSED_BELOW)
1234 ? IDS_REVGRAPH_POPUP_EXPAND_BELOW
1235 : IDS_REVGRAPH_POPUP_COLLAPSE_BELOW
1236 , ID_GRAPH_EXPANDCOLLAPSE_BELOW);
1238 if (node->GetSource() || (state & CGraphNodeStates::SPLIT_ABOVE))
1239 AppendMenu ( popup
1240 , (state & CGraphNodeStates::SPLIT_ABOVE)
1241 ? IDS_REVGRAPH_POPUP_JOIN_ABOVE
1242 : IDS_REVGRAPH_POPUP_SPLIT_ABOVE
1243 , ID_GRAPH_SPLITJOIN_ABOVE);
1245 if (node->GetFirstCopyTarget() || (state & CGraphNodeStates::SPLIT_RIGHT))
1246 AppendMenu ( popup
1247 , (state & CGraphNodeStates::SPLIT_RIGHT)
1248 ? IDS_REVGRAPH_POPUP_JOIN_RIGHT
1249 : IDS_REVGRAPH_POPUP_SPLIT_RIGHT
1250 , ID_GRAPH_SPLITJOIN_RIGHT);
1252 if (node->GetNext() || (state & CGraphNodeStates::SPLIT_BELOW))
1253 AppendMenu ( popup
1254 , (state & CGraphNodeStates::SPLIT_BELOW)
1255 ? IDS_REVGRAPH_POPUP_JOIN_BELOW
1256 : IDS_REVGRAPH_POPUP_SPLIT_BELOW
1257 , ID_GRAPH_SPLITJOIN_BELOW);
1259 #endif
1262 CString CRevisionGraphWnd::GetSelectedURL() const
1264 #if 0
1265 if (m_SelectedEntry1 == NULL)
1266 return CString();
1268 CString URL = m_state.GetRepositoryRoot()
1269 + CUnicodeUtils::GetUnicode (m_SelectedEntry1->GetPath().GetPath().c_str());
1270 URL = CUnicodeUtils::GetUnicode(CPathUtils::PathEscape(CUnicodeUtils::GetUTF8(URL)));
1272 return URL;
1273 #endif
1274 return CString();
1277 CString CRevisionGraphWnd::GetWCURL() const
1279 #if 0
1280 CTGitPath path (m_sPath);
1281 if (path.IsUrl())
1282 return CString();
1284 SVNInfo info;
1285 const SVNInfoData * status
1286 = info.GetFirstFileInfo (path, SVNRev(), SVNRev());
1288 return status == NULL ? CString() : status->url;
1289 #endif
1290 return CString();
1293 void CRevisionGraphWnd::DoShowLog()
1296 if(m_SelectedEntry1 == NULL)
1297 return;
1299 CString sCmd;
1301 if(m_SelectedEntry2 != NULL)
1302 sCmd.Format(_T("/command:log %s /startrev:%s /endrev:%s"),
1303 this->m_sPath.IsEmpty() ? _T("") : (_T("/path:\"") + this->m_sPath + _T("\"")),
1304 this->m_logEntries[m_SelectedEntry1->index()].ToString(),
1305 this->m_logEntries[m_SelectedEntry2->index()].ToString());
1306 else
1307 sCmd.Format(_T("/command:log %s /endrev:%s"),
1308 this->m_sPath.IsEmpty() ? _T("") : (_T("/path:\"") + this->m_sPath + _T("\"")),
1309 this->m_logEntries[m_SelectedEntry1->index()].ToString());
1311 CAppUtils::RunTortoiseGitProc(sCmd);
1315 void CRevisionGraphWnd::DoCheckForModification()
1317 CChangedDlg dlg;
1318 dlg.m_pathList = CTGitPathList (CTGitPath (m_sPath));
1319 dlg.DoModal();
1322 void CRevisionGraphWnd::DoMergeTo()
1324 #if 0
1325 CString URL = GetSelectedURL();
1326 CString path = m_sPath;
1327 CBrowseFolder folderBrowser;
1328 folderBrowser.SetInfo(CString(MAKEINTRESOURCE(IDS_LOG_MERGETO)));
1329 if (folderBrowser.Show(GetSafeHwnd(), path, path) == CBrowseFolder::OK)
1331 CSVNProgressDlg dlg;
1332 dlg.SetCommand(CSVNProgressDlg::SVNProgress_Merge);
1333 dlg.SetPathList(CTGitPathList(CTGitPath(path)));
1334 dlg.SetUrl(URL);
1335 dlg.SetSecondUrl(URL);
1336 SVNRevRangeArray revarray;
1337 revarray.AddRevRange (m_SelectedEntry1->GetRevision()-1, svn_revnum_t(m_SelectedEntry1->GetRevision()));
1338 dlg.SetRevisionRanges(revarray);
1339 dlg.DoModal();
1341 #endif
1344 void CRevisionGraphWnd::DoUpdate()
1346 #if 0
1347 CSVNProgressDlg progDlg;
1348 progDlg.SetCommand (CSVNProgressDlg::SVNProgress_Update);
1349 progDlg.SetOptions (0); // don't ignore externals
1350 progDlg.SetPathList (CTGitPathList (CTGitPath (m_sPath)));
1351 progDlg.SetRevision (m_SelectedEntry1->GetRevision());
1352 progDlg.SetDepth();
1353 progDlg.DoModal();
1355 if (m_state.GetFetchedWCState())
1356 m_parent->UpdateFullHistory();
1357 #endif
1360 void CRevisionGraphWnd::DoSwitch(CString rev)
1362 CAppUtils::PerformSwitch(rev);
1365 void CRevisionGraphWnd::DoSwitchToHead()
1367 #if 0
1368 CSVNProgressDlg progDlg;
1369 progDlg.SetCommand (CSVNProgressDlg::SVNProgress_Switch);
1370 progDlg.SetPathList (CTGitPathList (CTGitPath (m_sPath)));
1371 progDlg.SetUrl (GetSelectedURL());
1372 progDlg.SetRevision (SVNRev::REV_HEAD);
1373 progDlg.SetPegRevision (m_SelectedEntry1->GetRevision());
1374 progDlg.DoModal();
1376 if (m_state.GetFetchedWCState())
1377 m_parent->UpdateFullHistory();
1378 #endif
1381 void CRevisionGraphWnd::DoBrowseRepo()
1383 if (m_SelectedEntry1 == NULL)
1384 return;
1386 CString sCmd;
1387 sCmd.Format(_T("/command:repobrowser %s /rev:%s"),
1388 this->m_sPath.IsEmpty() ? _T("") : (_T("/path:\"") + this->m_sPath + _T("\"")),
1389 GetFriendRefName(m_SelectedEntry1));
1391 CAppUtils::RunTortoiseGitProc(sCmd);
1394 void CRevisionGraphWnd::ResetNodeFlags (DWORD /*flags*/)
1396 // m_state.GetNodeStates()->ResetFlags (flags);
1397 // m_parent->StartWorkerThread();
1400 void CRevisionGraphWnd::ToggleNodeFlag (const CVisibleGraphNode * /*node*/, DWORD /*flag*/)
1402 #if 0
1403 CSyncPointer<CGraphNodeStates> nodeStates (m_state.GetNodeStates());
1405 if (nodeStates->GetFlags (node) & flag)
1406 nodeStates->ResetFlags (node, flag);
1407 else
1408 nodeStates->SetFlags (node, flag);
1410 m_parent->StartWorkerThread();
1411 #endif
1414 void CRevisionGraphWnd::DoCopyRefs()
1416 if (m_SelectedEntry1 == NULL)
1417 return;
1419 STRING_VECTOR list = GetFriendRefNames(m_SelectedEntry1);
1420 CString text;
1421 if (list.empty())
1422 text = m_logEntries[m_SelectedEntry1->index()].ToString();
1423 for (size_t i = 0; i < list.size(); ++i)
1425 if (i > 0)
1426 text.Append(_T("\r\n"));
1427 text.Append(list[i]);
1429 CStringUtils::WriteAsciiStringToClipboard(text, m_hWnd);
1432 void CRevisionGraphWnd::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
1434 if (IsUpdateJobRunning())
1435 return;
1437 CPoint clientpoint = point;
1438 this->ScreenToClient(&clientpoint);
1440 node nodeIndex = GetHitNode (clientpoint);
1442 if ( !UpdateSelectedEntry (nodeIndex))
1444 return;
1447 CMenu popup;
1448 if (!popup.CreatePopupMenu())
1449 return;
1451 bool bothPresent = (m_SelectedEntry2 && m_SelectedEntry1);
1453 AppendMenu (popup, IDS_REPOBROWSE_SHOWLOG, ID_SHOWLOG);
1455 STRING_VECTOR branchNames;
1456 if (m_SelectedEntry1 && (m_SelectedEntry2 == nullptr))
1458 AppendMenu(popup, IDS_LOG_BROWSEREPO, ID_BROWSEREPO);
1460 CString currentBranch = g_Git.GetCurrentBranch();
1461 CGit::REF_TYPE refType = CGit::LOCAL_BRANCH;
1462 STRING_VECTOR allBranchNames = GetFriendRefNames(m_SelectedEntry1, &refType, 1);
1463 for (size_t i = 0; i < allBranchNames.size(); ++i)
1464 if (allBranchNames[i] != currentBranch)
1465 branchNames.push_back(allBranchNames[i]);
1466 if (branchNames.size() == 1)
1468 CString text;
1469 text.Format(_T("%s \"%s\""), CString(MAKEINTRESOURCE(IDS_SWITCH_BRANCH)), branchNames[0]);
1470 AppendMenu(popup, text, ID_SWITCH, &branchNames[0]);
1472 else if (branchNames.size() > 1)
1474 CMenu switchMenu;
1475 switchMenu.CreatePopupMenu();
1476 for (size_t i = 0; i < branchNames.size(); ++i)
1477 AppendMenu(switchMenu, branchNames[i], ID_SWITCH + ((int)(i + 1) << 16), &branchNames[i]);
1478 AppendMenu(popup, CString(MAKEINTRESOURCE(IDS_SWITCH_BRANCH)), ID_SWITCH, NULL, &switchMenu);
1481 AppendMenu(popup, IDS_COPY_REF_NAMES, ID_COPYREFS);
1483 AppendMenu(popup, IDS_REVGRAPH_POPUP_COMPAREHEADS, ID_COMPAREHEADS);
1484 AppendMenu(popup, IDS_REVGRAPH_POPUP_UNIDIFFHEADS, ID_UNIDIFFHEADS);
1486 AppendMenu(popup, IDS_LOG_POPUP_COMPARE, ID_COMPAREWT);
1489 if (bothPresent)
1491 AppendMenu (popup, IDS_REVGRAPH_POPUP_COMPAREREVS, ID_COMPAREREVS);
1492 AppendMenu (popup, IDS_REVGRAPH_POPUP_UNIDIFFREVS, ID_UNIDIFFREVS);
1495 // AddGraphOps (popup, clickedentry);
1497 // if the context menu is invoked through the keyboard, we have to use
1498 // a calculated position on where to anchor the menu on
1499 if ((point.x == -1) && (point.y == -1))
1501 CRect rect = GetWindowRect();
1502 point = rect.CenterPoint();
1505 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON, point.x, point.y, this, 0);
1506 switch (cmd & 0xFFFF)
1508 case ID_COMPAREREVS:
1509 if (m_SelectedEntry1 != NULL)
1510 CompareRevs(false);
1511 break;
1512 case ID_UNIDIFFREVS:
1513 if (m_SelectedEntry1 != NULL)
1514 UnifiedDiffRevs(false);
1515 break;
1516 case ID_UNIDIFFHEADS:
1517 if (m_SelectedEntry1 != NULL)
1518 UnifiedDiffRevs(true);
1519 break;
1520 case ID_SHOWLOG:
1521 DoShowLog();
1522 break;
1523 case ID_SWITCH:
1525 MENUITEMINFO mii;
1526 memset(&mii, 0, sizeof(mii));
1527 mii.cbSize = sizeof(mii);
1528 mii.fMask |= MIIM_DATA;
1529 GetMenuItemInfo(popup, cmd, FALSE, &mii);
1530 CString *rev = (CString *)mii.dwItemData;
1531 if (rev != NULL)
1533 DoSwitch(*rev);
1534 m_parent->UpdateFullHistory();
1536 break;
1538 case ID_COPYREFS:
1539 DoCopyRefs();
1540 break;
1541 case ID_BROWSEREPO:
1542 DoBrowseRepo();
1543 break;
1544 case ID_COMPAREHEADS:
1545 if (m_SelectedEntry1 != NULL)
1546 CompareRevs(_T("HEAD"));
1547 break;
1548 case ID_COMPAREWT:
1549 if (m_SelectedEntry1 != NULL)
1550 CompareRevs(CGitHash().ToString());
1551 break;
1553 #if 0
1554 case ID_COMPAREREVS:
1555 if (m_SelectedEntry1 != NULL)
1556 CompareRevs(false);
1557 break;
1558 case ID_UNIDIFFREVS:
1559 if (m_SelectedEntry1 != NULL)
1560 UnifiedDiffRevs(false);
1561 break;
1562 case ID_UNIDIFFHEADS:
1563 if (m_SelectedEntry1 != NULL)
1564 UnifiedDiffRevs(true);
1565 break;
1566 case ID_SHOWLOG:
1567 DoShowLog();
1568 break;
1569 case ID_CFM:
1570 DoCheckForModification();
1571 break;
1572 case ID_MERGETO:
1573 DoMergeTo();
1574 break;
1575 case ID_UPDATE:
1576 DoUpdate();
1577 break;
1578 case ID_SWITCHTOHEAD:
1579 DoSwitchToHead();
1580 break;
1581 case ID_EXPAND_ALL:
1582 ResetNodeFlags (CGraphNodeStates::COLLAPSED_ALL);
1583 break;
1584 case ID_JOIN_ALL:
1585 ResetNodeFlags (CGraphNodeStates::SPLIT_ALL);
1586 break;
1587 case ID_GRAPH_EXPANDCOLLAPSE_ABOVE:
1588 ToggleNodeFlag (clickedentry, CGraphNodeStates::COLLAPSED_ABOVE);
1589 break;
1590 case ID_GRAPH_EXPANDCOLLAPSE_RIGHT:
1591 ToggleNodeFlag (clickedentry, CGraphNodeStates::COLLAPSED_RIGHT);
1592 break;
1593 case ID_GRAPH_EXPANDCOLLAPSE_BELOW:
1594 ToggleNodeFlag (clickedentry, CGraphNodeStates::COLLAPSED_BELOW);
1595 break;
1596 case ID_GRAPH_SPLITJOIN_ABOVE:
1597 ToggleNodeFlag (clickedentry, CGraphNodeStates::SPLIT_ABOVE);
1598 break;
1599 case ID_GRAPH_SPLITJOIN_RIGHT:
1600 ToggleNodeFlag (clickedentry, CGraphNodeStates::SPLIT_RIGHT);
1601 break;
1602 case ID_GRAPH_SPLITJOIN_BELOW:
1603 ToggleNodeFlag (clickedentry, CGraphNodeStates::SPLIT_BELOW);
1604 break;
1605 #endif
1610 void CRevisionGraphWnd::OnMouseMove(UINT nFlags, CPoint point)
1613 if (IsUpdateJobRunning())
1615 return __super::OnMouseMove(nFlags, point);
1617 if (!m_bIsCanvasMove)
1619 if (m_bShowOverview && (m_OverviewRect.PtInRect(point))&&(nFlags & MK_LBUTTON))
1621 // scrolling
1622 CRect viewRect = GetViewRect();
1623 int x = (int)((point.x-m_OverviewRect.left - (m_OverviewPosRect.Width()/2)) / m_previewZoom * m_fZoomFactor);
1624 int y = (int)((point.y - m_OverviewRect.top - (m_OverviewPosRect.Height()/2)) / m_previewZoom * m_fZoomFactor);
1625 x = max(0, x);
1626 y = max(0, y);
1627 SetScrollbars(y, x);
1628 Invalidate(FALSE);
1629 return __super::OnMouseMove(nFlags, point);
1631 else
1633 // update screen if we hover over a different
1634 // node than during the last redraw
1636 CPoint clientPoint = point;
1637 GetCursorPos (&clientPoint);
1638 ScreenToClient (&clientPoint);
1640 #if 0
1641 const CRevisionGraphState::SVisibleGlyph* hitGlyph
1642 = GetHitGlyph (clientPoint);
1643 const CFullGraphNode* glyphNode
1644 = hitGlyph ? hitGlyph->node->GetBase() : NULL;
1646 const CFullGraphNode* hoverNode = NULL;
1647 if (m_hoverIndex != NO_INDEX)
1649 CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
1650 if (m_hoverIndex < nodeList->GetCount())
1651 hoverNode = nodeList->GetNode (m_hoverIndex).node->GetBase();
1654 //bool onHoverNodeGlyph = (hoverNode != NULL) && (glyphNode == hoverNode);
1655 if ( !m_hoverIndex
1656 && ( (m_hoverIndex != GetHitNode (clientPoint))))
1658 m_showHoverGlyphs = false;
1660 KillTimer (GLYPH_HOVER_EVENT);
1661 SetTimer (GLYPH_HOVER_EVENT, GLYPH_HOVER_DELAY, NULL);
1663 Invalidate(FALSE);
1665 #endif
1666 return __super::OnMouseMove(nFlags, point);
1669 SetCapture();
1671 int pos_h = GetScrollPos(SB_HORZ);
1672 pos_h -= point.x - m_ptMoveCanvas.x;
1673 SetScrollPos(SB_HORZ, pos_h);
1675 int pos_v = GetScrollPos(SB_VERT);
1676 pos_v -= point.y - m_ptMoveCanvas.y;
1677 SetScrollPos(SB_VERT, pos_v);
1679 m_ptMoveCanvas = point;
1681 this->Invalidate();
1683 __super::OnMouseMove(nFlags, point);
1686 BOOL CRevisionGraphWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
1688 CRect viewRect = GetViewRect();
1690 LPTSTR cursorID = IDC_ARROW;
1691 HINSTANCE resourceHandle = NULL;
1693 if ((nHitTest == HTCLIENT)&&(pWnd == this)&&(viewRect.Width())&&(viewRect.Height())&&(message))
1695 POINT pt;
1696 if (GetCursorPos(&pt))
1698 ScreenToClient(&pt);
1699 if (m_OverviewPosRect.PtInRect(pt))
1701 resourceHandle = AfxGetResourceHandle();
1702 cursorID = GetKeyState(VK_LBUTTON) & 0x8000
1703 ? MAKEINTRESOURCE(IDC_PANCURDOWN)
1704 : MAKEINTRESOURCE(IDC_PANCUR);
1706 if (m_bIsCanvasMove)
1707 cursorID = IDC_HAND;
1711 HCURSOR hCur = LoadCursor(resourceHandle, MAKEINTRESOURCE(cursorID));
1712 if (GetCursor() != hCur)
1713 SetCursor (hCur);
1715 return TRUE;
1718 void CRevisionGraphWnd::OnTimer (UINT_PTR nIDEvent)
1720 if (nIDEvent == GLYPH_HOVER_EVENT)
1722 KillTimer (GLYPH_HOVER_EVENT);
1724 m_showHoverGlyphs = true;
1725 Invalidate (FALSE);
1727 else
1729 __super::OnTimer (nIDEvent);
1733 LRESULT CRevisionGraphWnd::OnWorkerThreadDone(WPARAM, LPARAM)
1735 // handle potential race condition between PostMessage and leaving job:
1736 // the background job may not have exited, yet
1738 if (updateJob.get())
1739 updateJob->GetResult();
1741 InitView();
1742 BuildPreview();
1744 if (m_HeadNode)
1746 SCROLLINFO sinfo = { 0 };
1747 sinfo.cbSize = sizeof(SCROLLINFO);
1748 if (GetScrollInfo(SB_HORZ, &sinfo))
1750 sinfo.nPos = (int)min(max(sinfo.nMin, m_GraphAttr.x(m_HeadNode) - m_GraphAttr.width(m_HeadNode) / 2), sinfo.nMax);
1751 SetScrollInfo(SB_HORZ, &sinfo);
1753 if (GetScrollInfo(SB_VERT, &sinfo))
1755 sinfo.nPos = (int)min(max(sinfo.nMin, m_GraphAttr.y(m_HeadNode) - m_GraphAttr.height(m_HeadNode) / 2), sinfo.nMax);
1756 SetScrollInfo(SB_VERT, &sinfo);
1760 Invalidate(FALSE);
1762 if (m_parent && !m_parent->GetOutputFile().IsEmpty())
1764 // save the graph to the output file and exit
1765 SaveGraphAs(m_parent->GetOutputFile());
1766 PostQuitMessage(0);
1768 return 0;
1771 void CRevisionGraphWnd::SetDlgTitle (bool /*offline*/)
1773 #if 0
1774 if (m_sTitle.IsEmpty())
1775 GetParent()->GetWindowText(m_sTitle);
1777 CString newTitle;
1778 if (offline)
1779 newTitle.Format (IDS_REVGRAPH_DLGTITLEOFFLINE, (LPCTSTR)m_sTitle);
1780 else
1781 newTitle = m_sTitle;
1783 CAppUtils::SetWindowTitle(GetParent()->GetSafeHwnd(), m_sPath, newTitle);
1784 #endif