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/>.
25 #include "wselectlist.h"
27 WSelectList::WSelectList():
28 backgroundColour(DrawStyle::VIEWBACKGROUND)
32 WSelectList::~WSelectList()
37 void WSelectList::clear()
39 int vsize = options.size();
40 for (int i = 0; i < vsize; i++)
42 delete[] options[i].text;
43 if (options[i].pict) delete options[i].pict;
49 numOptionsDisplayable = 0;
53 void WSelectList::setNoLoop()
58 void WSelectList::setBackgroundColour(const DrawStyle& colour)
60 backgroundColour = colour;
63 void WSelectList::hintSetCurrent(int idx)
66 if (selectedOption >= static_cast<int>(options.size())) selectedOption = options.size() - 1;
69 void WSelectList::hintSetTop(int idx)
74 int WSelectList::addOption(const char* text, void* data, int selected, TVMediaInfo* pict)
76 int thisNewOption = options.size();
79 wslo.text = new char[strlen(text) + 1];
80 strcpy(wslo.text, text);
83 options.push_back(wslo);
84 if (selected) selectedOption = thisNewOption;
88 void WSelectList::draw()
90 int fontHeight = getFontHeight();
91 int ySeparation = static_cast<int>(static_cast<float>(fontHeight) * linesPerOption) + gap;
93 numOptionsDisplayable = (area.h - 5) / ySeparation;
95 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
96 if (selectedOption == (topOption - 1)) topOption--;
97 // if still not visible...
98 if ((selectedOption < topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
100 topOption = selectedOption - (numOptionsDisplayable / 2);
103 if (topOption < 0) topOption = 0;
105 fillColour(backgroundColour);
108 for (int i = topOption; i < (topOption + numOptionsDisplayable); i++)
110 if (i == static_cast<int>(options.size())) return;
111 if ((ypos + ySeparation) > area.h) break;
113 if (i == selectedOption && showseloption)
115 rectangle(0, ypos, area.w, static_cast<UINT>(static_cast<float>(fontHeight) * linesPerOption) - 1, darkseloption ? DrawStyle::SELECTDARKHIGHLIGHT : DrawStyle::SELECTHIGHLIGHT);
116 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::DARKTEXT, options[i].pict);
120 rectangle(0, ypos, area.w, static_cast<UINT>(static_cast<float>(fontHeight) * linesPerOption) - 1, DrawStyle::SELECTBACKGROUND);
121 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::LIGHTTEXT, options[i].pict);
127 void WSelectList::addColumn(int x)
129 if (numColumns == 10) return;
130 columns[numColumns++] = x;
133 int WSelectList::getColumn(int x)
135 if (x >= numColumns) return 0;
139 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const DrawStyle& colour, TVMediaInfo* pict)
143 int fontHeight = getFontHeight();
144 float ypos_mod = static_cast<float>(ypos) + (linesPerOption - floor(linesPerOption)) * static_cast<float>(fontHeight) * 0.5f;
148 if (numColumns > 1) imagewidth = columns[1] - columns[0];
151 drawTVMedia(*pict, static_cast<float>(xpos), static_cast<float>(ypos), static_cast<float>(imagewidth),
152 static_cast<float>(fontHeight) * linesPerOption, TopLeftLimited);
157 if (!numColumns && linesPerOption == 1)
159 drawText(text, xpos, ypos, width, colour);
164 strncpy(buffer, text, 199);
165 int currentColumn = taboffset;
169 pointer = STRTOKR(buffer, "\t\n", &savepointer);
172 drawText(pointer, xposmod + columns[currentColumn], (fontHeight * curline) + static_cast<int>(ypos_mod), width - columns[currentColumn], colour);
174 pointer = STRTOKR(NULL, "\t\n", &savepointer);
177 char delimiter = text[pointer - buffer - 1];
178 if (delimiter == '\t') currentColumn++;
179 else if (delimiter == '\n')
181 currentColumn = taboffset;
185 if (static_cast<float>(curline) >= linesPerOption) return;
187 if (currentColumn == 10) return;
192 void WSelectList::up()
194 if (selectedOption > 0)
200 if (!noLoop) selectedOption = options.size() - 1;
204 void WSelectList::down()
206 if (selectedOption < (static_cast<int>(options.size()) - 1))
212 if (!noLoop) selectedOption = 0;
216 void WSelectList::pageUp()
218 topOption -= numOptionsDisplayable;
219 if (topOption < 0) topOption = 0;
221 selectedOption = topOption;
224 void WSelectList::pageDown()
226 if ((topOption + numOptionsDisplayable) >= static_cast<int>(options.size()))
228 selectedOption = options.size() - 1;
232 topOption += numOptionsDisplayable;
233 selectedOption = topOption;
237 int WSelectList::getTopOption()
242 int WSelectList::getNumOptions()
244 return options.size();
247 int WSelectList::getBottomOption()
249 UINT retval = topOption + numOptionsDisplayable;
250 if (retval > options.size()) return options.size();
254 int WSelectList::getCurrentOption()
256 return selectedOption;
259 void* WSelectList::getCurrentOptionData()
261 if (!options.size()) return 0;
262 return options[selectedOption].data;
265 int WSelectList::getNumOptionsDisplayable()
267 return numOptionsDisplayable;
270 bool WSelectList::mouseAndroidScroll(int /* x */, int /* y */, int /* sx */, int /* sy */)
273 * int fontHeight = getFontHeight();
274 * int movelines= sy/fontHeight;
276 * int seloption=selectedOption+movelines;
277 * if (seloption<0) seloption=0;
278 * else if (seloption>options.size()-1) seloption=options.size()-1;
279 * selectedOption=seloption;
284 bool WSelectList::mouseMove(int x, int y)
286 int ml = getMouseLine(x - getRootBoxOffsetX(), y - getRootBoxOffsetY());
287 if (ml >= 0 && ml != selectedOption)
295 bool WSelectList::mouseLBDOWN(int x, int y)
297 int ml = getMouseLine(x - getRootBoxOffsetX(), y - getRootBoxOffsetY());
298 if (ml == selectedOption)
300 /* caller should generate a OK message*/
306 int WSelectList::getMouseLine(int x, int y)
308 int fontHeight = getFontHeight();
309 int ySeparation = static_cast<int>(static_cast<float>(fontHeight) * linesPerOption) + gap;
311 if (y < 0) return -1;
312 if (x < 0 || x > static_cast<int>(area.w)) return -1;
313 if (y > (10 + numOptionsDisplayable * ySeparation)) return -1;
317 int selected = cy / ySeparation;
318 if (y < 5) selected = -1;
319 if (selected > numOptionsDisplayable) return -1;
320 /* Important: should be the same algorithm used in draw! */
321 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
322 if (selectedOption == (topOption - 1)) topOption--;
323 // if still not visible...
324 if ((selectedOption < topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
326 topOption = selectedOption - (numOptionsDisplayable / 2);
329 if (topOption < 0) topOption = 0;
330 if ((selected + topOption >= static_cast<int>(options.size())) || (selected + topOption < 0)) return -1;
331 return selected + topOption;