2 Copyright 2004-2020 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
26 #include "wselectlist.h"
28 WSelectList::WSelectList():
29 backgroundColour(DrawStyle::VIEWBACKGROUND)
33 WSelectList::~WSelectList()
38 void WSelectList::clear()
40 int vsize = options.size();
41 for (int i = 0; i < vsize; i++)
43 delete[] options[i].text;
44 if (options[i].pict) delete options[i].pict;
50 numOptionsDisplayable = 0;
54 void WSelectList::setNoLoop()
59 void WSelectList::setBackgroundColour(const DrawStyle& colour)
61 backgroundColour = colour;
64 void WSelectList::hintSetCurrent(int idx)
67 if (selectedOption >= static_cast<int>(options.size())) selectedOption = options.size() - 1;
70 void WSelectList::hintSetTop(int idx)
75 int WSelectList::addOption(const char* text, void* data, int selected, TVMediaInfo* pict)
77 int thisNewOption = options.size();
80 wslo.text = new char[strlen(text) + 1];
81 strcpy(wslo.text, text);
84 options.push_back(wslo);
85 if (selected) selectedOption = thisNewOption;
89 void WSelectList::draw()
91 int fontHeight = getFontHeight();
92 int ySeparation = static_cast<int>(static_cast<float>(fontHeight) * linesPerOption) + gap;
94 numOptionsDisplayable = (area.h - 5) / ySeparation;
96 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
97 if (selectedOption == (topOption - 1)) topOption--;
98 // if still not visible...
99 if ((selectedOption < topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
101 topOption = selectedOption - (numOptionsDisplayable / 2);
104 if (topOption < 0) topOption = 0;
106 fillColour(backgroundColour);
109 for (int i = topOption; i < (topOption + numOptionsDisplayable); i++)
111 if (i == static_cast<int>(options.size())) return;
112 if ((ypos + ySeparation) > area.h) break;
114 if (i == selectedOption && showseloption)
116 rectangle(0, ypos, area.w, static_cast<UINT>(static_cast<float>(fontHeight) * linesPerOption) - 1, darkseloption ? DrawStyle::SELECTDARKHIGHLIGHT : DrawStyle::SELECTHIGHLIGHT);
117 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::DARKTEXT, options[i].pict);
121 rectangle(0, ypos, area.w, static_cast<UINT>(static_cast<float>(fontHeight) * linesPerOption) - 1, DrawStyle::SELECTBACKGROUND);
122 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::LIGHTTEXT, options[i].pict);
128 void WSelectList::addColumn(int x)
130 if (numColumns == 10) return;
131 columns[numColumns++] = x;
134 int WSelectList::getColumn(int x)
136 if (x >= numColumns) return 0;
140 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const DrawStyle& colour, TVMediaInfo* pict)
144 int fontHeight = getFontHeight();
145 float ypos_mod = static_cast<float>(ypos) + (linesPerOption - floor(linesPerOption)) * static_cast<float>(fontHeight) * 0.5f;
149 if (numColumns > 1) imagewidth = columns[1] - columns[0];
152 drawTVMedia(*pict, static_cast<float>(xpos), static_cast<float>(ypos), static_cast<float>(imagewidth),
153 static_cast<float>(fontHeight) * linesPerOption, TopLeftLimited);
158 if (!numColumns && linesPerOption == 1)
160 drawText(text, xpos, ypos, width, colour);
165 strncpy(buffer, text, 199);
166 int currentColumn = taboffset;
170 pointer = STRTOKR(buffer, "\t\n", &savepointer);
173 drawText(pointer, xposmod + columns[currentColumn], (fontHeight * curline) + static_cast<int>(ypos_mod), width - columns[currentColumn], colour);
175 pointer = STRTOKR(NULL, "\t\n", &savepointer);
178 char delimiter = text[pointer - buffer - 1];
179 if (delimiter == '\t') currentColumn++;
180 else if (delimiter == '\n')
182 currentColumn = taboffset;
186 if (static_cast<float>(curline) >= linesPerOption) return;
188 if (currentColumn == 10) return;
193 void WSelectList::up()
195 if (selectedOption > 0)
201 if (!noLoop) selectedOption = options.size() - 1;
205 void WSelectList::down()
207 if (selectedOption < (static_cast<int>(options.size()) - 1))
213 if (!noLoop) selectedOption = 0;
217 void WSelectList::pageUp()
219 topOption -= numOptionsDisplayable;
220 if (topOption < 0) topOption = 0;
222 selectedOption = topOption;
225 void WSelectList::pageDown()
227 if ((topOption + numOptionsDisplayable) >= static_cast<int>(options.size()))
229 selectedOption = options.size() - 1;
233 topOption += numOptionsDisplayable;
234 selectedOption = topOption;
238 int WSelectList::getTopOption()
243 int WSelectList::getNumOptions()
245 return options.size();
248 int WSelectList::getBottomOption()
250 UINT retval = topOption + numOptionsDisplayable;
251 if (retval > options.size()) return options.size();
255 int WSelectList::getCurrentOption()
257 return selectedOption;
260 void* WSelectList::getCurrentOptionData()
262 if (!options.size()) return 0;
263 return options[selectedOption].data;
266 int WSelectList::getNumOptionsDisplayable()
268 return numOptionsDisplayable;
271 bool WSelectList::mouseAndroidScroll(int /* x */, int /* y */, int /* sx */, int /* sy */)
274 * int fontHeight = getFontHeight();
275 * int movelines= sy/fontHeight;
277 * int seloption=selectedOption+movelines;
278 * if (seloption<0) seloption=0;
279 * else if (seloption>options.size()-1) seloption=options.size()-1;
280 * selectedOption=seloption;
285 bool WSelectList::mouseMove(int x, int y)
287 int ml = getMouseLine(x - getRootBoxOffsetX(), y - getRootBoxOffsetY());
288 if (ml >= 0 && ml != selectedOption)
296 bool WSelectList::mouseLBDOWN(int x, int y)
298 int ml = getMouseLine(x - getRootBoxOffsetX(), y - getRootBoxOffsetY());
299 if (ml == selectedOption)
301 /* caller should generate a OK message*/
307 int WSelectList::getMouseLine(int x, int y)
309 int fontHeight = getFontHeight();
310 int ySeparation = static_cast<int>(static_cast<float>(fontHeight) * linesPerOption) + gap;
312 if (y < 0) return -1;
313 if (x < 0 || x > static_cast<int>(area.w)) return -1;
314 if (y > (10 + numOptionsDisplayable * ySeparation)) return -1;
318 int selected = cy / ySeparation;
319 if (y < 5) selected = -1;
320 if (selected > numOptionsDisplayable) return -1;
321 /* Important: should be the same algorithm used in draw! */
322 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
323 if (selectedOption == (topOption - 1)) topOption--;
324 // if still not visible...
325 if ((selectedOption < topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
327 topOption = selectedOption - (numOptionsDisplayable / 2);
330 if (topOption < 0) topOption = 0;
331 if ((selected + topOption >= static_cast<int>(options.size())) || (selected + topOption < 0)) return -1;
332 return selected + topOption;