summaryrefslogtreecommitdiff
path: root/plugins/MirandaG15/src/LCDFramework/CLCDLabel.cpp
blob: 38d6ccf3263a5f845c65f26ba1e648aa37958b1a (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "stdafx.h"
#include "CLCDGfx.h"
#include "CLCDObject.h"
#include "CLCDTextObject.h"
#include "CLCDLabel.h"
#include "math.h"

//************************************************************************
// constructor
//************************************************************************
CLCDLabel::CLCDLabel() : m_bCutOff(true), m_bWordWrap(false), m_iCutOffIndex(0),
	m_iLineCount(0)
{
	m_strText = L"";
}

//************************************************************************
// destructor
//************************************************************************
CLCDLabel::~CLCDLabel()
{
}

//************************************************************************
// initializes the label
//************************************************************************
bool CLCDLabel::Initialize()
{
	if(!CLCDTextObject::Initialize())
		return false;

	return true;
}

//************************************************************************
// deinitializes the label
//************************************************************************
bool CLCDLabel::Shutdown()
{
	if(!CLCDTextObject::Shutdown())
		return false;

	return true;
}

//************************************************************************
// updates the label
//************************************************************************
bool CLCDLabel::Update()
{
	if(!CLCDTextObject::Update())
		return false;

	return true;
}

//************************************************************************
// draws the label
//************************************************************************
bool CLCDLabel::Draw(CLCDGfx *pGfx)
{
	if(!CLCDTextObject::Draw(pGfx))
		return false;

	SelectObject(pGfx->GetHDC(),m_hFont);

	int iTop = (int)(GetHeight()-(m_vLines.size()*m_iFontHeight))/2;///2;//()/2;
	RECT rBoundary = {0,iTop,GetWidth(), GetHeight()-iTop}; 

	vector<tstring>::iterator iter = m_vLines.begin();
	while(!m_vLines.empty() && iter != m_vLines.end())
	{
		DrawTextEx(pGfx->GetHDC(), (LPTSTR)(*iter).c_str(), (int)(*iter).length(), &rBoundary, m_iTextFormat, &m_dtp);
		rBoundary.top += m_iFontHeight;
		iter++;
	}
	return true;
}

//************************************************************************
// sets the label's text
//************************************************************************
void CLCDLabel::SetText(tstring strText)
{
	m_strText = strText;

	UpdateCutOffIndex();
}

//************************************************************************
// called when the labels font has changed
//************************************************************************
void CLCDLabel::OnFontChanged()
{
	UpdateCutOffIndex();
}

//************************************************************************
// called when the labels size has changed
//************************************************************************
void CLCDLabel::OnSizeChanged(SIZE OldSize)
{
	if(GetWidth() == OldSize.cx && GetHeight() == OldSize.cy)
		return;
	UpdateCutOffIndex();
}

//************************************************************************
// sets the wordwrap mode
//************************************************************************
void CLCDLabel::SetWordWrap(bool bEnable)
{
	m_bWordWrap = bEnable;
}

//************************************************************************
// updates the cutoff index
//************************************************************************
void CLCDLabel::UpdateCutOffIndex()
{
	int iLen = (int)m_strText.length();
	
	m_vLines.clear();

	if(iLen <= 0)
	{
		m_strCutOff = L"";
		m_iCutOffIndex = 0;
		return;
	}
	// variables
	
	HDC hDC = CreateCompatibleDC(nullptr);
	SelectObject(hDC, m_hFont);   
    
	if(nullptr == hDC)
		return;

	int iWidth = GetWidth();
	SIZE sizeLine =  {0, 0};
	SIZE sizeCutOff = {0, 0};

	int iAvailableLines = GetHeight()/m_iFontHeight;
	// unitialized or too small labels need to be handled
	if(iAvailableLines <= 0)
		iAvailableLines = 1;

	// process wordwrapping
	int i = 0;
	if(m_bWordWrap && GetWidth() > 0)
	{
		int *piExtents = new int[m_strText.length()];
		wchar_t *szString = (wchar_t*)m_strText.c_str();
		int iMaxChars = 0;
		tstring::size_type pos = 0;

		while(i<iLen && m_vLines.size() < iAvailableLines)
		{
			GetTextExtentExPoint(hDC, szString+i, (int)m_strText.length()-i, GetWidth(), &iMaxChars, piExtents, &sizeLine);
			
			// filter out spaces or line breaks at the beginning of a new line
			if(m_strText[i] == '\n' || m_strText[i] == ' ')
				i++;

			pos = m_strText.find(L"\n",i);
			// check for linebreaks
			if(pos != tstring::npos && pos != i && pos >= i && pos != i+iMaxChars)
				iMaxChars = 1 + (int)pos - i;
			// if the string doesnt fit, try to wrap the last word to the next line
			else if(iMaxChars < iLen - i || sizeLine.cx >= GetWidth())
			{
				// find the last space in the line ( substract -1 from offset to ignore spaces as last chars )
				pos = m_strText.rfind(L" ",i + iMaxChars -1 );
				if(pos != tstring::npos && pos != i && pos >= i && pos != i+iMaxChars)
					iMaxChars = 1 + (int)pos - i;
			}

			if(m_vLines.size() == iAvailableLines-1)
				iMaxChars = iLen - i;

			m_vLines.push_back(m_strText.substr(i,iMaxChars));
			i += iMaxChars;
		}
		delete[] piExtents;
	}
	else
		m_vLines.push_back(m_strText);

	// calculate the cutoff position

	GetTextExtentPoint(hDC,L"...",3,&sizeCutOff);

	int *piWidths = new int[(*--m_vLines.end()).length()];
	int iMaxChars = 0;

	GetTextExtentExPoint(hDC,(*--m_vLines.end()).c_str(),(int)(*--m_vLines.end()).length(),iWidth,&iMaxChars,piWidths,&sizeLine);
	
	if(iMaxChars < (*--m_vLines.end()).length())
	{
		for(iMaxChars--;iMaxChars>0;iMaxChars--)
		{	
			if(piWidths[iMaxChars] + sizeCutOff.cx <= iWidth)
				break;
		}
		(*--m_vLines.end()) = (*--m_vLines.end()).substr(0,iMaxChars) + L"...";
	}
	delete[] piWidths;

	DeleteObject(hDC);

	//if(GetWidth() == 0)
		m_iLineCount = 1;
	//else
	//	m_iLineCount = ceil((double)(sizeLine.cx + sizeCutOff.cx)/GetWidth());

	//if(m_iLineCount > GetHeight()/m_iFontHeight)
	//	m_iLineCount = GetHeight()/m_iFontHeight;
}

//************************************************************************
// sets the cutoff mode
//************************************************************************
void CLCDLabel::SetCutOff(bool bEnable)
{
	m_bCutOff = bEnable;
}