Fixed some warnings
[TortoiseGit.git] / src / Utils / Graphviz.cpp
blob4fa12a68a6e082e5324da3911ff2d13ad50d4ad7
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013 - 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 if (hasAttr)
65 content.Append(_T(", "));
66 format.Format(_T("fontname=\"%s\""), fontName);
67 content.Append(format);
68 hasAttr = true;
71 if (m_defaultFontSize != fontSize)
73 if (hasAttr)
74 content.Append(_T(", "));
75 format.Format(_T("fontsize=\"%d\""), fontSize);
76 content.Append(format);
77 hasAttr = true;
80 if (hasAttr)
81 content.Append(_T(", "));
82 content.Append(_T("color=transparent"));
84 content.Append(_T(", label=<\r\n\t<table border=\"0\" cellborder=\"0\" cellpadding=\"5\">\r\n"));
87 void Graphviz::DrawTableNode(CString text, Gdiplus::Color backColor)
89 CString format;
90 format.Format(_T("\t<tr><td port=\"f%d\" bgcolor=\"#%06X\">%s</td></tr>\r\n"), m_tableNodeNum++, backColor.GetValue() & 0xffffff, text);
91 content.Append(format);
94 void Graphviz::EndDrawTableNode()
96 content.Append(_T("\t</table>\r\n\t>];\r\n"));
99 void Graphviz::DrawEdge(CString from, CString to)
101 content.Append(_T("\t"));
102 content.Append(from);
103 content.Append(_T("->"));
104 content.Append(to);
105 content.Append(_T("\r\n"));
108 bool Graphviz::Save(const CString &path)
110 DWORD dwWritten = 0;
111 CAutoFile hFile = CreateFile(path, GENERIC_WRITE, FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
112 if (!hFile)
113 return false;
115 CStringA header;
116 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");
117 CStringA footer = "\r\n}";
119 if (!WriteFile(hFile, header, (DWORD)header.GetLength(), &dwWritten, NULL))
120 return false;
122 CStringA contentA = CUnicodeUtils::GetUTF8(content);
123 if (!WriteFile(hFile, contentA, (DWORD)contentA.GetLength(), &dwWritten, NULL))
124 return false;
125 if (!WriteFile(hFile, footer, (DWORD)footer.GetLength(), &dwWritten, NULL))
126 return false;
128 return true;