]> git.vomp.tv Git - vompclient.git/blob - wselectlist.cc
Got rid of 500 entry limit by converting options to a vector
[vompclient.git] / wselectlist.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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
19 */
20
21 #include "wselectlist.h"
22
23 WSelectList::WSelectList()
24 {
25   selectedOption = 0;
26   topOption = 0;
27   numOptionsDisplayable = 0;
28   numColumns = 0;
29 }
30
31 WSelectList::~WSelectList()
32 {
33   clear();
34 }
35
36 void WSelectList::clear()
37 {
38   int vsize = options.size();
39   for (int i = 0; i < vsize; i++)
40   {
41     delete[] options[i];
42   }
43   options.clear();
44
45   selectedOption = 0;
46   topOption = 0;
47   numOptionsDisplayable = 0;
48   numColumns = 0;
49 }
50
51 void WSelectList::hintSetCurrent(int index)
52 {
53   selectedOption = index;
54   if (selectedOption >= options.size()) selectedOption = options.size() - 1;
55 }
56
57 void WSelectList::hintSetTop(int index)
58 {
59   topOption = index;
60 }
61
62 int WSelectList::addOption(char* text, int selected)
63 {
64   int thisNewOption = options.size();
65   int length = strlen(text);
66   options.push_back(new char[length + 1]);
67   strcpy(options[thisNewOption], text);
68   if (selected) selectedOption = thisNewOption;
69   return thisNewOption;
70 }
71
72 void WSelectList::draw()
73 {
74   int fontHeight = surface->getFontHeight();
75   int ySeperation = fontHeight + 1;
76
77   numOptionsDisplayable = (height - 5) / ySeperation;
78
79   if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
80   if (selectedOption == (topOption - 1)) topOption--;
81   // if still not visible...
82   if ((selectedOption < topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
83   {
84     topOption = selectedOption - (numOptionsDisplayable / 2);
85   }
86
87   if (topOption < 0) topOption = 0;
88
89
90
91   fillColour(Colour::VIEWBACKGROUND);
92
93   int ypos = 5;
94   for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
95   {
96     if (i == options.size()) return;
97     if ((ypos + ySeperation) > height) break;
98
99     if (i == selectedOption)
100     {
101       rectangle(0, ypos, width, fontHeight, Colour::SELECTHIGHLIGHT);
102       drawOptionLine(options[i], 5, ypos, Colour::DARKTEXT);
103     }
104     else
105     {
106       drawOptionLine(options[i], 5, ypos, Colour::LIGHTTEXT);
107     }
108     ypos += ySeperation;
109   }
110 }
111
112 void WSelectList::addColumn(int x)
113 {
114   if (numColumns == 10) return;
115   columns[numColumns++] = x;
116 }
117
118 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, Colour& colour)
119 {
120   if (!numColumns)
121   {
122     drawText(text, xpos, ypos, colour);
123   }
124   else
125   {
126     char buffer[200];
127     strncpy(buffer, text, 199);
128     int currentColumn = 0;
129     char* pointer;
130
131     pointer = strtok(buffer, "\t");
132     while(pointer)
133     {
134       drawText(pointer, xpos + columns[currentColumn], ypos, colour);
135       currentColumn++;
136       if (currentColumn == 10) return;
137       pointer = strtok(NULL, "\t");
138     }
139   }
140 }
141
142
143 void WSelectList::up()
144 {
145   if (selectedOption > 0)
146   {
147     selectedOption--;
148   }
149   else
150   {
151     selectedOption = options.size() - 1;
152   }
153 }
154
155 void WSelectList::down()
156 {
157   if (selectedOption < options.size() - 1)
158   {
159     selectedOption++;
160   }
161   else
162   {
163     selectedOption = 0;
164   }
165 }
166
167 void WSelectList::pageUp()
168 {
169   topOption -= numOptionsDisplayable;
170   if (topOption < 0) topOption = 0;
171
172   selectedOption = topOption;
173 }
174
175 void WSelectList::pageDown()
176 {
177   if ((topOption + numOptionsDisplayable) >= options.size()) return;
178
179   topOption += numOptionsDisplayable;
180   selectedOption = topOption;
181 }
182
183 int WSelectList::getTopOption()
184 {
185   return topOption;
186 }
187
188 int WSelectList::getNumOptions()
189 {
190   return options.size();
191 }
192
193 int WSelectList::getBottomOption()
194 {
195   UINT retval = topOption + numOptionsDisplayable;
196   if (retval > options.size()) return options.size();
197   else return retval;
198 }
199
200 int WSelectList::getCurrentOption()
201 {
202   return selectedOption;
203 }