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 darkseloption = false;
35 backgroundColour = Colour::VIEWBACKGROUND;
38 WSelectList::~WSelectList()
43 void WSelectList::clear()
45 int vsize = options.size();
46 for (int i = 0; i < vsize; i++)
48 delete[] options[i].text;
54 numOptionsDisplayable = 0;
58 void WSelectList::setNoLoop()
63 void WSelectList::setBackgroundColour(const Colour& colour)
65 backgroundColour = colour;
68 void WSelectList::hintSetCurrent(int idx)
71 if (selectedOption >= options.size()) selectedOption = options.size() - 1;
74 void WSelectList::hintSetTop(int idx)
79 int WSelectList::addOption(const char* text, ULONG data, int selected)
81 int thisNewOption = options.size();
84 wslo.text = new char[strlen(text) + 1];
85 strcpy(wslo.text, text);
87 options.push_back(wslo);
88 if (selected) selectedOption = thisNewOption;
92 void WSelectList::draw()
94 int fontHeight = surface->getFontHeight();
95 int ySeperation = fontHeight + gap;
97 numOptionsDisplayable = (area.h - 5) / ySeperation;
99 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
100 if (selectedOption == ((UINT)topOption - 1)) topOption--;
101 // if still not visible...
102 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
104 topOption = selectedOption - (numOptionsDisplayable / 2);
107 if (topOption < 0) topOption = 0;
110 fillColour(backgroundColour);
113 for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
115 if (i == options.size()) return;
116 if ((ypos + ySeperation) > area.h) break;
118 if (i == selectedOption && showseloption)
120 rectangle(0, ypos, area.w, fontHeight, darkseloption ? Colour::SELECTDARKHIGHLIGHT: Colour::SELECTHIGHLIGHT);
121 drawOptionLine(options[i].text, 5, ypos, area.w - 5, Colour::DARKTEXT);
125 drawOptionLine(options[i].text, 5, ypos, area.w - 5, Colour::LIGHTTEXT);
131 void WSelectList::addColumn(int x)
133 if (numColumns == 10) return;
134 columns[numColumns++] = x;
137 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const Colour& colour)
141 drawText(text, xpos, ypos, width, colour);
146 strncpy(buffer, text, 199);
147 int currentColumn = 0;
150 pointer = strtok(buffer, "\t");
153 drawText(pointer, xpos + columns[currentColumn], ypos, width - columns[currentColumn], colour);
155 if (currentColumn == 10) return;
156 pointer = strtok(NULL, "\t");
161 void WSelectList::up()
163 if (selectedOption > 0)
169 if (!noLoop) selectedOption = options.size() - 1;
173 void WSelectList::down()
175 if (selectedOption < options.size() - 1)
181 if (!noLoop) selectedOption = 0;
185 void WSelectList::pageUp()
187 topOption -= numOptionsDisplayable;
188 if (topOption < 0) topOption = 0;
190 selectedOption = topOption;
193 void WSelectList::pageDown()
195 if ((topOption + numOptionsDisplayable) >= options.size())
197 selectedOption = options.size() - 1;
201 topOption += numOptionsDisplayable;
202 selectedOption = topOption;
206 int WSelectList::getTopOption()
211 int WSelectList::getNumOptions()
213 return options.size();
216 int WSelectList::getBottomOption()
218 UINT retval = topOption + numOptionsDisplayable;
219 if (retval > options.size()) return options.size();
223 int WSelectList::getCurrentOption()
225 return selectedOption;
228 ULONG WSelectList::getCurrentOptionData()
230 if (!options.size()) return 0;
231 return options[selectedOption].data;
234 bool WSelectList::mouseMove(int x, int y)
236 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
237 if (ml>=0 && ml!=(int)selectedOption)
245 bool WSelectList::mouseLBDOWN(int x, int y)
247 int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
248 if (ml == (int)selectedOption)
250 /* caller should generate a OK message*/
256 int WSelectList::getMouseLine(int x,int y)
258 int fontHeight = surface->getFontHeight();
259 int ySeperation = fontHeight + gap;
262 if (x<0 || x>(int)area.w) return -1;
263 if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
267 int selected=cy/ySeperation;
268 if (y<5) selected=-1;
269 if (selected> ((int)numOptionsDisplayable)) return -1;
270 /* Important: should be the same algorithm used in draw! */
271 if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
272 if (selectedOption == ((UINT)topOption - 1)) topOption--;
273 // if still not visible...
274 if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
276 topOption = selectedOption - (numOptionsDisplayable / 2);
279 if (topOption < 0) topOption = 0;
281 if ((selected+topOption >= (int) options.size()) ||
282 (selected + topOption < 0)) return -1;
284 return selected + topOption;