Adjust the background icon size for high dpi monitors
[TortoiseGit.git] / src / Utils / Graphviz.cpp
blob36af87b520ef066c5e310b354ec0e22e317403bd
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2014, 2016 - 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.AppendChar(L'\t');
27 content.Append(id);
29 content.AppendFormat(L" [label=\"%s\"", (LPCTSTR)text);
30 if (m_defaultFontName != fontName)
31 content.AppendFormat(L", fontname=\"%s\"", (LPCTSTR)fontName);
33 if (m_defaultFontSize != fontSize)
34 content.AppendFormat(L", fontsize=\"%d\"", fontSize);
36 if (m_defaultBackColor.GetValue() != backColor.GetValue())
37 content.AppendFormat(L", color=\"#%06X\"", backColor.GetValue() & 0xffffff);
39 content.Append(L"];\r\n");
42 void Graphviz::BeginDrawTableNode(CString id, CString fontName, int fontSize, int /*height*/)
44 m_tableNodeNum = 0;
45 content.AppendChar(L'\t');
46 content.Append(id);
48 content.AppendChar(L'[');
49 bool hasAttr = false;
50 if (m_defaultFontName != fontName)
52 content.AppendFormat(L"fontname=\"%s\"", (LPCTSTR)fontName);
53 hasAttr = true;
56 if (m_defaultFontSize != fontSize)
58 if (hasAttr)
59 content.Append(L", ");
60 content.AppendFormat(L"fontsize=\"%d\"", fontSize);
61 hasAttr = true;
64 if (hasAttr)
65 content.Append(L", ");
66 content.Append(L"color=transparent");
68 content.Append(L", label=<\r\n\t<table border=\"0\" cellborder=\"0\" cellpadding=\"5\">\r\n");
71 void Graphviz::DrawTableNode(CString text, Gdiplus::Color backColor)
73 content.AppendFormat(L"\t<tr><td port=\"f%d\" bgcolor=\"#%06X\">%s</td></tr>\r\n", m_tableNodeNum++, backColor.GetValue() & 0xffffff, (LPCTSTR)text);
76 void Graphviz::EndDrawTableNode()
78 content.Append(L"\t</table>\r\n\t>];\r\n");
81 void Graphviz::DrawEdge(CString from, CString to)
83 content.AppendChar(L'\t');
84 content.Append(from);
85 content.Append(L"->");
86 content.Append(to);
87 content.Append(L"\r\n");
90 bool Graphviz::Save(const CString &path)
92 DWORD dwWritten = 0;
93 CAutoFile hFile = CreateFile(path, GENERIC_WRITE, FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
94 if (!hFile)
95 return false;
97 CStringA header;
98 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");
99 CStringA footer = "\r\n}";
101 if (!WriteFile(hFile, header, (DWORD)header.GetLength(), &dwWritten, nullptr))
102 return false;
104 CStringA contentA = CUnicodeUtils::GetUTF8(content);
105 if (!WriteFile(hFile, contentA, (DWORD)contentA.GetLength(), &dwWritten, nullptr))
106 return false;
107 if (!WriteFile(hFile, footer, (DWORD)footer.GetLength(), &dwWritten, nullptr))
108 return false;
110 return true;