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"
25 WSelectList::WSelectList()
29 numOptionsDisplayable = 0;
34 backgroundColour = Colour::VIEWBACKGROUND;
37 WSelectList::~WSelectList()
42 void WSelectList::clear()
44 int vsize = options.size();
45 for (int i = 0; i < vsize; i++)
47 delete[] options[i].text;
53 numOptionsDisplayable = 0;
57 void WSelectList::setNoLoop()
62 void WSelectList::setBackgroundColour(const Colour& colour)
64 backgroundColour = colour;
67 void WSelectList::hintSetCurrent(int idx)
70 if (selectedOption >= options.size()) selectedOption = options.size() - 1;
73 void WSelectList::hintSetTop(int idx)
78 int WSelectList::addOption(const char* text, ULONG data, int selected)
80 int thisNewOption = options.size();
83 wslo.text = new char[strlen(text) + 1];
84 strcpy(wslo.text, text);
86 options.push_back(wslo);
87 if (selected) selectedOption = thisNewOption;
91 void WSelectList::draw()
93 int fontHeight = surface->getFontHeight();
94 int ySeperation = fontHeight + gap;
96 numOptionsDisplayable = (area.h - 5) / ySeperation;
98 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
99 if (selectedOption == ((UINT)topOption - 1)) topOption--;
100 // if still not visible...
101 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
103 topOption = selectedOption - (numOptionsDisplayable / 2);
106 if (topOption < 0) topOption = 0;
109 fillColour(backgroundColour);
112 for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
114 if (i == options.size()) return;
115 if ((ypos + ySeperation) > area.h) break;
117 if (i == selectedOption && showseloption)
119 rectangle(0, ypos, area.w, fontHeight, Colour::SELECTHIGHLIGHT);
120 drawOptionLine(options[i].text, 5, ypos, area.w - 5, Colour::DARKTEXT);
124 drawOptionLine(options[i].text, 5, ypos, area.w - 5, Colour::LIGHTTEXT);
130 void WSelectList::addColumn(int x)
132 if (numColumns == 10) return;
133 columns[numColumns++] = x;
136 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const Colour& colour)
140 drawText(text, xpos, ypos, width, colour);
145 strncpy(buffer, text, 199);
146 int currentColumn = 0;
149 pointer = strtok(buffer, "\t");
152 drawText(pointer, xpos + columns[currentColumn], ypos, width - columns[currentColumn], colour);
154 if (currentColumn == 10) return;
155 pointer = strtok(NULL, "\t");
160 void WSelectList::up()
162 if (selectedOption > 0)
168 if (!noLoop) selectedOption = options.size() - 1;
172 void WSelectList::down()
174 if (selectedOption < options.size() - 1)
180 if (!noLoop) selectedOption = 0;
184 void WSelectList::pageUp()
186 topOption -= numOptionsDisplayable;
187 if (topOption < 0) topOption = 0;
189 selectedOption = topOption;
192 void WSelectList::pageDown()
194 if ((topOption + numOptionsDisplayable) >= options.size())
196 selectedOption = options.size() - 1;
200 topOption += numOptionsDisplayable;
201 selectedOption = topOption;
205 int WSelectList::getTopOption()
210 int WSelectList::getNumOptions()
212 return options.size();
215 int WSelectList::getBottomOption()
217 UINT retval = topOption + numOptionsDisplayable;
218 if (retval > options.size()) return options.size();
222 int WSelectList::getCurrentOption()
224 return selectedOption;
227 ULONG WSelectList::getCurrentOptionData()
229 if (!options.size()) return 0;
230 return options[selectedOption].data;
233 bool WSelectList::mouseMove(int x, int y)
235 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
236 if (ml>=0 && ml!=(int)selectedOption)
244 bool WSelectList::mouseLBDOWN(int x, int y)
246 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
247 if (ml == (int)selectedOption)
249 /* caller should generate a OK message*/
255 int WSelectList::getMouseLine(int x,int y)
257 int fontHeight = surface->getFontHeight();
258 int ySeperation = fontHeight + gap;
261 if (x<0 || x>(int)area.w) return -1;
262 if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
266 int selected=cy/ySeperation;
267 if (y<5) selected=-1;
268 if (selected> ((int)numOptionsDisplayable)) return -1;
269 /* Important: should be the same algorithm used in draw! */
270 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
271 if (selectedOption == ((UINT)topOption - 1)) topOption--;
272 // if still not visible...
273 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
275 topOption = selectedOption - (numOptionsDisplayable / 2);
278 if (topOption < 0) topOption = 0;
280 if ((selected+topOption >= (int) options.size()) ||
281 (selected + topOption < 0)) return -1;
283 return selected + topOption;