This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGDI_Text.cpp
More file actions
376 lines (314 loc) · 14.3 KB
/
GDI_Text.cpp
File metadata and controls
376 lines (314 loc) · 14.3 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*******************************************************************************
*******************************************************************************
* Copyright (c) 2009-2023 ectotropic (ectotropic@gmail.com, *
* https://github.com/ectotropic) *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************
******************************************************************************/
//--------------------------------------
//
#include "CommonHeaders.h"
#include "GDI_Text.h"
//--------------------------------------
//--------------------------------------
//
#include <cwctype>
//--------------------------------------
namespace Windows::GDI {
//**************************************************************************
// CText
//**************************************************************************
CText::CText(_In_ HDC hDC) : baseClass(hDC) {
CLogFont logFont;
WinAPIVerify(GetSystemFont(SYS_FONT_MESSAGE, &logFont));
WinAPIVerify(m_Font.CreateFontIndirect(&logFont));
}
//--------------------------------------------------------------------------
void CText::SetFont(_In_ const LOGFONT& logFont, _In_ bool bAllowOverlap) {
if (m_Font) { m_Font.DeleteObject(); }
WinAPIVerify(m_Font.CreateFontIndirect(&logFont));
m_bAllowOverlap = bAllowOverlap;
int iWidth = 0, iHeight = 0;
for (auto& line: m_Lines) {
line.SetFont(m_Font);
if (line.Size().cx > iWidth) { iWidth = line.Size().cx; }
iHeight += GetLineHeight(line);
}
Update(iWidth, iHeight);
}
//--------------------------------------------------------------------------
void CText::SetText(_In_reads_z_(nLength) LPCTSTR szText, _In_ int nLength,
_In_ int nMaxLines) {
CString text{ szText, nLength };
std::vector<CString> TextLines;
int index{ 0 };
while (text.GetLength() > 0) {
index = text.FindOneOf(TEXT("\r\n"));
if (index != -1) {
TextLines.push_back(std::move(text.Left(index)));
if (text[index] == TEXT('\r')) {
const auto new_index{ index + 1 };
if ((text.GetLength() > new_index) &&
text[new_index] == TEXT('\n')) {
index = new_index;
}
}
text = text.Mid(index + 1);
} else {
TextLines.push_back(std::move(text));
break;
}
}
auto lineCount = TextLines.size();
if (nMaxLines > 0) {
lineCount =
std::min(static_cast<std::size_t>(nMaxLines), lineCount);
}
SetLineCount(lineCount);
lineCount = std::min(TextLines.size(), m_Lines.size());
int iWidth = 0;
int iHeight = 0;
for (decltype(lineCount) i = 0; i < lineCount; ++i) {
auto& strLine = TextLines[i];
auto& line = m_Lines[i];
line.SetText(strLine.Trim());
if (line.Size().cx > iWidth) { iWidth = line.Size().cx; }
iHeight += GetLineHeight(line);
}
Update(iWidth, iHeight);
}
//--------------------------------------------------------------------------
void CText::SetHAlign(_In_ Text::Align::Horizontal eAlign) noexcept {
if (m_eHAlign == eAlign) { return; }
m_eHAlign = eAlign;
for (auto& line: m_Lines) { line.SetHAlign(m_eHAlign); }
}
//--------------------------------------------------------------------------
void CText::SetVScroll(_In_ const ScrollParams& params) noexcept {
m_Scroller.SetMode(params.eMode);
m_Scroller.SetSpeed(params.fSpeed);
m_Scroller.SetDelay(params.fDelay);
if ((params.eMode == ScrollMode::Loop) && (params.fGapFactor > 0.f)) {
m_fLoopGapFactor = params.fGapFactor;
const auto fHeight = static_cast<float>(Size().cy);
const auto fLineCount = static_cast<float>(m_Lines.size());
m_fLoopGap = std::round(m_fLoopGapFactor * (fHeight / fLineCount));
} else {
m_fLoopGapFactor = m_fLoopGap = 0.f;
}
}
//--------------------------------------------------------------------------
void CText::SetHScroll(_In_ const ScrollParams& params) noexcept {
m_ScrollH = params;
for (auto& line: m_Lines) {
line.SetHScroll(m_ScrollH.eMode, m_ScrollH.fSpeed, m_ScrollH.fDelay,
m_ScrollH.fGapFactor);
}
}
//--------------------------------------------------------------------------
void CText::SetBGClear(bool bClear) noexcept {
if (m_bBGClear == bClear) { return; }
m_bBGClear = bClear;
for (auto& line: m_Lines) { line.SetClear(m_bBGClear); }
}
//--------------------------------------------------------------------------
void CText::Reset() noexcept {
m_Lines.clear();
m_Scroller.Reset();
m_fTarget = 0.f;
m_iScrollLineID = 0;
m_eScrollLastStatus = ScrollStatus::None;
}
//--------------------------------------------------------------------------
void CText::Update() {
const auto iClipHeight = Clip().bottom - Clip().top;
m_Scroller.Update(static_cast<float>(Size().cy) + m_fLoopGap,
static_cast<float>(iClipHeight));
const float fClipTop = static_cast<float>(Clip().top);
const float fClipBottom = static_cast<float>(Clip().bottom);
int iScrollingLineCount = 0;
for (auto& line: m_Lines) {
const auto iLineHeight = GetLineHeight(line);
CPoint lineLimits[2] = {
{ 0, 0 },
{ line.Size().cx, iLineHeight },
};
WinAPIVerify(Context().LPtoDP(lineLimits, 2));
const auto fLineHeight = static_cast<float>(iLineHeight);
const auto fLineStart = static_cast<float>(lineLimits[0].y);
const auto fLineEnd = static_cast<float>(lineLimits[1].y);
// BOOL RectVisible( [in] HDC hdc, [in] const RECT* lprect );
bool bVisible = true;
bool bShouldScroll = false;
if (fLineStart > 0) {
bVisible = fLineStart < fClipBottom;
bShouldScroll =
(!m_bScrollStaggered) ||
((m_iScrollLineID >= 0) &&
((fClipBottom - fLineStart) > (0.75f * fLineHeight)));
} else {
bVisible = fLineEnd > fClipTop;
bShouldScroll =
(!m_bScrollStaggered) ||
((m_iScrollLineID >= 0) &&
((fLineEnd - fClipTop) > (0.75f * fLineHeight)));
}
const auto eScrollStatus = line.Update(bVisible && bShouldScroll);
if (bVisible) {
if (eScrollStatus == ScrollStatus::Finished) {
if (m_Scroller.IsScrolling() && m_bScrollStaggered) {
line.PauseScroll();
}
} else if ((eScrollStatus != ScrollStatus::None) &&
(eScrollStatus != ScrollStatus::Paused)) {
++iScrollingLineCount;
}
}
}
const auto iHeight = Size().cy;
const bool bScrolling =
m_Scroller.IsScrolling() && (iHeight > iClipHeight);
if (bScrolling && m_bScrollStaggered) {
if (iScrollingLineCount == 0) {
m_Scroller.Resume(true);
} else {
m_Scroller.Pause();
}
}
}
//--------------------------------------------------------------------------
void CText::OnDraw(float fInterp) {
if (m_Lines.empty()) { return; }
ScopedSetBkColor _bkColor{ Context(), Color(Layer::Background) };
ScopedSelectFont _font{ Context(), m_Font };
ScopedSetMapMode _mapMode{ Context(), MM_TEXT };
ScopedSetTextColor _textColor{ Context(), Color(Layer::Foreground) };
const auto iClipHeight = Clip().bottom - Clip().top;
const auto iHeight = Size().cy;
const auto fHeight = static_cast<float>(iHeight);
float fOffsetV = 0.f;
const bool bScrolling =
m_Scroller.IsScrolling() && (iHeight > iClipHeight);
if (bScrolling) {
fOffsetV = m_Scroller.GetOffset(fInterp);
const auto fMaxOffset = fHeight + m_fLoopGap;
if (std::fabs(fOffsetV) > fMaxOffset) {
fOffsetV = std::copysign(fMaxOffset, fOffsetV);
}
} else {
const auto fClipHeight = static_cast<float>(iClipHeight);
switch (m_eVAlign) {
case Text::Align::Horizontal::Left: fOffsetV = 0.0f; break;
case Text::Align::Horizontal::Centre:
fOffsetV = std::max(0.0f, (fClipHeight - fHeight) * 0.5f);
break;
case Text::Align::Horizontal::Right:
fOffsetV = std::max(0.0f, fClipHeight - fHeight);
break;
DEFAULT_UNREACHABLE;
}
}
const int iOffsetV = static_cast<int>(std::floor(fOffsetV));
DrawLines(0, iOffsetV);
if (bScrolling && m_Scroller.IsLooping()) {
DrawLines(0, static_cast<int>(
std::round(static_cast<float>(iOffsetV + iHeight)) +
m_fLoopGap));
}
}
//--------------------------------------------------------------------------
void CText::OnChanged(_In_ GDIChange eWhat, _In_ LPARAM lparam,
_In_ WPARAM wparam) noexcept {
for (auto& line: m_Lines) { line.OnChanged(eWhat, lparam, wparam); }
}
//--------------------------------------------------------------------------
void CText::ResumeLineScroll(bool bDelay) noexcept {
for (auto& line: m_Lines) { line.ResumeScroll(bDelay); }
}
//--------------------------------------------------------------------------
void CText::DrawLines(int iOffsetX, int iOffsetY) {
const float fClipTop = static_cast<float>(Clip().top);
const float fClipBottom = static_cast<float>(Clip().bottom);
POINT ViewportOffset = { iOffsetX, iOffsetY };
Context().SetTextAlign(TA_LEFT | TA_TOP | TA_NOUPDATECP);
for (auto& line: m_Lines) {
PushViewportOriginOffset(ViewportOffset);
{
const auto iLineHeight = GetLineHeight(line);
CPoint lineLimits[2] = {
{ 0, 0 },
{ line.Size().cx, iLineHeight },
};
WinAPIVerify(Context().LPtoDP(lineLimits, 2));
// const auto fLineHeight = static_cast<float>(iLineHeight);
const auto fLineStart = static_cast<float>(lineLimits[0].y);
const auto fLineEnd = static_cast<float>(lineLimits[1].y);
bool bVisible = true;
if (fLineStart > 0) {
bVisible = fLineStart < fClipBottom;
} else {
bVisible = fLineEnd > fClipTop;
}
if (bVisible) {
if (m_bBGClear) {
line.Clear(Size().cx, line.Size().cy, 0.f);
}
line.Draw(Size().cx, line.Size().cy, 0.f);
}
ViewportOffset.y += iLineHeight;
}
PopViewportOrigin();
}
}
//--------------------------------------------------------------------------
void CText::SetLineCount(lineList::size_type count) {
const auto clip = Clip();
const auto clipSize =
CSize{ clip.right - clip.left, clip.bottom - clip.top };
// Grow if required
while (static_cast<int>(m_Lines.size()) < count) {
m_Lines.emplace_back(Context(), m_Font);
auto& line = m_Lines.back();
line.SetHAlign(m_eHAlign);
line.SetHScroll(m_ScrollH.eMode, m_ScrollH.fSpeed, m_ScrollH.fDelay,
m_ScrollH.fGapFactor);
line.ClipSize(clipSize);
line.SetClear(m_bBGClear);
}
// Shrink if required
while (static_cast<int>(m_Lines.size()) > count) { m_Lines.pop_back(); }
}
//--------------------------------------------------------------------------
void CText::Update(int iWidth, int iHeight) {
SetSize(iWidth, iHeight);
m_eScrollLastStatus = ScrollStatus::None;
if (m_bScrollStaggered) {
m_iScrollLineID = 1;
m_fTarget = static_cast<float>(GetLineHeight(0));
} else {
m_iScrollLineID = 1;
m_fTarget = 0.f;
}
if (m_fLoopGapFactor > 0.f) {
const auto fHeight = static_cast<float>(iHeight);
const auto fLineCount = static_cast<float>(m_Lines.size());
m_fLoopGap = std::round(m_fLoopGapFactor * (fHeight / fLineCount));
} else {
m_fLoopGap = 0.f;
}
m_Scroller.Reset();
}
} // namespace Windows::GDI