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