2 Copyright 2004-2005 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, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "wselectlist.h"
29 WSelectList::WSelectList():
30 backgroundColour(DrawStyle::VIEWBACKGROUND)
34 numOptionsDisplayable = 0;
39 darkseloption = false;
44 WSelectList::~WSelectList()
49 void WSelectList::clear()
51 int vsize = options.size();
52 for (int i = 0; i < vsize; i++)
54 delete[] options[i].text;
55 if (options[i].pict) delete options[i].pict;
61 numOptionsDisplayable = 0;
65 void WSelectList::setNoLoop()
70 void WSelectList::setBackgroundColour(const DrawStyle& colour)
72 backgroundColour = colour;
75 void WSelectList::hintSetCurrent(int idx)
78 if (selectedOption >= options.size()) selectedOption = options.size() - 1;
81 void WSelectList::hintSetTop(int idx)
86 int WSelectList::addOption(const char* text, void* data, int selected, TVMediaInfo * pict)
88 int thisNewOption = options.size();
91 wslo.text = new char[strlen(text) + 1];
92 strcpy(wslo.text, text);
95 options.push_back(wslo);
96 if (selected) selectedOption = thisNewOption;
100 void WSelectList::draw()
102 int fontHeight = getFontHeight();
103 int ySeperation = (int) (fontHeight * linesPerOption + gap);
105 numOptionsDisplayable = (area.h - 5) / ySeperation;
107 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
108 if (selectedOption == ((UINT)topOption - 1)) topOption--;
109 // if still not visible...
110 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
112 topOption = selectedOption - (numOptionsDisplayable / 2);
115 if (topOption < 0) topOption = 0;
118 fillColour(backgroundColour);
121 for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
123 if (i == options.size()) return;
124 if ((ypos + ySeperation) > area.h) break;
126 if (i == selectedOption && showseloption)
129 rectangle(0, ypos, area.w, (UINT)(fontHeight * linesPerOption-1), darkseloption ? DrawStyle::SELECTDARKHIGHLIGHT: DrawStyle::SELECTHIGHLIGHT);
131 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::DARKTEXT, options[i].pict);
135 rectangle(0, ypos, area.w, (UINT)(fontHeight * linesPerOption-1), DrawStyle::SELECTBACKGROUND);
137 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::LIGHTTEXT, options[i].pict);
144 void WSelectList::addColumn(int x)
146 if (numColumns == 10) return;
147 columns[numColumns++] = x;
150 int WSelectList::getColumn(int x)
152 if (x>= numColumns) return 0;
156 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const DrawStyle& colour, TVMediaInfo* pict)
160 int fontHeight = getFontHeight();
161 float ypos_mod= ypos + (linesPerOption-floor(linesPerOption))*((float)fontHeight)*0.5f;
165 if (numColumns>1) imagewidth=columns[1]-columns[0];
168 drawTVMedia(*pict, xpos,ypos,imagewidth,fontHeight*linesPerOption,TopLeftLimited);
173 if (!numColumns && linesPerOption == 1)
176 drawText(text, xpos, ypos, width, colour);
181 strncpy(buffer, text, 199);
182 int currentColumn = taboffset;
186 pointer = STRTOKR(buffer, "\t\n", &savepointer);
189 drawText(pointer, xposmod + columns[currentColumn], (int)(ypos_mod + curline * fontHeight), width - columns[currentColumn], colour);
191 pointer = STRTOKR(NULL, "\t\n", &savepointer);
193 char delimiter = text[pointer - buffer-1];
194 if (delimiter == '\t') currentColumn++;
195 else if (delimiter == '\n' ){
196 currentColumn = taboffset;
200 if (curline >= linesPerOption) return;
202 if (currentColumn == 10) return;
207 void WSelectList::up()
209 if (selectedOption > 0)
215 if (!noLoop) selectedOption = options.size() - 1;
219 void WSelectList::down()
221 if (selectedOption < options.size() - 1)
227 if (!noLoop) selectedOption = 0;
231 void WSelectList::pageUp()
233 topOption -= numOptionsDisplayable;
234 if (topOption < 0) topOption = 0;
236 selectedOption = topOption;
239 void WSelectList::pageDown()
241 if ((topOption + numOptionsDisplayable) >= options.size())
243 selectedOption = options.size() - 1;
247 topOption += numOptionsDisplayable;
248 selectedOption = topOption;
252 int WSelectList::getTopOption()
257 int WSelectList::getNumOptions()
259 return options.size();
262 int WSelectList::getBottomOption()
264 UINT retval = topOption + numOptionsDisplayable;
265 if (retval > options.size()) return options.size();
269 int WSelectList::getCurrentOption()
271 return selectedOption;
274 void* WSelectList::getCurrentOptionData()
276 if (!options.size()) return 0;
277 return options[selectedOption].data;
280 int WSelectList::getNumOptionsDisplayable()
282 return numOptionsDisplayable;
285 bool WSelectList::mouseAndroidScroll(int x, int y,int sx, int sy)
287 /* int fontHeight = getFontHeight();
288 int movelines= sy/fontHeight;
290 int seloption=selectedOption+movelines;
291 if (seloption<0) seloption=0;
292 else if (seloption>options.size()-1) seloption=options.size()-1;
293 selectedOption=seloption;*/
298 bool WSelectList::mouseMove(int x, int y)
300 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
301 if (ml>=0 && ml!=(int)selectedOption)
309 bool WSelectList::mouseLBDOWN(int x, int y)
311 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
312 if (ml == (int)selectedOption)
314 /* caller should generate a OK message*/
320 int WSelectList::getMouseLine(int x,int y)
322 int fontHeight = getFontHeight();
323 int ySeperation = (int)(fontHeight * linesPerOption + gap);
326 if (x<0 || x>(int)area.w) return -1;
327 if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
331 int selected=cy/ySeperation;
332 if (y<5) selected=-1;
333 if (selected> ((int)numOptionsDisplayable)) return -1;
334 /* Important: should be the same algorithm used in draw! */
335 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
336 if (selectedOption == ((UINT)topOption - 1)) topOption--;
337 // if still not visible...
338 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
340 topOption = selectedOption - (numOptionsDisplayable / 2);
343 if (topOption < 0) topOption = 0;
345 if ((selected+topOption >= (int) options.size()) ||
346 (selected + topOption < 0)) return -1;
348 return selected + topOption;