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"
26 WSelectList::WSelectList():
27 backgroundColour(DrawStyle::VIEWBACKGROUND)
31 numOptionsDisplayable = 0;
36 darkseloption = false;
40 WSelectList::~WSelectList()
45 void WSelectList::clear()
47 int vsize = options.size();
48 for (int i = 0; i < vsize; i++)
50 delete[] options[i].text;
56 numOptionsDisplayable = 0;
60 void WSelectList::setNoLoop()
65 void WSelectList::setBackgroundColour(const DrawStyle& colour)
67 backgroundColour = colour;
70 void WSelectList::hintSetCurrent(int idx)
73 if (selectedOption >= options.size()) selectedOption = options.size() - 1;
76 void WSelectList::hintSetTop(int idx)
81 int WSelectList::addOption(const char* text, ULONG data, int selected)
83 int thisNewOption = options.size();
86 wslo.text = new char[strlen(text) + 1];
87 strcpy(wslo.text, text);
89 options.push_back(wslo);
90 if (selected) selectedOption = thisNewOption;
94 void WSelectList::draw()
96 int fontHeight = getFontHeight();
97 int ySeperation = fontHeight + gap;
99 numOptionsDisplayable = (area.h - 5) / ySeperation;
101 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
102 if (selectedOption == ((UINT)topOption - 1)) topOption--;
103 // if still not visible...
104 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
106 topOption = selectedOption - (numOptionsDisplayable / 2);
109 if (topOption < 0) topOption = 0;
112 fillColour(backgroundColour);
115 for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
117 if (i == options.size()) return;
118 if ((ypos + ySeperation) > area.h) break;
120 if (i == selectedOption && showseloption)
123 rectangle(0, ypos, area.w, fontHeight, darkseloption ? DrawStyle::SELECTDARKHIGHLIGHT: DrawStyle::SELECTHIGHLIGHT);
125 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::DARKTEXT);
130 drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::LIGHTTEXT);
137 void WSelectList::addColumn(int x)
139 if (numColumns == 10) return;
140 columns[numColumns++] = x;
143 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const DrawStyle& colour)
148 drawText(text, xpos, ypos, width, colour);
153 strncpy(buffer, text, 199);
154 int currentColumn = 0;
157 pointer = strtok(buffer, "\t");
161 drawText(pointer, xpos + columns[currentColumn], ypos, width - columns[currentColumn], colour);
164 if (currentColumn == 10) return;
165 pointer = strtok(NULL, "\t");
170 void WSelectList::up()
172 if (selectedOption > 0)
178 if (!noLoop) selectedOption = options.size() - 1;
182 void WSelectList::down()
184 if (selectedOption < options.size() - 1)
190 if (!noLoop) selectedOption = 0;
194 void WSelectList::pageUp()
196 topOption -= numOptionsDisplayable;
197 if (topOption < 0) topOption = 0;
199 selectedOption = topOption;
202 void WSelectList::pageDown()
204 if ((topOption + numOptionsDisplayable) >= options.size())
206 selectedOption = options.size() - 1;
210 topOption += numOptionsDisplayable;
211 selectedOption = topOption;
215 int WSelectList::getTopOption()
220 int WSelectList::getNumOptions()
222 return options.size();
225 int WSelectList::getBottomOption()
227 UINT retval = topOption + numOptionsDisplayable;
228 if (retval > options.size()) return options.size();
232 int WSelectList::getCurrentOption()
234 return selectedOption;
237 ULONG WSelectList::getCurrentOptionData()
239 if (!options.size()) return 0;
240 return options[selectedOption].data;
243 bool WSelectList::mouseAndroidScroll(int x, int y,int sx, int sy)
245 /* int fontHeight = getFontHeight();
246 int movelines= sy/fontHeight;
248 int seloption=selectedOption+movelines;
249 if (seloption<0) seloption=0;
250 else if (seloption>options.size()-1) seloption=options.size()-1;
251 selectedOption=seloption;*/
256 bool WSelectList::mouseMove(int x, int y)
258 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
259 if (ml>=0 && ml!=(int)selectedOption)
267 bool WSelectList::mouseLBDOWN(int x, int y)
269 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
270 if (ml == (int)selectedOption)
272 /* caller should generate a OK message*/
278 int WSelectList::getMouseLine(int x,int y)
280 int fontHeight = getFontHeight();
281 int ySeperation = fontHeight + gap;
284 if (x<0 || x>(int)area.w) return -1;
285 if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
289 int selected=cy/ySeperation;
290 if (y<5) selected=-1;
291 if (selected> ((int)numOptionsDisplayable)) return -1;
292 /* Important: should be the same algorithm used in draw! */
293 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
294 if (selectedOption == ((UINT)topOption - 1)) topOption--;
295 // if still not visible...
296 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
298 topOption = selectedOption - (numOptionsDisplayable / 2);
301 if (topOption < 0) topOption = 0;
303 if ((selected+topOption >= (int) options.size()) ||
304 (selected + topOption < 0)) return -1;
306 return selected + topOption;