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