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 0
Expand file tree
/
Copy pathGDI_ProgressBar.cpp
More file actions
313 lines (272 loc) · 12.7 KB
/
GDI_ProgressBar.cpp
File metadata and controls
313 lines (272 loc) · 12.7 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
/*******************************************************************************
*******************************************************************************
* 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_ProgressBar.h"
#include "GDI_ProgressBar_Util.h"
//--------------------------------------
//--------------------------------------
//
#include "Windows/GDI/GDI_Util.h"
#include "Windows/GDI/GDI_Font.h"
//--------------------------------------
namespace Windows::GDI {
//**************************************************************************
// CProgressBar
//**************************************************************************
CProgressBar::CProgressBar(_In_ HDC hDC)
: baseClass{ hDC } {
CLogFont logFont;
GetSystemFont(SYS_FONT_MESSAGE, logFont);
SetFont(logFont);
}
//--------------------------------------------------------------------------
void CProgressBar::Update(_In_ float fProgress) {
if (m_Progress.Current(fProgress)) {
UpdateProgress();
}
}
//--------------------------------------------------------------------------
void CProgressBar::Range(_In_ float fMin,
_In_ float fMax) {
if (m_Progress.Range(fMin, fMax)) {
UpdateBounds();
}
}
//--------------------------------------------------------------------------
void CProgressBar::SetTextMode(_In_ ProgressText eLeft,
_In_ ProgressText eRight) {
if ((m_Text[TextLeft].m_eMode != eLeft) ||
(m_Text[TextRight].m_eMode != eRight)) {
m_Text[TextLeft].m_eMode = eLeft;
m_Text[TextRight].m_eMode = eRight;
UpdateBounds();
}
}
//--------------------------------------------------------------------------
gsl_suppress(26447) // C26447: The function is declared 'noexcept' but calls function '...' which may throw exceptions (f.6).
bool CProgressBar::SetFont(_In_ const LOGFONT& logFont) {
CFont newFont = ::CreateFontIndirect(&logFont);
ATLASSERT(newFont);
if (!newFont) { return false; }
{
ScopedSelectFont _font{ Context(), newFont };
ZeroMemory(&m_Font.Metrics, sizeof(m_Font.Metrics));
if (Context().GetTextMetrics(&m_Font.Metrics) == FALSE) { return false; }
m_Font.Metrics.tmDateStringWidth = MeasureDateStringWidth(Context());
m_Font.Metrics.tmTimeStringWidth = MeasureTimeStringWidth(Context());
m_Font.Metrics.tmPercentStringWidth = MeasurePercentStringWidth(Context());
UpdateBounds();
}
m_Font.font = newFont.Detach();
ATLASSERT(m_Font.font);
return !m_Font.font.IsNull();
}
//--------------------------------------------------------------------------
bool CProgressBar::SetFont(_In_z_ LPCTSTR szFamily,
_In_ int iHeight) {
CLogFont logFont{};
if (GetFontClose(szFamily, -iHeight, &logFont) == FONT_MATCH_NONE) {
return false;
}
return SetFont(logFont);
}
//--------------------------------------------------------------------------
gsl_suppress(26447) // C26447: The function is declared 'noexcept' but calls function '...' which may throw exceptions (f.6).
void CProgressBar::OnDraw(float /*fInterp*/) noexcept {
if (m_bClear) {
ScopedSelectPen _pen{ Context(), Pen(Layer::Background) };
ScopedSelectBrush _brush{ Context(), Brush(Layer::Background) };
ScopedSetDCPenColor _penColor{ Context(), Color(Layer::Background) };
const auto size = Size();
WinAPIVerify(Context().Rectangle(0, 0, size.cx, size.cy));
}
ScopedSelectPen _pen{ Context(), Pen(Layer::Foreground) };
ScopedSetDCPenColor _penColor{ Context(), Color(Layer::Foreground) };
// Draw Progress
{
// Outline
WinAPIVerify(Context().FrameRect(m_OutlineRect, Brush(Layer::Foreground)));
// Fill
WinAPIVerify(Context().FillRect(m_FillRect, Brush(Layer::Foreground)));
}
// Draw Text
{
ScopedSetMapMode _mapMode{ Context(), MM_TEXT };
ScopedSelectFont _font{ Context(), m_Font.font };
if (m_Text[TextLeft].m_eMode != ProgressText::None) {
m_Text[TextLeft].Draw(Context(), DT_LEFT);
}
if (m_Text[TextRight].m_eMode != ProgressText::None) {
m_Text[TextRight].Draw(Context(), DT_RIGHT);
}
}
}
//--------------------------------------------------------------------------
void CProgressBar::OnChanged(_In_ GDIChange eWhat,
_In_ LPARAM lparam,
_In_ WPARAM /*wparam*/) {
switch (eWhat) {
case GDIChange::Size: { //Size has been changed so change font to match
CLogFont logFont;
const auto cy = (LONG)lparam;
if (m_Font.font) {
WinAPIVerify(m_Font.font.GetLogFont(logFont));
logFont.lfHeight = -cy;
} else {
GetSystemFont(SYS_FONT_MESSAGE, -cy, &logFont);
}
SetFont(logFont);
break;
}
}
}
//--------------------------------------------------------------------------
int CProgressBar::CalcTextSize(ProgressText eMode) {
switch (eMode) {
case ProgressText::None:
return 0;
case ProgressText::ElapsedPercent:
[[fallthrough]];
case ProgressText::RemainingPercent:
return m_Font.Metrics.tmPercentStringWidth;
case ProgressText::SystemDate:
return m_Font.Metrics.tmDateStringWidth;
case ProgressText::SystemTime:
return m_Font.Metrics.tmTimeStringWidth;
default: {
LONG cchTimeMax = 0;
LONG dxWidth = 0;
CalcTimeStringMetrics(Context(),
m_Font.font,
m_Progress.Total(),
cchTimeMax,
dxWidth);
if (cchTimeMax > 6) {
m_TimeFormat.Format(TEXT("%%0%dd:%%02d:%%02d"),
cchTimeMax - 6);
} else if (m_Progress.Total() > 3600.f) {
m_TimeFormat = TEXT("%d:%02d:%02d");
} else if (cchTimeMax > 4) {
m_TimeFormat = TEXT("%02d:%02d");
} else {
m_TimeFormat = TEXT("%d:%02d");
}
return dxWidth;
}
}
}
//--------------------------------------------------------------------------
void CProgressBar::UpdateBounds() {
const auto size = Size();
const auto fHeight = static_cast<float>(size.cy);
const auto iMarginX = m_Font.Metrics.tmAveCharWidth / 2;
const auto fMarginY = fHeight * 0.25f;
m_OutlineRect.left = iMarginX;
m_OutlineRect.top = static_cast<LONG>(std::ceil(fMarginY));
m_OutlineRect.right = size.cx - iMarginX;
m_OutlineRect.bottom = static_cast<LONG>(std::floor(fHeight - fMarginY));
{
auto& text = m_Text[TextLeft];
text.m_Bounds.top = 0;
text.m_Bounds.bottom = size.cy;
text.m_Bounds.left = iMarginX;
const auto dxTextSize = CalcTextSize(text.m_eMode);
text.m_Bounds.right = text.m_Bounds.left + dxTextSize;
}
{
auto& text = m_Text[TextRight];
text.m_Bounds.top = 0;
text.m_Bounds.bottom = size.cy;
text.m_Bounds.right = size.cx - iMarginX;
const auto dxTextSize = CalcTextSize(text.m_eMode);
text.m_Bounds.left = text.m_Bounds.right - dxTextSize;
}
const auto outlineLeft = m_Text[TextLeft].m_Bounds.right + iMarginX;
const auto outlineRight = m_Text[TextRight].m_Bounds.left - iMarginX;
if (outlineLeft < outlineRight) {
m_OutlineRect.left = outlineLeft;
m_OutlineRect.right = outlineRight;
}
m_FillRect = m_OutlineRect;
UpdateProgress();
}
//--------------------------------------------------------------------------
void CProgressBar::UpdateProgress() {
if (m_Progress.Start() == m_Progress.Finish()) { return; }
const auto x0 = m_OutlineRect.left;
const auto x1 = m_OutlineRect.right;
const auto dx = std::round(static_cast<float>(x1 - x0) * m_Progress.ElapsedFraction());
m_FillRect.right = x0 + static_cast<LONG>(dx);
assert(m_FillRect.right >= m_OutlineRect.left);
assert(m_FillRect.right <= m_OutlineRect.right);
for (auto i = 0; i < TextCount; ++i) {
switch (m_Text[i].m_eMode) {
case ProgressText::ElapsedPercent: {
const auto fPercent = m_Progress.ElapsedPercent();
const auto nPercent = static_cast<int>(std::round(fPercent));
assert(nPercent >= 0 && nPercent <= 100);
m_Text[i].m_strText.Format(TEXT("%3d"), nPercent);
break;
}
case ProgressText::RemainingPercent: {
const auto fPercent = m_Progress.RemainPercent();
const auto nPercent = static_cast<int>(std::round(fPercent));
assert(nPercent >= 0 && nPercent <= 100);
m_Text[i].m_strText.Format(TEXT("%3d"), nPercent);
break;
}
case ProgressText::ElapsedTime:
FormatStringFromSeconds(m_Text[i].m_strText,
m_TimeFormat,
m_Progress.Elapsed(),
m_Progress.Total());
break;
case ProgressText::RemainingTime:
FormatStringFromSeconds(m_Text[i].m_strText,
m_TimeFormat,
m_Progress.Remain(),
m_Progress.Total());
break;
case ProgressText::TotalTime:
FormatStringFromSeconds(m_Text[i].m_strText,
m_TimeFormat,
m_Progress.Total(),
m_Progress.Total());
break;
case ProgressText::SystemTime: {
static constexpr const auto s_cchTimeBuf = 32;
StringFromTimeNow(m_Text[i].m_strText.GetBuffer(s_cchTimeBuf),
s_cchTimeBuf - 1,
TEXT("%H:%M"));
break;
}
case ProgressText::SystemDate: {
static constexpr const auto s_cchDateBuf = 32;
StringFromDateNow(m_Text[i].m_strText.GetBuffer(s_cchDateBuf),
s_cchDateBuf - 1);
break;
}
}
}
}
} // namespace Windows::GDI