forked from vdr-projects/vdr-plugin-skinflatplus
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomplexcontent.c
More file actions
280 lines (231 loc) · 10.1 KB
/
Copy pathcomplexcontent.c
File metadata and controls
280 lines (231 loc) · 10.1 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
/*
* Skin flatPlus: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include "./complexcontent.h"
#include <vdr/tools.h>
#include <algorithm>
#include <numeric>
cComplexContent::cComplexContent() {
Contents.reserve(128); // Set to at least 128 entry's
}
cComplexContent::cComplexContent(cOsd *osd, int ScrollSize) {
m_Osd = osd;
m_ScrollSize = ScrollSize;
Contents.reserve(128);
}
cComplexContent::~cComplexContent() {
}
void cComplexContent::Clear() {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cComplexContent::Clear() Contents size: %ld", Contents.size());
#endif
m_IsShown = false;
Contents.clear();
if (m_Osd) { //! Check because Clear() is called before SetOsd()
m_Osd->DestroyPixmap(Pixmap);
Pixmap = nullptr;
m_Osd->DestroyPixmap(PixmapImage);
PixmapImage = nullptr;
}
}
void cComplexContent::CreatePixmaps(bool FullFillBackground) {
CalculateDrawPortHeight();
m_FullFillBackground = FullFillBackground;
// if (!m_Osd) return;
m_Osd->DestroyPixmap(Pixmap);
Pixmap = nullptr;
m_Osd->DestroyPixmap(PixmapImage);
PixmapImage = nullptr;
cRect PositionDraw {0, 0, m_Position.Width(), m_DrawPortHeight};
if (FullFillBackground && m_DrawPortHeight < m_Position.Height())
PositionDraw.SetHeight(m_Position.Height());
Pixmap = CreatePixmap(m_Osd, "Pixmap", 1, m_Position, PositionDraw);
PixmapImage = CreatePixmap(m_Osd, "PixmapImage", 2, m_Position, PositionDraw);
if (Pixmap) {
if (FullFillBackground) {
PixmapFill(Pixmap, m_ColorBg);
} else {
const int HeightContent {ContentHeight(false)};
(HeightContent > 0) // Only draw background if content height is > 0
? Pixmap->DrawRectangle(cRect(0, 0, m_Position.Width(), HeightContent), m_ColorBg)
: PixmapClear(Pixmap);
}
} else { // Log values and return
esyslog(
"flatPlus: cComplexContent::CreatePixmaps() Failed to create pixmap left: %d top: %d size: %dx%d",
m_Position.Left(), m_Position.Top(), m_Position.Width(), m_Position.Height());
return;
}
PixmapClear(PixmapImage);
}
void cComplexContent::CalculateDrawPortHeight() {
m_DrawPortHeight = BottomContent();
// m_DrawPortHeight has to be set for 'ScrollTotal()' to work
if (m_IsScrollingActive && m_ScrollSize > 0) {
m_DrawPortHeight = ScrollTotal() * m_ScrollSize;
}
}
int cComplexContent::BottomContent() const {
// Using std::accumulate algorithm instead of manual loop
return std::accumulate(Contents.begin(), Contents.end(), 0,
[](int max, const auto &content) {
return std::max(max, content.GetBottom());
});
}
int cComplexContent::ContentHeight(bool Full) {
if (Full) return Height();
CalculateDrawPortHeight();
return std::min(m_DrawPortHeight, Height());
}
bool cComplexContent::Scrollable(int height) {
CalculateDrawPortHeight();
if (height == 0) height = m_Position.Height();
if (m_ScrollSize == 0) { // Avoid DIV/0
esyslog("flatPlus: Error in cComplexContent::Scrollable() m_ScrollSize is 0!");
return false;
}
// const int shown = ceil(height * 1.0 / m_ScrollSize); // Narrowing conversion
const int shown {(height + m_ScrollSize - 1) / m_ScrollSize}; // Avoid floating-point and use integer division
return ScrollTotal() > shown;
}
void cComplexContent::AddText(const char *Text, bool Multiline, const cRect &Position, tColor ColorFg, tColor ColorBg,
cFont *Font, int TextWidth, int TextHeight, int TextAlignment) {
// Use emplace_back to construct a cSimpleContent in-place and then set its properties.
Contents.emplace_back();
Contents.back().SetText(Text, Multiline, Position, ColorFg, ColorBg, Font, TextWidth, TextHeight, TextAlignment);
}
void cComplexContent::AddImage(cImage *image, const cRect &Position) {
Contents.emplace_back();
Contents.back().SetImage(image, Position);
}
void cComplexContent::AddImageWithFloatedText(cImage *image, int imageAlignment, const char *Text, int Margin,
const cRect &TextPos, tColor ColorFg, tColor ColorBg, cFont *Font,
int TextWidth, int TextHeight, int TextAlignment) {
const int TextWidthFull {(TextWidth > 0) ? TextWidth : m_Position.Width() - TextPos.Left()};
// const int TextWidthLeft = m_Position.Width() - image->Width() - Margin * 2 - TextPos.Left();
const int TextWidthLeft {TextWidthFull - image->Width() - Margin * 2};
if (m_ScrollSize == 0) { // Avoid DIV/0
esyslog("flatPlus: Error in cComplexContent::AddImageWithFloatedText() m_ScrollSize is 0!");
return;
}
// const int FloatLines = ceil(image->Height() * 1.0 / m_ScrollSize); // Narrowing conversion
const int FloatLines {(image->Height() + m_ScrollSize - 1) / m_ScrollSize}; // Use integer division
cTextFloatingWrapper WrapperFloat; // Modified cTextWrapper lent from skin ElchiHD
WrapperFloat.Set(Text, Font, TextWidthFull, FloatLines, TextWidthLeft); //* Set() strips trailing newlines!
const int NumLines {WrapperFloat.Lines()};
// dsyslog("flatPlus: AddImageWithFloatedText:\nFloatLines %d, Lines %d, TextWithLeft %d, TextWidthFull %d",
// FloatLines, NumLines, TextWidthLeft, TextWidthFull);
std::string Line {""};
Line.reserve(256);
cRect FloatedTextPos {TextPos};
const int Top {TextPos.Top()}; // Cache TextPos.Top() outside the loop
for (int i {0}; i < NumLines; ++i) { // Add text line by line
FloatedTextPos.SetTop(Top + (i * m_ScrollSize));
Line = WrapperFloat.GetLine(i);
// Last line is not justified
if (Config.MenuEventRecordingViewJustify == 1 && i < (NumLines - 1))
JustifyLine(Line, Font, (i < FloatLines) ? TextWidthLeft : TextWidthFull);
AddText(Line.c_str(), false, FloatedTextPos, ColorFg, ColorBg, Font, TextWidthFull, TextHeight, TextAlignment);
}
const cRect ImagePos {TextPos.Left() + TextWidthLeft + Margin, Top, image->Width(), image->Height()};
AddImage(image, ImagePos); // Add the image (Currently always on the right side)
}
void cComplexContent::AddRect(const cRect &Position, tColor ColorBg) {
Contents.emplace_back();
Contents.back().SetRect(Position, ColorBg);
}
void cComplexContent::Draw() {
m_IsShown = true;
if (Contents.empty()) {
esyslog("flatPlus: Error in cComplexContent::Draw() Contents is empty!");
return;
}
for (const auto &content : Contents) {
content.Draw((content.GetContentType() == CT_Image) ? PixmapImage : Pixmap);
}
}
double cComplexContent::ScrollbarSize() const {
if (m_DrawPortHeight == 0) { // Avoid DIV/0}
esyslog("flatPlus: Error in cComplexContent::ScrollbarSize() m_DrawPortHeight is 0!");
return 0;
}
return static_cast<double>(m_Position.Height()) / m_DrawPortHeight;
}
int cComplexContent::ScrollTotal() const {
if (m_ScrollSize == 0) { // Avoid DIV/0}
esyslog("flatPlus: Error in cComplexContent::ScrollTotal() m_ScrollSize is 0!");
return 0;
}
// return ceil(m_DrawPortHeight * 1.0 / m_ScrollSize);
return (m_DrawPortHeight + m_ScrollSize - 1) / m_ScrollSize;
}
int cComplexContent::ScrollShown() const {
if (m_ScrollSize == 0) { // Avoid DIV/0
esyslog("flatPlus: Error in cComplexContent::ScrollShown() m_ScrollSize is 0!");
return 0;
}
// return ceil(m_Position.Height() * 1.0 / m_ScrollSize);
return m_Position.Height() / m_ScrollSize;
}
int cComplexContent::ScrollOffset() const {
if (!Pixmap) return 0;
int y {Pixmap->DrawPort().Point().Y() * -1};
const int PositionHeight {m_Position.Height()};
if (y + PositionHeight + m_ScrollSize > m_DrawPortHeight) {
(y == m_DrawPortHeight - PositionHeight) ? y += m_ScrollSize
: y = m_DrawPortHeight - PositionHeight - 1;
}
if (m_DrawPortHeight == 0) { // Avoid DIV/0
esyslog("flatPlus: Error in cComplexContent::ScrollOffset() m_DrawPortHeight is 0!");
return 0;
}
// return ScrollTotal() * (static_cast<double>(y) / m_DrawPortHeight); // offset = y * 1.0 / m_DrawPortHeight;
return (y * ScrollTotal()) / m_DrawPortHeight;
}
/**
* Scrolls the content of this cComplexContent by one line or one page, depending
* on the given parameters.
*
* @param Up true to scroll up, false to scroll down
* @param Page true to scroll by one page, false to scroll by one line
* @return true if the content was scrolled, false otherwise
*/
bool cComplexContent::Scroll(bool Up, bool Page) {
if (!Pixmap || !PixmapImage) return false;
const int CurrentHeight {Pixmap->DrawPort().Point().Y()};
const int TotalHeight {Pixmap->DrawPort().Height()};
const int ScreenHeight {Pixmap->ViewPort().Height()};
const int LineHeight {m_ScrollSize};
int NewY {CurrentHeight};
if (Up) {
if (Page) { // Page up
NewY = std::min(CurrentHeight + ScreenHeight, 0);
} else if (CurrentHeight < 0) { // Line up
NewY = std::min(CurrentHeight + LineHeight, 0);
} else { // No scrolling needed
return false;
}
} else {
const int MaxScroll {-(TotalHeight - ScreenHeight)}; // Maximum scroll position (negative value)
if (Page) { // Page down
NewY = std::max(CurrentHeight - ScreenHeight, MaxScroll);
} else { // Line down
NewY = (TotalHeight - (-CurrentHeight + LineHeight) > ScreenHeight)
? CurrentHeight - LineHeight
: MaxScroll;
}
}
// Only update when the new Y position is different from the current one
// This prevents unnecessary updates when the position is already correct
if (NewY != CurrentHeight) {
const cPoint NewPoint(0, NewY);
Pixmap->SetDrawPortPoint(NewPoint);
PixmapImage->SetDrawPortPoint(NewPoint);
return true;
}
return false;
}