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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wselectlist.h"
23 WSelectList::WSelectList()
27 numOptionsDisplayable = 0;
34 WSelectList::~WSelectList()
39 void WSelectList::clear()
41 int vsize = options.size();
42 for (int i = 0; i < vsize; i++)
44 delete[] options[i].text;
50 numOptionsDisplayable = 0;
54 void WSelectList::setNoLoop()
59 void WSelectList::hintSetCurrent(int idx)
62 if (selectedOption >= options.size()) selectedOption = options.size() - 1;
65 void WSelectList::hintSetTop(int idx)
70 int WSelectList::addOption(char* text, ULONG data, int selected)
72 int thisNewOption = options.size();
75 wslo.text = new char[strlen(text) + 1];
76 strcpy(wslo.text, text);
78 options.push_back(wslo);
79 if (selected) selectedOption = thisNewOption;
83 void WSelectList::draw()
85 int fontHeight = surface->getFontHeight();
86 int ySeperation = fontHeight + gap;
88 numOptionsDisplayable = (area.h - 5) / ySeperation;
90 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
91 if (selectedOption == ((UINT)topOption - 1)) topOption--;
92 // if still not visible...
93 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
95 topOption = selectedOption - (numOptionsDisplayable / 2);
98 if (topOption < 0) topOption = 0;
101 fillColour(backgroundColour);
104 for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
106 if (i == options.size()) return;
107 if ((ypos + ySeperation) > area.h) break;
109 if (i == selectedOption && showseloption)
111 rectangle(0, ypos, area.w, fontHeight, Colour::SELECTHIGHLIGHT);
112 drawOptionLine(options[i].text, 5, ypos, Colour::DARKTEXT);
116 drawOptionLine(options[i].text, 5, ypos, Colour::LIGHTTEXT);
122 void WSelectList::addColumn(int x)
124 if (numColumns == 10) return;
125 columns[numColumns++] = x;
128 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, Colour& colour)
132 drawText(text, xpos, ypos, colour);
137 strncpy(buffer, text, 199);
138 int currentColumn = 0;
141 pointer = strtok(buffer, "\t");
144 drawText(pointer, xpos + columns[currentColumn], ypos, colour);
146 if (currentColumn == 10) return;
147 pointer = strtok(NULL, "\t");
152 void WSelectList::up()
154 if (selectedOption > 0)
160 if (!noLoop) selectedOption = options.size() - 1;
164 void WSelectList::down()
166 if (selectedOption < options.size() - 1)
172 if (!noLoop) selectedOption = 0;
176 void WSelectList::pageUp()
178 topOption -= numOptionsDisplayable;
179 if (topOption < 0) topOption = 0;
181 selectedOption = topOption;
184 void WSelectList::pageDown()
186 if ((topOption + numOptionsDisplayable) >= options.size())
188 selectedOption = options.size() - 1;
192 topOption += numOptionsDisplayable;
193 selectedOption = topOption;
197 int WSelectList::getTopOption()
202 int WSelectList::getNumOptions()
204 return options.size();
207 int WSelectList::getBottomOption()
209 UINT retval = topOption + numOptionsDisplayable;
210 if (retval > options.size()) return options.size();
214 int WSelectList::getCurrentOption()
216 return selectedOption;
219 ULONG WSelectList::getCurrentOptionData()
221 if (!options.size()) return 0;
222 return options[selectedOption].data;
225 bool WSelectList::mouseMove(int x, int y)
227 int ml = getMouseLine(x-offsetX,y-offsetY);
228 if (ml>=0 && ml!=(int)selectedOption)
236 bool WSelectList::mouseLBDOWN(int x, int y)
238 int ml = getMouseLine(x-offsetX, y-offsetY);
239 if (ml == (int)selectedOption)
241 /* caller should generate a OK message*/
247 int WSelectList::getMouseLine(int x,int y)
249 int fontHeight = surface->getFontHeight();
250 int ySeperation = fontHeight + gap;
253 if (x<0 || x>(int)area.w) return -1;
254 if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
258 int selected=cy/ySeperation;
259 if (y<5) selected=-1;
260 if (selected> ((int)numOptionsDisplayable)) return -1;
261 /* Important: should be the same algorithm used in draw! */
262 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
263 if (selectedOption == ((UINT)topOption - 1)) topOption--;
264 // if still not visible...
265 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
267 topOption = selectedOption - (numOptionsDisplayable / 2);
270 if (topOption < 0) topOption = 0;
272 if ((selected+topOption >= (int) options.size()) ||
273 (selected + topOption < 0)) return -1;
275 return selected + topOption;