Update rebase documentation
[TortoiseGit.git] / src / Utils / Graphviz.cpp
blob017ee1cc3230bd8c239b40dc32f8ebea83898d30
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "Graphviz.h"
21 #include "UnicodeUtils.h"
22 #include "SmartHandle.h"
24 void Graphviz::DrawNode(CString id, CString text, CString fontName, int fontSize, Gdiplus::Color /*borderColor*/, Gdiplus::Color backColor, int /*height*/)
26 content.Append(_T("\t"));
27 content.Append(id);
29 CString format;
30 format.Format(_T(" [label=\"%s\""), text);
31 content.Append(format);
32 if (m_defaultFontName != fontName)
34 format.Format(_T(", fontname=\"%s\""), fontName);
35 content.Append(format);
38 if (m_defaultFontSize != fontSize)
40 format.Format(_T(", fontsize=\"%d\""), fontSize);
41 content.Append(format);
44 if (m_defaultBackColor.GetValue() != backColor.GetValue())
46 format.Format(_T(", color=\"#%06X\""), backColor.GetValue() & 0xffffff);
47 content.Append(format);
50 content.Append(_T("];\r\n"));
53 void Graphviz::BeginDrawTableNode(CString id, CString fontName, int fontSize, int /*height*/)
55 m_tableNodeNum = 0;
56 content.Append(_T("\t"));
57 content.Append(id);
59 CString format;
60 content.Append(_T("["));
61 bool hasAttr = false;
62 if (m_defaultFontName != fontName)
64 format.Format(_T("fontname=\"%s\""), fontName);
65 content.Append(format);
66 hasAttr = true;
69 if (m_defaultFontSize != fontSize)
71 if (hasAttr)
72 content.Append(_T(", "));
73 format.Format(_T("fontsize=\"%d\""), fontSize);
74 content.Append(format);
75 hasAttr = true;
78 if (hasAttr)
79 content.Append(_T(", "));
80 content.Append(_T("color=transparent"));
82 content.Append(_T(", label=<\r\n\t<table border=\"0\" cellborder=\"0\" cellpadding=\"5\">\r\n"));
85 void Graphviz::DrawTableNode(CString text, Gdiplus::Color backColor)
87 CString format;
88 format.Format(_T("\t<tr><td port=\"f%d\" bgcolor=\"#%06X\">%s</td></tr>\r\n"), m_tableNodeNum++, backColor.GetValue() & 0xffffff, text);
89 content.Append(format);
92 void Graphviz::EndDrawTableNode()
94 content.Append(_T("\t</table>\r\n\t>];\r\n"));
97 void Graphviz::DrawEdge(CString from, CString to)
99 content.Append(_T("\t"));
100 content.Append(from);
101 content.Append(_T("->"));
102 content.Append(to);
103 content.Append(_T("\r\n"));
106 bool Graphviz::Save(const CString &path)
108 DWORD dwWritten = 0;
109 CAutoFile hFile = CreateFile(path, GENERIC_WRITE, FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
110 if (!hFile)
111 return false;
113 CStringA header;
114 header.Format("digraph G {\r\n\tgraph [rankdir=BT];\r\n\tnode [style=\"filled, rounded\", shape=box, fontname=\"Courier New\", fontsize=9, height=0.26, penwidth=0];\r\n");
115 CStringA footer = "\r\n}";
117 if (!WriteFile(hFile, header, (DWORD)header.GetLength(), &dwWritten, NULL))
118 return false;
120 CStringA contentA = CUnicodeUtils::GetUTF8(content);
121 if (!WriteFile(hFile, contentA, (DWORD)contentA.GetLength(), &dwWritten, NULL))
122 return false;
123 if (!WriteFile(hFile, footer, (DWORD)footer.GetLength(), &dwWritten, NULL))
124 return false;
126 return true;