/
ChartTitle.cpp
154 lines (126 loc) · 3.55 KB
/
ChartTitle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
*
* ChartTitle.cpp
*
* Written by Cédric Moonen (cedric_moonen@hotmail.com)
*
*
*
* This code may be used for any non-commercial and commercial purposes in a compiled form.
* The code may be redistributed as long as it remains unmodified and providing that the
* author name and this disclaimer remain intact. The sources can be modified WITH the author
* consent only.
*
* This code is provided without any garanties. I cannot be held responsible for the damage or
* the loss of time it causes. Use it at your own risks
*
* An e-mail to notify me that you are using this code is appreciated also.
*
*
*/
#include "stdafx.h"
#include "ChartTitle.h"
#include "Math.h"
using namespace std;
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChartTitle::CChartTitle(CChartCtrl* pParent) : CChartObject(pParent)
{
CChartObject::SetColor(RGB(0,0,0));
m_iFontSize = 100;
m_strFontName = "Microsoft Sans Serif";
}
CChartTitle::~CChartTitle()
{
}
void CChartTitle::AddString(std::string const& NewString)
{
m_StringArray.push_back(NewString);
}
size_t CChartTitle::GetStringCount() const
{
return m_StringArray.size();
}
string CChartTitle::GetString(size_t Index) const
{
if ( (Index<0) || (Index>=m_StringArray.size()) )
return "";
return m_StringArray[Index];
}
void CChartTitle::RemoveAll()
{
m_StringArray.clear();
}
void CChartTitle::Draw(CDC *pDC)
{
if (!pDC->GetSafeHdc())
return;
if (!m_bIsVisible)
return;
CPen SolidPen(PS_SOLID,0,m_ObjectColor);
CPen* pOldPen;
CFont* pOldFont;
CFont NewFont;
NewFont.CreatePointFont(m_iFontSize, CA2CT(m_strFontName.c_str()), pDC);
COLORREF OldColor = pDC->SetTextColor(m_ObjectColor);
pOldFont = pDC->SelectObject(&NewFont);
pOldPen = pDC->SelectObject(&SolidPen);
//Draw all entries
int YPos = 4;
size_t TitleCount = m_StringArray.size();
for (size_t i=0;i<TitleCount;i++)
{
//Draw Text
int TextWidth = pDC->GetTextExtent(m_StringArray[i].c_str()).cx;
int TextHeigh = pDC->GetTextExtent(m_StringArray[i].c_str()).cy;
int XPos = m_ObjectRect.left + (int)fabs((m_ObjectRect.left-m_ObjectRect.right)/2.0) - TextWidth/2;
pDC->ExtTextOut(XPos,m_ObjectRect.top+YPos,ETO_CLIPPED,NULL,m_StringArray[i].c_str(),NULL);
YPos += TextHeigh + 2;
}
pDC->SelectObject(pOldFont);
DeleteObject(NewFont);
pDC->SelectObject(pOldPen);
DeleteObject(SolidPen);
pDC->SetTextColor(OldColor);
}
CSize CChartTitle::GetSize(CDC *pDC)
{
CSize TitleSize;
if (!m_bIsVisible)
{
TitleSize.cx = TitleSize.cy = 0;
return TitleSize;
}
int Height = 4; //Upper space
CSize TextSize = 0;
int MaxTextWidth = 0;
size_t TitleCount = m_StringArray.size();
if (TitleCount==0)
{
TitleSize.cx = TitleSize.cy = 0;
return TitleSize;
}
CFont* pOldFont;
CFont NewFont;
NewFont.CreatePointFont(m_iFontSize, CA2CT(m_strFontName.c_str()), pDC);
pOldFont = pDC->SelectObject(&NewFont);
for (size_t i=0;i<TitleCount;i++)
{
TextSize = pDC->GetTextExtent(m_StringArray[i].c_str());
Height += TextSize.cy + 2;
if (TextSize.cx > MaxTextWidth)
MaxTextWidth = TextSize.cx;
}
TitleSize.cx = MaxTextWidth + 2;
TitleSize.cy = Height;
m_ObjectRect.bottom = m_ObjectRect.top + Height;
pDC->SelectObject(pOldFont);
DeleteObject(NewFont);
return TitleSize;
}