forked from mpc-hc/mpc-hc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCDPaginateText.cpp
More file actions
274 lines (223 loc) · 7.78 KB
/
LCDPaginateText.cpp
File metadata and controls
274 lines (223 loc) · 7.78 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//************************************************************************
// The Logitech LCD SDK, including all acompanying documentation,
// is protected by intellectual property laws. All use of the Logitech
// LCD SDK is subject to the License Agreement found in the
// "Logitech LCD SDK License Agreement" file and in the Reference Manual.
// All rights not expressly granted by Logitech are reserved.
//************************************************************************
//************************************************************************
//
// LCDPaginateText.h
//
// The CLCDPaginateText class draws text onto the LCD in pages.
//
// Logitech LCD SDK
//
// Copyright 2010 Logitech Inc.
//************************************************************************
#include "LCDUI.h"
//************************************************************************
//
// CLCDPaginateText::CLCDPaginateText
//
//************************************************************************
CLCDPaginateText::CLCDPaginateText(void)
: m_linePerPage(0),
m_totalPageNum(0),
m_iCurPageNum(0)
{
ZeroMemory(&m_origSize, sizeof(m_origSize));
}
//************************************************************************
//
// CLCDPaginateText::~CLCDPaginateText
//
//************************************************************************
CLCDPaginateText::~CLCDPaginateText(void)
{
}
//************************************************************************
//
// CLCDPaginateText::SetSize
//
//************************************************************************
void CLCDPaginateText::SetSize(SIZE& size)
{
CLCDBase::SetSize(size);
m_origSize = size;
m_bRecalcExtent = true;
}
//************************************************************************
//
// CLCDPaginateText::SetSize
//
//************************************************************************
void CLCDPaginateText::SetSize(int nCX, int nCY)
{
CLCDBase::SetSize(nCX, nCY);
m_origSize = m_Size;
m_bRecalcExtent = true;
}
//************************************************************************
//
// CLCDPaginateText::DoPaginate
//
// Force the re-pagination, will set the first page to be the current page
//
//************************************************************************
void CLCDPaginateText::DoPaginate(void)
{
// if m_bRecalcExtent is false, nothing affects pagination changes, don't do anything
if(!m_bRecalcExtent)
{
return;
}
// if the page size is 0, set current page, total number of page, and line of text per page as 0
if(m_origSize.cx == 0 || m_origSize.cy == 0)
{
m_iCurPageNum = 0;
m_totalPageNum = 0;
m_linePerPage = 0;
}
else
{
HDC hdc = CreateCompatibleDC(NULL);
int nOldMapMode = SetMapMode(hdc, MM_TEXT);
int nOldBkMode = SetBkMode(hdc, GetBackgroundMode());
// select current font
HFONT hOldFont = (HFONT)SelectObject(hdc, m_hFont);
RECT rExtent;
// calculate horizontal extent w/ single line, we can get the line height
int nTextFormat = (m_nTextFormat | DT_SINGLELINE | DT_CALCRECT);
rExtent.left = rExtent.top = 0;
rExtent.right = m_origSize.cx;
rExtent.bottom = m_origSize.cy;
DrawTextEx(hdc, (LPTSTR)m_sText.c_str(), static_cast<int>(m_nTextLength), &rExtent, nTextFormat, &m_dtp);
m_sizeHExtent.cx = rExtent.right;
m_sizeHExtent.cy = rExtent.bottom;
// calculate vertical extent with word wrap
nTextFormat = (m_nTextFormat | DT_WORDBREAK | DT_CALCRECT);
rExtent.left = rExtent.top = 0;
rExtent.right = m_origSize.cx;
rExtent.bottom = m_origSize.cy;
DrawTextEx(hdc, (LPTSTR)m_sText.c_str(), static_cast<int>(m_nTextLength), &rExtent, nTextFormat, &m_dtp);
m_sizeVExtent.cx = rExtent.right;
m_sizeVExtent.cy = rExtent.bottom;
CLCDText::SetLogicalSize(m_sizeVExtent.cx, m_sizeVExtent.cy);
// restores
SetMapMode(hdc, nOldMapMode);
SetBkMode(hdc, nOldBkMode);
SelectObject(hdc, hOldFont);
DeleteDC(hdc);
m_linePerPage = (int)(m_origSize.cy / m_sizeHExtent.cy);
// we re-set the m_Size.cy to show m_linePerPage line of text exactly (no clipping text)
m_Size.cy = m_linePerPage * m_sizeHExtent.cy;
// if the control size is too small to fit one line of text, the m_Size.cy is set to 0,
// and we also set total number of pages to be 0
if(m_Size.cy == 0)
{
m_iCurPageNum = 0;
m_totalPageNum = 0;
m_linePerPage = 0;
}
else
{
m_totalPageNum = (int)(m_sizeVExtent.cy / m_Size.cy);
m_totalPageNum += ((m_sizeVExtent.cy % m_Size.cy) > 0) ? 1 : 0;
m_totalPageNum = (m_totalPageNum > 0) ? m_totalPageNum : 1;
// after paginate, always set first page to be the current page
m_iCurPageNum = 0;
}
}
// set m_bRecalcExtent to be false to avoid another calculation at Draw time
m_bRecalcExtent = false;
SetLogicalOrigin(0, (int)((-1) * m_iCurPageNum * m_Size.cy));
}
//************************************************************************
//
// CLCDPaginateText::GetTotalPages
//
//************************************************************************
int CLCDPaginateText::GetTotalPages(void)
{
DoPaginate();
return m_totalPageNum;
}
//************************************************************************
//
// CLCDPaginateText::SetCurPage
//
//************************************************************************
void CLCDPaginateText::SetCurPage(int iPageNum)
{
DoPaginate();
if(iPageNum > m_totalPageNum - 1)
{
iPageNum = m_totalPageNum - 1;
}
if(iPageNum < 0)
{
iPageNum = 0;
}
m_iCurPageNum = iPageNum;
SetLogicalOrigin(0, (int)((-1) * m_iCurPageNum * m_Size.cy));
}
//************************************************************************
//
// CLCDPaginateText::GetCurPage
//
//************************************************************************
int CLCDPaginateText::GetCurPage(void)
{
DoPaginate();
return m_iCurPageNum;
}
//************************************************************************
//
// CLCDPaginateText::GotoPrevPage
//
// Go to previous page, return false if it is already at the first page.
// Otherwise, return true;
//
//************************************************************************
bool CLCDPaginateText::GotoPrevPage(void)
{
DoPaginate();
if(m_iCurPageNum > 0)
{
SetCurPage(--m_iCurPageNum);
return true;
}
return false;
}
//************************************************************************
//
// CLCDPaginateText::GotoNextPage
//
// Go to next page, return false if it is already at the last page.
// Otherwise, return true;
//
//************************************************************************
bool CLCDPaginateText::GotoNextPage(void)
{
DoPaginate();
if(m_iCurPageNum < m_totalPageNum -1)
{
SetCurPage(++m_iCurPageNum);
return true;
}
return false;
}
//************************************************************************
//
// CLCDPaginateText::OnDraw
//
// Before drawing, make sure we have a updated pagination
//
//************************************************************************
void CLCDPaginateText::OnDraw(CLCDGfxBase &rGfx)
{
DoPaginate();
CLCDText::OnDraw(rGfx);
}
//** end of LCDPaginateText.cpp ******************************************