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;
33 WSelectList::~WSelectList()
38 void WSelectList::clear()
40 int vsize = options.size();
41 for (int i = 0; i < vsize; i++)
43 delete[] options[i].text;
49 numOptionsDisplayable = 0;
53 void WSelectList::setNoLoop()
58 void WSelectList::hintSetCurrent(int idx)
61 if (selectedOption >= options.size()) selectedOption = options.size() - 1;
64 void WSelectList::hintSetTop(int idx)
69 int WSelectList::addOption(char* text, ULONG data, int selected)
71 int thisNewOption = options.size();
74 wslo.text = new char[strlen(text) + 1];
75 strcpy(wslo.text, text);
77 options.push_back(wslo);
78 if (selected) selectedOption = thisNewOption;
82 void WSelectList::draw()
84 int fontHeight = surface->getFontHeight();
85 int ySeperation = fontHeight + gap;
87 numOptionsDisplayable = (area.h - 5) / ySeperation;
89 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
90 if (selectedOption == ((UINT)topOption - 1)) topOption--;
91 // if still not visible...
92 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
94 topOption = selectedOption - (numOptionsDisplayable / 2);
97 if (topOption < 0) topOption = 0;
100 fillColour(backgroundColour);
103 for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
105 if (i == options.size()) return;
106 if ((ypos + ySeperation) > area.h) break;
108 if (i == selectedOption)
110 rectangle(0, ypos, area.w, fontHeight, Colour::SELECTHIGHLIGHT);
111 drawOptionLine(options[i].text, 5, ypos, Colour::DARKTEXT);
115 drawOptionLine(options[i].text, 5, ypos, Colour::LIGHTTEXT);
121 void WSelectList::addColumn(int x)
123 if (numColumns == 10) return;
124 columns[numColumns++] = x;
127 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, Colour& colour)
131 drawText(text, xpos, ypos, colour);
136 strncpy(buffer, text, 199);
137 int currentColumn = 0;
140 pointer = strtok(buffer, "\t");
143 drawText(pointer, xpos + columns[currentColumn], ypos, colour);
145 if (currentColumn == 10) return;
146 pointer = strtok(NULL, "\t");
151 void WSelectList::up()
153 if (selectedOption > 0)
159 if (!noLoop) selectedOption = options.size() - 1;
163 void WSelectList::down()
165 if (selectedOption < options.size() - 1)
171 if (!noLoop) selectedOption = 0;
175 void WSelectList::pageUp()
177 topOption -= numOptionsDisplayable;
178 if (topOption < 0) topOption = 0;
180 selectedOption = topOption;
183 void WSelectList::pageDown()
185 if ((topOption + numOptionsDisplayable) >= options.size())
187 selectedOption = options.size() - 1;
191 topOption += numOptionsDisplayable;
192 selectedOption = topOption;
196 int WSelectList::getTopOption()
201 int WSelectList::getNumOptions()
203 return options.size();
206 int WSelectList::getBottomOption()
208 UINT retval = topOption + numOptionsDisplayable;
209 if (retval > options.size()) return options.size();
213 int WSelectList::getCurrentOption()
215 return selectedOption;
218 ULONG WSelectList::getCurrentOptionData()
220 return options[selectedOption].data;
223 bool WSelectList::mouseMove(int x, int y)
225 int ml = getMouseLine(x-offsetX,y-offsetY);
226 if (ml>=0 && ml!=(int)selectedOption)
234 bool WSelectList::mouseLBDOWN(int x, int y)
236 int ml = getMouseLine(x-offsetX, y-offsetY);
237 if (ml == (int)selectedOption)
239 /* caller should generate a OK message*/
245 int WSelectList::getMouseLine(int x,int y)
247 int fontHeight = surface->getFontHeight();
248 int ySeperation = fontHeight + gap;
251 if (x<0 || x>(int)area.w) return -1;
252 if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
256 int selected=cy/ySeperation;
257 if (y<5) selected=-1;
258 if (selected> ((int)numOptionsDisplayable)) return -1;
259 /* Important: should be the same algorithm used in draw! */
260 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
261 if (selectedOption == ((UINT)topOption - 1)) topOption--;
262 // if still not visible...
263 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
265 topOption = selectedOption - (numOptionsDisplayable / 2);
268 if (topOption < 0) topOption = 0;
270 if ((selected+topOption >= (int) options.size()) ||
271 (selected + topOption < 0)) return -1;
273 return selected + topOption;