Fixed issue #2180: When opening revision graph, scroll to current branch
[TortoiseGit.git] / src / TortoiseProc / RevisionGraph / RevisionGraphWnd.cpp
blob24fd790cc2d8cf3ad93062d063c20c475197a27b
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)
111 memset(&m_lfBaseFont, 0, sizeof(LOGFONT));
112 std::fill_n(m_apFonts, MAXFONTS, (CFont*)NULL);
114 WNDCLASS wndcls;
115 HINSTANCE hInst = AfxGetInstanceHandle();
116 #define REVGRAPH_CLASSNAME _T("Revgraph_windowclass")
117 if (!(::GetClassInfo(hInst, REVGRAPH_CLASSNAME, &wndcls)))
119 // otherwise we need to register a new class
120 wndcls.style = CS_DBLCLKS | CS_OWNDC;
121 wndcls.lpfnWndProc = ::DefWindowProc;
122 wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
123 wndcls.hInstance = hInst;
124 wndcls.hIcon = NULL;
125 wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
126 wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
127 wndcls.lpszMenuName = NULL;
128 wndcls.lpszClassName = REVGRAPH_CLASSNAME;
130 RegisterClass(&wndcls);
133 m_bTweakTrunkColors = CRegDWORD(_T("Software\\TortoiseGit\\RevisionGraph\\TweakTrunkColors"), TRUE) != FALSE;
134 m_bTweakTagsColors = CRegDWORD(_T("Software\\TortoiseGit\\RevisionGraph\\TweakTagsColors"), TRUE) != FALSE;
135 m_szTip[0] = 0;
136 m_wszTip[0] = 0;
138 m_GraphAttr.init(this->m_Graph, ogdf::GraphAttributes::nodeGraphics | ogdf::GraphAttributes::edgeGraphics |
139 ogdf:: GraphAttributes::nodeLabel | ogdf::GraphAttributes::nodeColor |
140 ogdf::GraphAttributes::edgeColor | ogdf::GraphAttributes::edgeStyle |
141 ogdf::GraphAttributes::nodeStyle | ogdf::GraphAttributes::nodeTemplate);
143 m_SugiyamLayout.setRanking(::new ogdf::OptimalRanking());
144 m_SugiyamLayout.setCrossMin(::new ogdf::MedianHeuristic());
146 double pi = 3.1415926;
147 m_ArrowCos = cos(pi/8);
148 m_ArrowSin = sin(pi/8);
149 this->m_ArrowSize = 8;
150 #if 0
151 ogdf::node one = this->m_Graph.newNode();
152 ogdf::node two = this->m_Graph.newNode();
153 ogdf::node three = this->m_Graph.newNode();
154 ogdf::node four = this->m_Graph.newNode();
157 m_GraphAttr.width(one)=100;
158 m_GraphAttr.height(one)=200;
159 m_GraphAttr.width(two)=100;
160 m_GraphAttr.height(two)=100;
161 m_GraphAttr.width(three)=100;
162 m_GraphAttr.height(three)=20;
163 m_GraphAttr.width(four)=100;
164 m_GraphAttr.height(four)=20;
166 m_GraphAttr.labelNode(one)="One";
167 m_GraphAttr.labelNode(two)="Two";
168 m_GraphAttr.labelNode(three)="three";
170 this->m_Graph.newEdge(one, two);
171 this->m_Graph.newEdge(one, three);
172 this->m_Graph.newEdge(two, four);
173 this->m_Graph.newEdge(three, four);
175 #endif
176 FastHierarchyLayout *pOHL = ::new FastHierarchyLayout;
177 //It will auto delte when m_SugiyamLayout destory
179 pOHL->layerDistance(30.0);
180 pOHL->nodeDistance(25.0);
182 m_SugiyamLayout.setLayout(pOHL);
184 #if 0
185 //this->m_OHL.layerDistance(30.0);
186 //this->m_OHL.nodeDistance(25.0);
187 //this->m_OHL.weightBalancing(0.8);
188 m_SugiyamLayout.setLayout(&m_OHL);
189 m_SugiyamLayout.call(m_GraphAttr);
190 #endif
191 #if 0
192 PlanarizationLayout pl;
194 FastPlanarSubgraph *ps = ::new FastPlanarSubgraph;
195 ps->runs(100);
196 VariableEmbeddingInserter *ves = ::new VariableEmbeddingInserter;
197 ves->removeReinsert(EdgeInsertionModule::rrAll);
198 pl.setSubgraph(ps);
199 pl.setInserter(ves);
201 EmbedderMinDepthMaxFaceLayers *emb = ::new EmbedderMinDepthMaxFaceLayers;
202 pl.setEmbedder(emb);
204 OrthoLayout *ol =::new OrthoLayout;
205 ol->separation(20.0);
206 ol->cOverhang(0.4);
207 ol->setOptions(2+4);
208 ol->preferedDir(OrthoDir::odEast);
209 pl.setPlanarLayouter(ol);
211 pl.call(m_GraphAttr);
213 node v;
214 forall_nodes(v,m_Graph) {
216 TRACE(_T("node x %f y %f %f %f\n"),/* m_GraphAttr.idNode(v), */
217 m_GraphAttr.x(v),
218 m_GraphAttr.y(v),
219 m_GraphAttr.width(v),
220 m_GraphAttr.height(v)
224 edge e;
225 forall_edges(e, m_Graph)
227 // get connection and point position
228 const DPolyline &dpl = this->m_GraphAttr.bends(e);
230 ListConstIterator<DPoint> it;
231 for(it = dpl.begin(); it.valid(); ++it)
233 TRACE(_T("edge %f %f\n"), (*it).m_x, (*it).m_y);
236 m_GraphAttr.writeGML("test.gml");
237 #endif
240 CRevisionGraphWnd::~CRevisionGraphWnd()
242 for (int i = 0; i < MAXFONTS; ++i)
244 if (m_apFonts[i] != NULL)
246 m_apFonts[i]->DeleteObject();
247 delete m_apFonts[i];
249 m_apFonts[i] = NULL;
251 delete m_pDlgTip;
252 m_Graph.clear();
255 void CRevisionGraphWnd::DoDataExchange(CDataExchange* pDX)
257 CWnd::DoDataExchange(pDX);
261 BEGIN_MESSAGE_MAP(CRevisionGraphWnd, CWnd)
262 ON_WM_PAINT()
263 ON_WM_ERASEBKGND()
264 ON_WM_HSCROLL()
265 ON_WM_VSCROLL()
266 ON_WM_SIZE()
267 ON_WM_LBUTTONDOWN()
268 ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
269 ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
270 ON_WM_MOUSEWHEEL()
271 ON_WM_MOUSEHWHEEL()
272 ON_WM_CONTEXTMENU()
273 ON_WM_MOUSEMOVE()
274 ON_WM_LBUTTONUP()
275 ON_WM_SETCURSOR()
276 ON_WM_TIMER()
277 ON_MESSAGE(WM_WORKERTHREADDONE,OnWorkerThreadDone)
278 ON_WM_CAPTURECHANGED()
279 END_MESSAGE_MAP()
281 void CRevisionGraphWnd::Init(CWnd * pParent, LPRECT rect)
283 WNDCLASS wndcls;
284 HINSTANCE hInst = AfxGetInstanceHandle();
285 #define REVGRAPH_CLASSNAME _T("Revgraph_windowclass")
286 if (!(::GetClassInfo(hInst, REVGRAPH_CLASSNAME, &wndcls)))
288 // otherwise we need to register a new class
289 wndcls.style = CS_DBLCLKS | CS_OWNDC;
290 wndcls.lpfnWndProc = ::DefWindowProc;
291 wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
292 wndcls.hInstance = hInst;
293 wndcls.hIcon = NULL;
294 wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
295 wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
296 wndcls.lpszMenuName = NULL;
297 wndcls.lpszClassName = REVGRAPH_CLASSNAME;
299 RegisterClass(&wndcls);
302 if (!IsWindow(m_hWnd))
303 CreateEx(WS_EX_CLIENTEDGE, REVGRAPH_CLASSNAME, _T("RevGraph"), WS_CHILD|WS_VISIBLE|WS_TABSTOP, *rect, pParent, 0);
304 m_pDlgTip = new CToolTipCtrl;
305 if(!m_pDlgTip->Create(this))
307 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Unable to add tooltip!\n");
309 EnableToolTips();
311 memset(&m_lfBaseFont, 0, sizeof(m_lfBaseFont));
312 m_lfBaseFont.lfHeight = 0;
313 m_lfBaseFont.lfWeight = FW_NORMAL;
314 m_lfBaseFont.lfItalic = FALSE;
315 m_lfBaseFont.lfCharSet = DEFAULT_CHARSET;
316 m_lfBaseFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
317 m_lfBaseFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
318 m_lfBaseFont.lfQuality = DEFAULT_QUALITY;
319 m_lfBaseFont.lfPitchAndFamily = DEFAULT_PITCH;
321 m_dwTicks = GetTickCount();
323 m_parent = dynamic_cast<CRevisionGraphDlg*>(pParent);
326 CPoint CRevisionGraphWnd::GetLogCoordinates (CPoint point) const
328 // translate point into logical coordinates
330 int nVScrollPos = GetScrollPos(SB_VERT);
331 int nHScrollPos = GetScrollPos(SB_HORZ);
333 return CPoint ( (int)((point.x + nHScrollPos) / m_fZoomFactor)
334 , (int)((point.y + nVScrollPos) / m_fZoomFactor));
337 node CRevisionGraphWnd::GetHitNode (CPoint point, CSize /*border*/) const
339 #if 0
340 // any nodes at all?
342 CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
343 if (!nodeList)
344 return index_t(NO_INDEX);
346 // search the nodes for one at that grid position
348 return nodeList->GetAt (GetLogCoordinates (point), border);
349 #endif
351 node v;
352 forall_nodes(v,m_Graph)
354 RectF noderect (GetNodeRect (v, CPoint(GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT))));
355 if(point.x>noderect.X && point.x <(noderect.X+noderect.Width) &&
356 point.y>noderect.Y && point.y <(noderect.Y+noderect.Height))
358 return v;
361 return NULL;
364 DWORD CRevisionGraphWnd::GetHoverGlyphs (CPoint /*point*/) const
366 // if there is no layout, there will be no nodes,
367 // hence, no glyphs
368 DWORD result = 0;
369 #if 0
370 CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
371 if (!nodeList)
372 return 0;
374 // get node at point or node that is close enough
375 // so that point may hit a glyph area
377 index_t nodeIndex = GetHitNode(point);
378 if (nodeIndex == NO_INDEX)
379 nodeIndex = GetHitNode(point, CSize (GLYPH_SIZE, GLYPH_SIZE / 2));
381 if (nodeIndex >= nodeList->GetCount())
382 return 0;
384 ILayoutNodeList::SNode node = nodeList->GetNode (nodeIndex);
385 const CVisibleGraphNode* base = node.node;
387 // what glyphs should be shown depending on position of point
388 // relative to the node rect?
390 CPoint logCoordinates = GetLogCoordinates (point);
391 CRect r = node.rect;
392 CPoint center = r.CenterPoint();
394 CRect rightGlyphArea ( r.right - GLYPH_SIZE, center.y - GLYPH_SIZE / 2
395 , r.right + GLYPH_SIZE, center.y + GLYPH_SIZE / 2);
396 CRect topGlyphArea ( center.x - GLYPH_SIZE, r.top - GLYPH_SIZE / 2
397 , center.x + GLYPH_SIZE, r.top + GLYPH_SIZE / 2);
398 CRect bottomGlyphArea ( center.x - GLYPH_SIZE, r.bottom - GLYPH_SIZE / 2
399 , center.x + GLYPH_SIZE, r.bottom + GLYPH_SIZE / 2);
401 bool upsideDown
402 = m_state.GetOptions()->GetOption<CUpsideDownLayout>()->IsActive();
404 if (upsideDown)
406 std::swap (topGlyphArea.top, bottomGlyphArea.top);
407 std::swap (topGlyphArea.bottom, bottomGlyphArea.bottom);
411 if (rightGlyphArea.PtInRect (logCoordinates))
412 result = base->GetFirstCopyTarget() != NULL
413 ? CGraphNodeStates::COLLAPSED_RIGHT | CGraphNodeStates::SPLIT_RIGHT
414 : 0;
416 if (topGlyphArea.PtInRect (logCoordinates))
417 result = base->GetSource() != NULL
418 ? CGraphNodeStates::COLLAPSED_ABOVE | CGraphNodeStates::SPLIT_ABOVE
419 : 0;
421 if (bottomGlyphArea.PtInRect (logCoordinates))
422 result = base->GetNext() != NULL
423 ? CGraphNodeStates::COLLAPSED_BELOW | CGraphNodeStates::SPLIT_BELOW
424 : 0;
426 // if some nodes have already been split, don't allow collapsing etc.
428 CSyncPointer<const CGraphNodeStates> nodeStates (m_state.GetNodeStates());
429 if (result & nodeStates->GetFlags (base))
430 result = 0;
431 #endif
432 return result;
434 #if 0
435 const CRevisionGraphState::SVisibleGlyph* CRevisionGraphWnd::GetHitGlyph (CPoint point) const
437 float glyphSize = GLYPH_SIZE * m_fZoomFactor;
439 CSyncPointer<const CRevisionGraphState::TVisibleGlyphs>
440 visibleGlyphs (m_state.GetVisibleGlyphs());
442 for (size_t i = 0, count = visibleGlyphs->size(); i < count; ++i)
444 const CRevisionGraphState::SVisibleGlyph* entry = &(*visibleGlyphs)[i];
446 float xRel = point.x - entry->leftTop.X;
447 float yRel = point.y - entry->leftTop.Y;
449 if ( (xRel >= 0) && (xRel < glyphSize)
450 && (yRel >= 0) && (yRel < glyphSize))
452 return entry;
456 return NULL;
458 #endif
459 void CRevisionGraphWnd::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
461 SCROLLINFO sinfo = {0};
462 sinfo.cbSize = sizeof(SCROLLINFO);
463 GetScrollInfo(SB_HORZ, &sinfo);
465 // Determine the new position of scroll box.
466 switch (nSBCode)
468 case SB_LEFT: // Scroll to far left.
469 sinfo.nPos = sinfo.nMin;
470 break;
471 case SB_RIGHT: // Scroll to far right.
472 sinfo.nPos = sinfo.nMax;
473 break;
474 case SB_ENDSCROLL: // End scroll.
475 break;
476 case SB_LINELEFT: // Scroll left.
477 if (sinfo.nPos > sinfo.nMin)
478 sinfo.nPos--;
479 break;
480 case SB_LINERIGHT: // Scroll right.
481 if (sinfo.nPos < sinfo.nMax)
482 ++sinfo.nPos;
483 break;
484 case SB_PAGELEFT: // Scroll one page left.
486 if (sinfo.nPos > sinfo.nMin)
487 sinfo.nPos = max(sinfo.nMin, sinfo.nPos - (int) sinfo.nPage);
489 break;
490 case SB_PAGERIGHT: // Scroll one page right.
492 if (sinfo.nPos < sinfo.nMax)
493 sinfo.nPos = min(sinfo.nMax, sinfo.nPos + (int) sinfo.nPage);
495 break;
496 case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
497 sinfo.nPos = sinfo.nTrackPos; // of the scroll box at the end of the drag operation.
498 break;
499 case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
500 sinfo.nPos = sinfo.nTrackPos; // position that the scroll box has been dragged to.
501 break;
503 SetScrollInfo(SB_HORZ, &sinfo);
504 Invalidate (FALSE);
505 __super::OnHScroll(nSBCode, nPos, pScrollBar);
508 void CRevisionGraphWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
510 SCROLLINFO sinfo = {0};
511 sinfo.cbSize = sizeof(SCROLLINFO);
512 GetScrollInfo(SB_VERT, &sinfo);
514 // Determine the new position of scroll box.
515 switch (nSBCode)
517 case SB_LEFT: // Scroll to far left.
518 sinfo.nPos = sinfo.nMin;
519 break;
520 case SB_RIGHT: // Scroll to far right.
521 sinfo.nPos = sinfo.nMax;
522 break;
523 case SB_ENDSCROLL: // End scroll.
524 break;
525 case SB_LINELEFT: // Scroll left.
526 if (sinfo.nPos > sinfo.nMin)
527 sinfo.nPos--;
528 break;
529 case SB_LINERIGHT: // Scroll right.
530 if (sinfo.nPos < sinfo.nMax)
531 ++sinfo.nPos;
532 break;
533 case SB_PAGELEFT: // Scroll one page left.
535 if (sinfo.nPos > sinfo.nMin)
536 sinfo.nPos = max(sinfo.nMin, sinfo.nPos - (int) sinfo.nPage);
538 break;
539 case SB_PAGERIGHT: // Scroll one page right.
541 if (sinfo.nPos < sinfo.nMax)
542 sinfo.nPos = min(sinfo.nMax, sinfo.nPos + (int) sinfo.nPage);
544 break;
545 case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
546 sinfo.nPos = sinfo.nTrackPos; // of the scroll box at the end of the drag operation.
547 break;
548 case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
549 sinfo.nPos = sinfo.nTrackPos; // position that the scroll box has been dragged to.
550 break;
552 SetScrollInfo(SB_VERT, &sinfo);
553 Invalidate(FALSE);
554 __super::OnVScroll(nSBCode, nPos, pScrollBar);
557 void CRevisionGraphWnd::OnSize(UINT nType, int cx, int cy)
559 __super::OnSize(nType, cx, cy);
560 SetScrollbars(GetScrollPos(SB_VERT), GetScrollPos(SB_HORZ));
561 Invalidate(FALSE);
564 void CRevisionGraphWnd::OnLButtonDown(UINT nFlags, CPoint point)
567 if (IsUpdateJobRunning())
568 return __super::OnLButtonDown(nFlags, point);
570 // CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
572 SetFocus();
573 bool bHit = false;
574 bool bControl = !!(GetKeyState(VK_CONTROL)&0x8000);
575 bool bOverview = m_bShowOverview && m_OverviewRect.PtInRect(point);
576 if (! bOverview)
578 #if 0
579 const CRevisionGraphState::SVisibleGlyph* hitGlyph
580 = GetHitGlyph (point);
582 if (hitGlyph != NULL)
584 ToggleNodeFlag (hitGlyph->node, hitGlyph->state);
585 return __super::OnLButtonDown(nFlags, point);
587 #endif
588 node nodeIndex = GetHitNode (point);
589 if (nodeIndex != NULL)
591 if (bControl)
593 if (m_SelectedEntry1 == nodeIndex)
595 if (m_SelectedEntry2)
597 m_SelectedEntry1 = m_SelectedEntry2;
598 m_SelectedEntry2 = NULL;
600 else
601 m_SelectedEntry1 = NULL;
603 else if (m_SelectedEntry2 == nodeIndex)
604 m_SelectedEntry2 = NULL;
605 else if (m_SelectedEntry1)
606 m_SelectedEntry2 = nodeIndex;
607 else
608 m_SelectedEntry1 = nodeIndex;
610 else
612 if (m_SelectedEntry1 == nodeIndex)
613 m_SelectedEntry1 = NULL;
614 else
615 m_SelectedEntry1 = nodeIndex;
616 m_SelectedEntry2 = NULL;
618 bHit = true;
619 Invalidate(FALSE);
623 if ((!bHit)&&(!bControl)&&(!bOverview))
625 m_SelectedEntry1 = NULL;
626 m_SelectedEntry2 = NULL;
627 m_bIsCanvasMove = true;
628 Invalidate(FALSE);
629 if (m_bShowOverview && m_OverviewRect.PtInRect(point))
630 m_bIsCanvasMove = false;
632 m_ptMoveCanvas = point;
634 UINT uEnable = MF_BYCOMMAND;
635 if ((m_SelectedEntry1 != NULL)&&(m_SelectedEntry2 != NULL))
636 uEnable |= MF_ENABLED;
637 else
638 uEnable |= MF_GRAYED;
640 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_COMPAREREVISIONS, uEnable);
641 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_UNIFIEDDIFF, uEnable);
643 uEnable = MF_BYCOMMAND;
644 if ((m_SelectedEntry1 != NULL)&&(m_SelectedEntry2 == NULL))
645 uEnable |= MF_ENABLED;
646 else
647 uEnable |= MF_GRAYED;
649 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_UNIFIEDDIFFOFHEADREVISIONS, uEnable);
650 EnableMenuItem(GetParent()->GetMenu()->m_hMenu, ID_VIEW_COMPAREHEADREVISIONS, uEnable);
652 __super::OnLButtonDown(nFlags, point);
655 void CRevisionGraphWnd::OnCaptureChanged(CWnd *pWnd)
657 __super::OnCaptureChanged(pWnd);
660 void CRevisionGraphWnd::OnLButtonUp(UINT nFlags, CPoint point)
662 if (!m_bIsCanvasMove)
663 return; // we don't have a rubberband, so no zooming necessary
665 m_bIsCanvasMove = false;
666 ReleaseCapture();
667 if (IsUpdateJobRunning())
668 return __super::OnLButtonUp(nFlags, point);
670 // zooming is finished
671 m_ptRubberEnd = CPoint(0,0);
672 CRect rect = GetClientRect();
673 int x = abs(m_ptMoveCanvas.x - point.x);
674 int y = abs(m_ptMoveCanvas.y - point.y);
676 if ((x < 20)&&(y < 20))
678 // too small zoom rectangle
679 // assume zooming by accident
680 Invalidate(FALSE);
681 __super::OnLButtonUp(nFlags, point);
682 return;
685 float xfact = float(rect.Width())/float(x);
686 float yfact = float(rect.Height())/float(y);
687 float fact = max(yfact, xfact);
689 // find out where to scroll to
690 x = min(m_ptMoveCanvas.x, point.x) + GetScrollPos(SB_HORZ);
691 y = min(m_ptMoveCanvas.y, point.y) + GetScrollPos(SB_VERT);
693 float fZoomfactor = m_fZoomFactor*fact;
694 if (fZoomfactor > 10 * MAX_ZOOM)
696 // with such a big zoomfactor, the user
697 // most likely zoomed by accident
698 Invalidate(FALSE);
699 __super::OnLButtonUp(nFlags, point);
700 return;
702 if (fZoomfactor > MAX_ZOOM)
704 fZoomfactor = MAX_ZOOM;
705 fact = fZoomfactor/m_fZoomFactor;
708 CRevisionGraphDlg * pDlg = (CRevisionGraphDlg*)GetParent();
709 if (pDlg)
711 m_fZoomFactor = fZoomfactor;
712 pDlg->DoZoom (m_fZoomFactor);
713 SetScrollbars(int(float(y)*fact), int(float(x)*fact));
715 __super::OnLButtonUp(nFlags, point);
718 bool CRevisionGraphWnd::CancelMouseZoom()
720 bool bRet = m_bIsCanvasMove;
721 ReleaseCapture();
722 if (m_bIsCanvasMove)
723 Invalidate(FALSE);
724 m_bIsCanvasMove = false;
725 m_ptRubberEnd = CPoint(0,0);
726 return bRet;
729 INT_PTR CRevisionGraphWnd::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
732 if (IsUpdateJobRunning())
733 return -1;
735 node nodeIndex = GetHitNode (point);
736 if (m_tooltipIndex != nodeIndex)
738 // force tooltip to be updated
740 m_tooltipIndex = nodeIndex;
741 return -1;
744 if (nodeIndex == NULL)
745 return -1;
747 // if ((GetHoverGlyphs (point) != 0) || (GetHitGlyph (point) != NULL))
748 // return -1;
750 pTI->hwnd = this->m_hWnd;
751 CWnd::GetClientRect(&pTI->rect);
752 pTI->uFlags |= TTF_ALWAYSTIP | TTF_IDISHWND;
753 pTI->uId = (UINT_PTR)m_hWnd;
754 pTI->lpszText = LPSTR_TEXTCALLBACK;
756 return 1;
759 BOOL CRevisionGraphWnd::OnToolTipNotify(UINT /*id*/, NMHDR *pNMHDR, LRESULT *pResult)
761 if (pNMHDR->idFrom != (UINT_PTR)m_hWnd)
762 return FALSE;
764 POINT point;
765 DWORD ptW = GetMessagePos();
766 point.x = GET_X_LPARAM(ptW);
767 point.y = GET_Y_LPARAM(ptW);
768 ScreenToClient(&point);
770 CString strTipText = TooltipText (GetHitNode (point));
772 *pResult = 0;
773 if (strTipText.IsEmpty())
774 return TRUE;
776 CSize tooltipSize = UsableTooltipRect();
777 strTipText = DisplayableText (strTipText, tooltipSize);
779 // need to handle both ANSI and UNICODE versions of the message
780 if (pNMHDR->code == TTN_NEEDTEXTA)
782 TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
783 ::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, tooltipSize.cx);
784 pTTTA->lpszText = m_szTip;
785 WideCharToMultiByte(CP_ACP, 0, strTipText, -1, m_szTip, strTipText.GetLength()+1, 0, 0);
787 else
789 TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
790 ::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, tooltipSize.cx);
791 lstrcpyn(m_wszTip, strTipText, strTipText.GetLength()+1);
792 pTTTW->lpszText = m_wszTip;
795 // show the tooltip for 32 seconds. A higher value than 32767 won't work
796 // even though it's nowhere documented!
797 ::SendMessage(pNMHDR->hwndFrom, TTM_SETDELAYTIME, TTDT_AUTOPOP, 32767);
798 return TRUE; // message was handled
801 CSize CRevisionGraphWnd::UsableTooltipRect()
803 // get screen size
805 int screenWidth = GetSystemMetrics(SM_CXSCREEN);
806 int screenHeight = GetSystemMetrics(SM_CYSCREEN);
808 // get current mouse position
810 CPoint cursorPos;
811 if (GetCursorPos (&cursorPos) == FALSE)
813 // we could not determine the mouse position
814 // use screen / 2 minus some safety margin
816 return CSize (screenWidth / 2 - 20, screenHeight / 2 - 20);
819 // tool tip will display in the biggest sector beside the cursor
820 // deduct some safety margin (for the mouse cursor itself
822 CSize biggestSector
823 ( max (screenWidth - cursorPos.x - 40, cursorPos.x - 24)
824 , max (screenHeight - cursorPos.y - 40, cursorPos.y - 24));
826 return biggestSector;
829 CString CRevisionGraphWnd::DisplayableText ( const CString& wholeText
830 , const CSize& tooltipSize)
832 CDC* dc = GetDC();
833 if (dc == NULL)
835 // no access to the device context -> truncate hard at 1000 chars
837 return wholeText.GetLength() >= MAX_TT_LENGTH_DEFAULT
838 ? wholeText.Left (MAX_TT_LENGTH_DEFAULT-4) + _T(" ...")
839 : wholeText;
842 // select the tooltip font
844 NONCLIENTMETRICS metrics;
845 metrics.cbSize = sizeof (metrics);
846 if (!SysInfo::Instance().IsVistaOrLater())
848 metrics.cbSize -= sizeof(int); // subtract the size of the iPaddedBorderWidth member which is not available on XP
850 SystemParametersInfo (SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
852 CFont font;
853 font.CreateFontIndirect(&metrics.lfStatusFont);
854 CFont* pOldFont = dc->SelectObject (&font);
856 // split into lines and fill the tooltip rect
858 CString result;
860 int remainingHeight = tooltipSize.cy;
861 int pos = 0;
862 while (pos < wholeText.GetLength())
864 // extract a whole line
866 int nextPos = wholeText.Find ('\n', pos);
867 if (nextPos < 0)
868 nextPos = wholeText.GetLength();
870 CString line = wholeText.Mid (pos, nextPos-pos+1);
872 // find a way to make it fit
874 CSize size = dc->GetTextExtent (line);
875 while (size.cx > tooltipSize.cx)
877 line.Delete (line.GetLength()-1);
878 int nextPos2 = line.ReverseFind (' ');
879 if (nextPos2 < 0)
880 break;
882 line.Delete (nextPos2+1, line.GetLength() - pos-1);
883 size = dc->GetTextExtent (line);
886 // enough room for the new line?
888 remainingHeight -= size.cy;
889 if (remainingHeight <= size.cy)
891 result += _T("...");
892 break;
895 // add the line
897 result += line;
898 pos += line.GetLength();
901 // relase temp. resources
903 dc->SelectObject (pOldFont);
904 ReleaseDC(dc);
906 // ready
908 return result;
911 CString CRevisionGraphWnd::TooltipText(node index)
913 if(index)
915 CString str;
916 CGitHash hash = m_logEntries[index->index()];
917 GitRev *rev = this->m_LogCache.GetCacheData(hash);
918 str += rev->m_CommitHash.ToString();
919 str += _T("\n");
920 str += rev->GetAuthorName() +_T(" ") + rev->GetAuthorEmail();
921 str += _T(" ");
922 str += rev->GetAuthorDate().Format(_T("%Y-%m-%d %H:%M"));
923 str += _T("\n\n")+rev->GetSubject();
924 str += _T("\n");
925 str += rev->GetBody();
926 return str;
928 }else
929 return CString();
932 void CRevisionGraphWnd::SaveGraphAs(CString sSavePath)
935 CString extension = CPathUtils::GetFileExtFromPath(sSavePath);
936 if (extension.CompareNoCase(_T(".wmf"))==0)
938 // save the graph as an enhanced metafile
939 CMetaFileDC wmfDC;
940 wmfDC.CreateEnhanced(NULL, sSavePath, NULL, _T("TortoiseGit\0Revision Graph\0\0"));
941 float fZoom = m_fZoomFactor;
942 m_fZoomFactor = DEFAULT_ZOOM;
943 DoZoom(m_fZoomFactor);
944 CRect rect;
945 rect = GetViewRect();
946 GraphicsDevice dev;
947 dev.pDC = &wmfDC;
948 DrawGraph(dev, rect, 0, 0, true);
949 HENHMETAFILE hemf = wmfDC.CloseEnhanced();
950 DeleteEnhMetaFile(hemf);
951 m_fZoomFactor = fZoom;
952 DoZoom(m_fZoomFactor);
954 else if (extension.CompareNoCase(_T(".svg"))==0)
956 // save the graph as a scalable vector graphic
957 SVG svg;
958 float fZoom = m_fZoomFactor;
959 m_fZoomFactor = DEFAULT_ZOOM;
960 DoZoom(m_fZoomFactor);
961 CRect rect;
962 rect = GetViewRect();
963 svg.SetViewSize(rect.Width(), rect.Height());
964 GraphicsDevice dev;
965 dev.pSVG = &svg;
966 DrawGraph(dev, rect, 0, 0, true);
967 svg.Save(sSavePath);
968 m_fZoomFactor = fZoom;
969 DoZoom(m_fZoomFactor);
971 else if (extension.CompareNoCase(_T(".gv")) == 0)
973 Graphviz graphviz;
974 float fZoom = m_fZoomFactor;
975 m_fZoomFactor = DEFAULT_ZOOM;
976 DoZoom(m_fZoomFactor);
977 CRect rect;
978 rect = GetViewRect();
979 GraphicsDevice dev;
980 dev.pGraphviz = &graphviz;
981 DrawGraph(dev, rect, 0, 0, true);
982 graphviz.Save(sSavePath);
983 m_fZoomFactor = fZoom;
984 DoZoom(m_fZoomFactor);
986 else
988 // save the graph as a pixel picture instead of a vector picture
989 // create dc to paint on
992 CString sErrormessage;
993 CWindowDC ddc(this);
994 CDC dc;
995 if (!dc.CreateCompatibleDC(&ddc))
997 CFormatMessageWrapper errorDetails;
998 if( errorDetails )
999 MessageBox( errorDetails, _T("Error"), MB_OK | MB_ICONINFORMATION );
1001 return;
1003 CRect rect;
1004 rect = GetGraphRect();
1005 rect.bottom = (LONG)(float(rect.Height()) * m_fZoomFactor);
1006 rect.right = (LONG)(float(rect.Width()) * m_fZoomFactor);
1007 BITMAPINFO bmi;
1008 HBITMAP hbm;
1009 LPBYTE pBits;
1010 // Initialize header to 0s.
1011 SecureZeroMemory(&bmi, sizeof(bmi));
1012 // Fill out the fields you care about.
1013 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1014 bmi.bmiHeader.biWidth = rect.Width();
1015 bmi.bmiHeader.biHeight = rect.Height();
1016 bmi.bmiHeader.biPlanes = 1;
1017 bmi.bmiHeader.biBitCount = 24;
1018 bmi.bmiHeader.biCompression = BI_RGB;
1020 // Create the surface.
1021 hbm = CreateDIBSection(ddc.m_hDC, &bmi, DIB_RGB_COLORS,(void **)&pBits, NULL, 0);
1022 if (hbm==0)
1024 CMessageBox::Show(m_hWnd, IDS_REVGRAPH_ERR_NOMEMORY, IDS_APPNAME, MB_ICONERROR);
1025 return;
1027 HBITMAP oldbm = (HBITMAP)dc.SelectObject(hbm);
1028 // paint the whole graph
1029 GraphicsDevice dev;
1030 dev.pDC = &dc;
1031 DrawGraph(dev, rect, 0, 0, true);
1032 // now use GDI+ to save the picture
1033 CLSID encoderClsid;
1035 Bitmap bitmap(hbm, NULL);
1036 if (bitmap.GetLastStatus()==Ok)
1038 // Get the CLSID of the encoder.
1039 int ret = 0;
1040 if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".png"))==0)
1041 ret = GetEncoderClsid(L"image/png", &encoderClsid);
1042 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".jpg"))==0)
1043 ret = GetEncoderClsid(L"image/jpeg", &encoderClsid);
1044 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".jpeg"))==0)
1045 ret = GetEncoderClsid(L"image/jpeg", &encoderClsid);
1046 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".bmp"))==0)
1047 ret = GetEncoderClsid(L"image/bmp", &encoderClsid);
1048 else if (CPathUtils::GetFileExtFromPath(sSavePath).CompareNoCase(_T(".gif"))==0)
1049 ret = GetEncoderClsid(L"image/gif", &encoderClsid);
1050 else
1052 sSavePath += _T(".jpg");
1053 ret = GetEncoderClsid(L"image/jpeg", &encoderClsid);
1055 if (ret >= 0)
1057 CStringW tfile = CStringW(sSavePath);
1058 bitmap.Save(tfile, &encoderClsid, NULL);
1060 else
1062 sErrormessage.Format(IDS_REVGRAPH_ERR_NOENCODER, (LPCTSTR)CPathUtils::GetFileExtFromPath(sSavePath));
1065 else
1067 sErrormessage.LoadString(IDS_REVGRAPH_ERR_NOBITMAP);
1070 dc.SelectObject(oldbm);
1071 DeleteObject(hbm);
1072 dc.DeleteDC();
1073 if (!sErrormessage.IsEmpty())
1075 ::MessageBox(m_hWnd, sErrormessage, _T("TortoiseGit"), MB_ICONERROR);
1078 catch (CException * pE)
1080 TCHAR szErrorMsg[2048] = { 0 };
1081 pE->GetErrorMessage(szErrorMsg, 2048);
1082 pE->Delete();
1083 ::MessageBox(m_hWnd, szErrorMsg, _T("TortoiseGit"), MB_ICONERROR);
1089 BOOL CRevisionGraphWnd::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
1091 if (IsUpdateJobRunning())
1092 return __super::OnMouseWheel(nFlags, zDelta, pt);
1094 if (GetKeyState(VK_CONTROL)&0x8000)
1096 float newZoom = m_fZoomFactor * (zDelta < 0 ? ZOOM_STEP : 1.0f/ZOOM_STEP);
1097 DoZoom (max (MIN_ZOOM, min (MAX_ZOOM, newZoom)));
1099 else
1101 int orientation = GetKeyState(VK_SHIFT)&0x8000 ? SB_HORZ : SB_VERT;
1102 int pos = GetScrollPos(orientation);
1103 pos -= (zDelta);
1104 SetScrollPos(orientation, pos);
1105 Invalidate(FALSE);
1107 return __super::OnMouseWheel(nFlags, zDelta, pt);
1110 void CRevisionGraphWnd::OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt)
1112 if (IsUpdateJobRunning())
1113 return __super::OnMouseHWheel(nFlags, zDelta, pt);
1115 int orientation = GetKeyState(VK_SHIFT)&0x8000 ? SB_VERT : SB_HORZ;
1116 int pos = GetScrollPos(orientation);
1117 pos -= (zDelta);
1118 SetScrollPos(orientation, pos);
1119 Invalidate(FALSE);
1121 return __super::OnMouseHWheel(nFlags, zDelta, pt);
1124 bool CRevisionGraphWnd::UpdateSelectedEntry (node clickedentry)
1126 if ((m_SelectedEntry1 == NULL)&&(clickedentry == NULL))
1127 return false;
1129 if (m_SelectedEntry1 == NULL)
1131 m_SelectedEntry1 = clickedentry;
1132 Invalidate(FALSE);
1134 if ((m_SelectedEntry2 == NULL)&&(clickedentry != m_SelectedEntry1))
1136 m_SelectedEntry1 = clickedentry;
1137 Invalidate(FALSE);
1139 if (m_SelectedEntry1 && m_SelectedEntry2)
1141 if ((m_SelectedEntry2 != clickedentry)&&(m_SelectedEntry1 != clickedentry))
1142 return false;
1144 if (m_SelectedEntry1 == NULL)
1145 return false;
1147 return true;
1150 void CRevisionGraphWnd::AppendMenu
1151 ( CMenu& popup
1152 , UINT title
1153 , UINT command
1154 , UINT flags)
1156 // separate different groups / section within the context menu
1158 if (popup.GetMenuItemCount() > 0)
1160 UINT lastCommand = popup.GetMenuItemID (popup.GetMenuItemCount()-1);
1161 if ((lastCommand & GROUP_MASK) != (command & GROUP_MASK))
1162 popup.AppendMenu(MF_SEPARATOR, NULL);
1165 // actually add the new item
1167 CString titleString;
1168 titleString.LoadString (title);
1169 popup.AppendMenu (MF_STRING | flags, command, titleString);
1172 void CRevisionGraphWnd::AppendMenu(CMenu &popup, CString title, UINT command, CString *extra, CMenu *submenu)
1174 // separate different groups / section within the context menu
1175 if (popup.GetMenuItemCount() > 0)
1177 UINT lastCommand = popup.GetMenuItemID(popup.GetMenuItemCount() - 1);
1178 if ((lastCommand & GROUP_MASK) != (command & GROUP_MASK))
1179 popup.AppendMenu(MF_SEPARATOR, NULL);
1182 // actually add the new item
1183 MENUITEMINFO mii;
1184 memset(&mii, 0, sizeof(mii));
1185 mii.cbSize = sizeof(MENUITEMINFO);
1186 mii.fMask = MIIM_STRING | MIIM_ID | (extra ? MIIM_DATA : 0) | (submenu ? MIIM_SUBMENU : 0);
1187 mii.wID = command;
1188 mii.hSubMenu = submenu ? submenu->m_hMenu : NULL;
1189 mii.dwItemData = (ULONG_PTR)extra;
1190 mii.dwTypeData = title.GetBuffer();
1191 InsertMenuItem(popup, popup.GetMenuItemCount(), TRUE, &mii);
1192 title.ReleaseBuffer();
1195 void CRevisionGraphWnd::AddGraphOps (CMenu& /*popup*/, const CVisibleGraphNode * /*node*/)
1197 #if 0
1198 CSyncPointer<CGraphNodeStates> nodeStates (m_state.GetNodeStates());
1200 if (node == NULL)
1202 DWORD state = nodeStates->GetCombinedFlags();
1203 if (state != 0)
1205 if (state & CGraphNodeStates::COLLAPSED_ALL)
1206 AppendMenu (popup, IDS_REVGRAPH_POPUP_EXPAND_ALL, ID_EXPAND_ALL);
1208 if (state & CGraphNodeStates::SPLIT_ALL)
1209 AppendMenu (popup, IDS_REVGRAPH_POPUP_JOIN_ALL, ID_JOIN_ALL);
1212 else
1214 DWORD state = nodeStates->GetFlags (node);
1216 if (node->GetSource() || (state & CGraphNodeStates::COLLAPSED_ABOVE))
1217 AppendMenu ( popup
1218 , (state & CGraphNodeStates::COLLAPSED_ABOVE)
1219 ? IDS_REVGRAPH_POPUP_EXPAND_ABOVE
1220 : IDS_REVGRAPH_POPUP_COLLAPSE_ABOVE
1221 , ID_GRAPH_EXPANDCOLLAPSE_ABOVE);
1223 if (node->GetFirstCopyTarget() || (state & CGraphNodeStates::COLLAPSED_RIGHT))
1224 AppendMenu ( popup
1225 , (state & CGraphNodeStates::COLLAPSED_RIGHT)
1226 ? IDS_REVGRAPH_POPUP_EXPAND_RIGHT
1227 : IDS_REVGRAPH_POPUP_COLLAPSE_RIGHT
1228 , ID_GRAPH_EXPANDCOLLAPSE_RIGHT);
1230 if (node->GetNext() || (state & CGraphNodeStates::COLLAPSED_BELOW))
1231 AppendMenu ( popup
1232 , (state & CGraphNodeStates::COLLAPSED_BELOW)
1233 ? IDS_REVGRAPH_POPUP_EXPAND_BELOW
1234 : IDS_REVGRAPH_POPUP_COLLAPSE_BELOW
1235 , ID_GRAPH_EXPANDCOLLAPSE_BELOW);
1237 if (node->GetSource() || (state & CGraphNodeStates::SPLIT_ABOVE))
1238 AppendMenu ( popup
1239 , (state & CGraphNodeStates::SPLIT_ABOVE)
1240 ? IDS_REVGRAPH_POPUP_JOIN_ABOVE
1241 : IDS_REVGRAPH_POPUP_SPLIT_ABOVE
1242 , ID_GRAPH_SPLITJOIN_ABOVE);
1244 if (node->GetFirstCopyTarget() || (state & CGraphNodeStates::SPLIT_RIGHT))
1245 AppendMenu ( popup
1246 , (state & CGraphNodeStates::SPLIT_RIGHT)
1247 ? IDS_REVGRAPH_POPUP_JOIN_RIGHT
1248 : IDS_REVGRAPH_POPUP_SPLIT_RIGHT
1249 , ID_GRAPH_SPLITJOIN_RIGHT);
1251 if (node->GetNext() || (state & CGraphNodeStates::SPLIT_BELOW))
1252 AppendMenu ( popup
1253 , (state & CGraphNodeStates::SPLIT_BELOW)
1254 ? IDS_REVGRAPH_POPUP_JOIN_BELOW
1255 : IDS_REVGRAPH_POPUP_SPLIT_BELOW
1256 , ID_GRAPH_SPLITJOIN_BELOW);
1258 #endif
1261 CString CRevisionGraphWnd::GetSelectedURL() const
1263 #if 0
1264 if (m_SelectedEntry1 == NULL)
1265 return CString();
1267 CString URL = m_state.GetRepositoryRoot()
1268 + CUnicodeUtils::GetUnicode (m_SelectedEntry1->GetPath().GetPath().c_str());
1269 URL = CUnicodeUtils::GetUnicode(CPathUtils::PathEscape(CUnicodeUtils::GetUTF8(URL)));
1271 return URL;
1272 #endif
1273 return CString();
1276 CString CRevisionGraphWnd::GetWCURL() const
1278 #if 0
1279 CTGitPath path (m_sPath);
1280 if (path.IsUrl())
1281 return CString();
1283 SVNInfo info;
1284 const SVNInfoData * status
1285 = info.GetFirstFileInfo (path, SVNRev(), SVNRev());
1287 return status == NULL ? CString() : status->url;
1288 #endif
1289 return CString();
1292 void CRevisionGraphWnd::DoShowLog()
1295 if(m_SelectedEntry1 == NULL)
1296 return;
1298 CString sCmd;
1300 if(m_SelectedEntry2 != NULL)
1301 sCmd.Format(_T("/command:log %s /startrev:%s /endrev:%s"),
1302 this->m_sPath.IsEmpty() ? _T("") : (_T("/path:\"") + this->m_sPath + _T("\"")),
1303 this->m_logEntries[m_SelectedEntry1->index()].ToString(),
1304 this->m_logEntries[m_SelectedEntry2->index()].ToString());
1305 else
1306 sCmd.Format(_T("/command:log %s /endrev:%s"),
1307 this->m_sPath.IsEmpty() ? _T("") : (_T("/path:\"") + this->m_sPath + _T("\"")),
1308 this->m_logEntries[m_SelectedEntry1->index()].ToString());
1310 CAppUtils::RunTortoiseGitProc(sCmd);
1314 void CRevisionGraphWnd::DoCheckForModification()
1316 CChangedDlg dlg;
1317 dlg.m_pathList = CTGitPathList (CTGitPath (m_sPath));
1318 dlg.DoModal();
1321 void CRevisionGraphWnd::DoMergeTo()
1323 #if 0
1324 CString URL = GetSelectedURL();
1325 CString path = m_sPath;
1326 CBrowseFolder folderBrowser;
1327 folderBrowser.SetInfo(CString(MAKEINTRESOURCE(IDS_LOG_MERGETO)));
1328 if (folderBrowser.Show(GetSafeHwnd(), path, path) == CBrowseFolder::OK)
1330 CSVNProgressDlg dlg;
1331 dlg.SetCommand(CSVNProgressDlg::SVNProgress_Merge);
1332 dlg.SetPathList(CTGitPathList(CTGitPath(path)));
1333 dlg.SetUrl(URL);
1334 dlg.SetSecondUrl(URL);
1335 SVNRevRangeArray revarray;
1336 revarray.AddRevRange (m_SelectedEntry1->GetRevision()-1, svn_revnum_t(m_SelectedEntry1->GetRevision()));
1337 dlg.SetRevisionRanges(revarray);
1338 dlg.DoModal();
1340 #endif
1343 void CRevisionGraphWnd::DoUpdate()
1345 #if 0
1346 CSVNProgressDlg progDlg;
1347 progDlg.SetCommand (CSVNProgressDlg::SVNProgress_Update);
1348 progDlg.SetOptions (0); // don't ignore externals
1349 progDlg.SetPathList (CTGitPathList (CTGitPath (m_sPath)));
1350 progDlg.SetRevision (m_SelectedEntry1->GetRevision());
1351 progDlg.SetDepth();
1352 progDlg.DoModal();
1354 if (m_state.GetFetchedWCState())
1355 m_parent->UpdateFullHistory();
1356 #endif
1359 void CRevisionGraphWnd::DoSwitch(CString rev)
1361 CAppUtils::PerformSwitch(rev);
1364 void CRevisionGraphWnd::DoSwitchToHead()
1366 #if 0
1367 CSVNProgressDlg progDlg;
1368 progDlg.SetCommand (CSVNProgressDlg::SVNProgress_Switch);
1369 progDlg.SetPathList (CTGitPathList (CTGitPath (m_sPath)));
1370 progDlg.SetUrl (GetSelectedURL());
1371 progDlg.SetRevision (SVNRev::REV_HEAD);
1372 progDlg.SetPegRevision (m_SelectedEntry1->GetRevision());
1373 progDlg.DoModal();
1375 if (m_state.GetFetchedWCState())
1376 m_parent->UpdateFullHistory();
1377 #endif
1380 void CRevisionGraphWnd::DoBrowseRepo()
1382 if (m_SelectedEntry1 == NULL)
1383 return;
1385 CString sCmd;
1386 sCmd.Format(_T("/command:repobrowser %s /rev:%s"),
1387 this->m_sPath.IsEmpty() ? _T("") : (_T("/path:\"") + this->m_sPath + _T("\"")),
1388 GetFriendRefName(m_SelectedEntry1));
1390 CAppUtils::RunTortoiseGitProc(sCmd);
1393 void CRevisionGraphWnd::ResetNodeFlags (DWORD /*flags*/)
1395 // m_state.GetNodeStates()->ResetFlags (flags);
1396 // m_parent->StartWorkerThread();
1399 void CRevisionGraphWnd::ToggleNodeFlag (const CVisibleGraphNode * /*node*/, DWORD /*flag*/)
1401 #if 0
1402 CSyncPointer<CGraphNodeStates> nodeStates (m_state.GetNodeStates());
1404 if (nodeStates->GetFlags (node) & flag)
1405 nodeStates->ResetFlags (node, flag);
1406 else
1407 nodeStates->SetFlags (node, flag);
1409 m_parent->StartWorkerThread();
1410 #endif
1413 void CRevisionGraphWnd::DoCopyRefs()
1415 if (m_SelectedEntry1 == NULL)
1416 return;
1418 STRING_VECTOR list = GetFriendRefNames(m_SelectedEntry1);
1419 CString text;
1420 if (list.empty())
1421 text = m_logEntries[m_SelectedEntry1->index()].ToString();
1422 for (size_t i = 0; i < list.size(); ++i)
1424 if (i > 0)
1425 text.Append(_T("\r\n"));
1426 text.Append(list[i]);
1428 CStringUtils::WriteAsciiStringToClipboard(text, m_hWnd);
1431 void CRevisionGraphWnd::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
1433 if (IsUpdateJobRunning())
1434 return;
1436 CPoint clientpoint = point;
1437 this->ScreenToClient(&clientpoint);
1439 node nodeIndex = GetHitNode (clientpoint);
1441 if ( !UpdateSelectedEntry (nodeIndex))
1443 return;
1446 CMenu popup;
1447 if (!popup.CreatePopupMenu())
1448 return;
1450 bool bothPresent = (m_SelectedEntry2 && m_SelectedEntry1);
1452 AppendMenu (popup, IDS_REPOBROWSE_SHOWLOG, ID_SHOWLOG);
1454 STRING_VECTOR branchNames;
1455 if (m_SelectedEntry1 && (m_SelectedEntry2 == nullptr))
1457 AppendMenu(popup, IDS_LOG_BROWSEREPO, ID_BROWSEREPO);
1459 CString currentBranch = g_Git.GetCurrentBranch();
1460 CGit::REF_TYPE refType = CGit::LOCAL_BRANCH;
1461 STRING_VECTOR allBranchNames = GetFriendRefNames(m_SelectedEntry1, &refType, 1);
1462 for (size_t i = 0; i < allBranchNames.size(); ++i)
1463 if (allBranchNames[i] != currentBranch)
1464 branchNames.push_back(allBranchNames[i]);
1465 if (branchNames.size() == 1)
1467 CString text;
1468 text.Format(_T("%s \"%s\""), CString(MAKEINTRESOURCE(IDS_SWITCH_BRANCH)), branchNames[0]);
1469 AppendMenu(popup, text, ID_SWITCH, &branchNames[0]);
1471 else if (branchNames.size() > 1)
1473 CMenu switchMenu;
1474 switchMenu.CreatePopupMenu();
1475 for (size_t i = 0; i < branchNames.size(); ++i)
1476 AppendMenu(switchMenu, branchNames[i], ID_SWITCH + ((int)(i + 1) << 16), &branchNames[i]);
1477 AppendMenu(popup, CString(MAKEINTRESOURCE(IDS_SWITCH_BRANCH)), ID_SWITCH, NULL, &switchMenu);
1480 AppendMenu(popup, IDS_COPY_REF_NAMES, ID_COPYREFS);
1482 AppendMenu(popup, IDS_REVGRAPH_POPUP_COMPAREHEADS, ID_COMPAREHEADS);
1483 AppendMenu(popup, IDS_REVGRAPH_POPUP_UNIDIFFHEADS, ID_UNIDIFFHEADS);
1485 AppendMenu(popup, IDS_LOG_POPUP_COMPARE, ID_COMPAREWT);
1488 if (bothPresent)
1490 AppendMenu (popup, IDS_REVGRAPH_POPUP_COMPAREREVS, ID_COMPAREREVS);
1491 AppendMenu (popup, IDS_REVGRAPH_POPUP_UNIDIFFREVS, ID_UNIDIFFREVS);
1494 // AddGraphOps (popup, clickedentry);
1496 // if the context menu is invoked through the keyboard, we have to use
1497 // a calculated position on where to anchor the menu on
1498 if ((point.x == -1) && (point.y == -1))
1500 CRect rect = GetWindowRect();
1501 point = rect.CenterPoint();
1504 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON, point.x, point.y, this, 0);
1505 switch (cmd & 0xFFFF)
1507 case ID_COMPAREREVS:
1508 if (m_SelectedEntry1 != NULL)
1509 CompareRevs(false);
1510 break;
1511 case ID_UNIDIFFREVS:
1512 if (m_SelectedEntry1 != NULL)
1513 UnifiedDiffRevs(false);
1514 break;
1515 case ID_UNIDIFFHEADS:
1516 if (m_SelectedEntry1 != NULL)
1517 UnifiedDiffRevs(true);
1518 break;
1519 case ID_SHOWLOG:
1520 DoShowLog();
1521 break;
1522 case ID_SWITCH:
1524 MENUITEMINFO mii;
1525 memset(&mii, 0, sizeof(mii));
1526 mii.cbSize = sizeof(mii);
1527 mii.fMask |= MIIM_DATA;
1528 GetMenuItemInfo(popup, cmd, FALSE, &mii);
1529 CString *rev = (CString *)mii.dwItemData;
1530 if (rev != NULL)
1532 DoSwitch(*rev);
1533 m_parent->UpdateFullHistory();
1535 break;
1537 case ID_COPYREFS:
1538 DoCopyRefs();
1539 break;
1540 case ID_BROWSEREPO:
1541 DoBrowseRepo();
1542 break;
1543 case ID_COMPAREHEADS:
1544 if (m_SelectedEntry1 != NULL)
1545 CompareRevs(_T("HEAD"));
1546 break;
1547 case ID_COMPAREWT:
1548 if (m_SelectedEntry1 != NULL)
1549 CompareRevs(CGitHash().ToString());
1550 break;
1552 #if 0
1553 case ID_COMPAREREVS:
1554 if (m_SelectedEntry1 != NULL)
1555 CompareRevs(false);
1556 break;
1557 case ID_UNIDIFFREVS:
1558 if (m_SelectedEntry1 != NULL)
1559 UnifiedDiffRevs(false);
1560 break;
1561 case ID_UNIDIFFHEADS:
1562 if (m_SelectedEntry1 != NULL)
1563 UnifiedDiffRevs(true);
1564 break;
1565 case ID_SHOWLOG:
1566 DoShowLog();
1567 break;
1568 case ID_CFM:
1569 DoCheckForModification();
1570 break;
1571 case ID_MERGETO:
1572 DoMergeTo();
1573 break;
1574 case ID_UPDATE:
1575 DoUpdate();
1576 break;
1577 case ID_SWITCHTOHEAD:
1578 DoSwitchToHead();
1579 break;
1580 case ID_EXPAND_ALL:
1581 ResetNodeFlags (CGraphNodeStates::COLLAPSED_ALL);
1582 break;
1583 case ID_JOIN_ALL:
1584 ResetNodeFlags (CGraphNodeStates::SPLIT_ALL);
1585 break;
1586 case ID_GRAPH_EXPANDCOLLAPSE_ABOVE:
1587 ToggleNodeFlag (clickedentry, CGraphNodeStates::COLLAPSED_ABOVE);
1588 break;
1589 case ID_GRAPH_EXPANDCOLLAPSE_RIGHT:
1590 ToggleNodeFlag (clickedentry, CGraphNodeStates::COLLAPSED_RIGHT);
1591 break;
1592 case ID_GRAPH_EXPANDCOLLAPSE_BELOW:
1593 ToggleNodeFlag (clickedentry, CGraphNodeStates::COLLAPSED_BELOW);
1594 break;
1595 case ID_GRAPH_SPLITJOIN_ABOVE:
1596 ToggleNodeFlag (clickedentry, CGraphNodeStates::SPLIT_ABOVE);
1597 break;
1598 case ID_GRAPH_SPLITJOIN_RIGHT:
1599 ToggleNodeFlag (clickedentry, CGraphNodeStates::SPLIT_RIGHT);
1600 break;
1601 case ID_GRAPH_SPLITJOIN_BELOW:
1602 ToggleNodeFlag (clickedentry, CGraphNodeStates::SPLIT_BELOW);
1603 break;
1604 #endif
1609 void CRevisionGraphWnd::OnMouseMove(UINT nFlags, CPoint point)
1612 if (IsUpdateJobRunning())
1614 return __super::OnMouseMove(nFlags, point);
1616 if (!m_bIsCanvasMove)
1618 if (m_bShowOverview && (m_OverviewRect.PtInRect(point))&&(nFlags & MK_LBUTTON))
1620 // scrolling
1621 CRect viewRect = GetViewRect();
1622 int x = (int)((point.x-m_OverviewRect.left - (m_OverviewPosRect.Width()/2)) / m_previewZoom * m_fZoomFactor);
1623 int y = (int)((point.y - m_OverviewRect.top - (m_OverviewPosRect.Height()/2)) / m_previewZoom * m_fZoomFactor);
1624 x = max(0, x);
1625 y = max(0, y);
1626 SetScrollbars(y, x);
1627 Invalidate(FALSE);
1628 return __super::OnMouseMove(nFlags, point);
1630 else
1632 // update screen if we hover over a different
1633 // node than during the last redraw
1635 CPoint clientPoint = point;
1636 GetCursorPos (&clientPoint);
1637 ScreenToClient (&clientPoint);
1639 #if 0
1640 const CRevisionGraphState::SVisibleGlyph* hitGlyph
1641 = GetHitGlyph (clientPoint);
1642 const CFullGraphNode* glyphNode
1643 = hitGlyph ? hitGlyph->node->GetBase() : NULL;
1645 const CFullGraphNode* hoverNode = NULL;
1646 if (m_hoverIndex != NO_INDEX)
1648 CSyncPointer<const ILayoutNodeList> nodeList (m_state.GetNodes());
1649 if (m_hoverIndex < nodeList->GetCount())
1650 hoverNode = nodeList->GetNode (m_hoverIndex).node->GetBase();
1653 //bool onHoverNodeGlyph = (hoverNode != NULL) && (glyphNode == hoverNode);
1654 if ( !m_hoverIndex
1655 && ( (m_hoverIndex != GetHitNode (clientPoint))))
1657 m_showHoverGlyphs = false;
1659 KillTimer (GLYPH_HOVER_EVENT);
1660 SetTimer (GLYPH_HOVER_EVENT, GLYPH_HOVER_DELAY, NULL);
1662 Invalidate(FALSE);
1664 #endif
1665 return __super::OnMouseMove(nFlags, point);
1668 SetCapture();
1670 int pos_h = GetScrollPos(SB_HORZ);
1671 pos_h -= point.x - m_ptMoveCanvas.x;
1672 SetScrollPos(SB_HORZ, pos_h);
1674 int pos_v = GetScrollPos(SB_VERT);
1675 pos_v -= point.y - m_ptMoveCanvas.y;
1676 SetScrollPos(SB_VERT, pos_v);
1678 m_ptMoveCanvas = point;
1680 this->Invalidate();
1682 __super::OnMouseMove(nFlags, point);
1685 BOOL CRevisionGraphWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
1687 CRect viewRect = GetViewRect();
1689 LPTSTR cursorID = IDC_ARROW;
1690 HINSTANCE resourceHandle = NULL;
1692 if ((nHitTest == HTCLIENT)&&(pWnd == this)&&(viewRect.Width())&&(viewRect.Height())&&(message))
1694 POINT pt;
1695 if (GetCursorPos(&pt))
1697 ScreenToClient(&pt);
1698 if (m_OverviewPosRect.PtInRect(pt))
1700 resourceHandle = AfxGetResourceHandle();
1701 cursorID = GetKeyState(VK_LBUTTON) & 0x8000
1702 ? MAKEINTRESOURCE(IDC_PANCURDOWN)
1703 : MAKEINTRESOURCE(IDC_PANCUR);
1705 if (m_bIsCanvasMove)
1706 cursorID = IDC_HAND;
1710 HCURSOR hCur = LoadCursor(resourceHandle, MAKEINTRESOURCE(cursorID));
1711 if (GetCursor() != hCur)
1712 SetCursor (hCur);
1714 return TRUE;
1717 void CRevisionGraphWnd::OnTimer (UINT_PTR nIDEvent)
1719 if (nIDEvent == GLYPH_HOVER_EVENT)
1721 KillTimer (GLYPH_HOVER_EVENT);
1723 m_showHoverGlyphs = true;
1724 Invalidate (FALSE);
1726 else
1728 __super::OnTimer (nIDEvent);
1732 LRESULT CRevisionGraphWnd::OnWorkerThreadDone(WPARAM, LPARAM)
1734 // handle potential race condition between PostMessage and leaving job:
1735 // the background job may not have exited, yet
1737 if (updateJob.get())
1738 updateJob->GetResult();
1740 InitView();
1741 BuildPreview();
1743 if (m_HeadNode)
1745 SCROLLINFO sinfo = { 0 };
1746 sinfo.cbSize = sizeof(SCROLLINFO);
1747 if (GetScrollInfo(SB_HORZ, &sinfo))
1749 sinfo.nPos = (int)min(max(sinfo.nMin, m_GraphAttr.x(m_HeadNode) - m_GraphAttr.width(m_HeadNode) / 2), sinfo.nMax);
1750 SetScrollInfo(SB_HORZ, &sinfo);
1752 if (GetScrollInfo(SB_VERT, &sinfo))
1754 sinfo.nPos = (int)min(max(sinfo.nMin, m_GraphAttr.y(m_HeadNode) - m_GraphAttr.height(m_HeadNode) / 2), sinfo.nMax);
1755 SetScrollInfo(SB_VERT, &sinfo);
1759 Invalidate(FALSE);
1761 if (m_parent && !m_parent->GetOutputFile().IsEmpty())
1763 // save the graph to the output file and exit
1764 SaveGraphAs(m_parent->GetOutputFile());
1765 PostQuitMessage(0);
1767 return 0;
1770 void CRevisionGraphWnd::SetDlgTitle (bool /*offline*/)
1772 #if 0
1773 if (m_sTitle.IsEmpty())
1774 GetParent()->GetWindowText(m_sTitle);
1776 CString newTitle;
1777 if (offline)
1778 newTitle.Format (IDS_REVGRAPH_DLGTITLEOFFLINE, (LPCTSTR)m_sTitle);
1779 else
1780 newTitle = m_sTitle;
1782 CAppUtils::SetWindowTitle(GetParent()->GetSafeHwnd(), m_sPath, newTitle);
1783 #endif