]> git.vomp.tv Git - vompclient.git/blob - wselectlist.cc
1c5e2d7f0e627fb10344711fadc96c62b098986a
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "wselectlist.h"
22
23 #include "colour.h"
24 #include "log.h"
25
26 WSelectList::WSelectList():
27 backgroundColour(DrawStyle::VIEWBACKGROUND)
28 {
29   selectedOption = 0;
30   topOption = 0;
31   numOptionsDisplayable = 0;
32   numColumns = 0;
33   noLoop = 0;
34   gap = 1;
35   showseloption = true;
36   darkseloption = false;
37
38 }
39
40 WSelectList::~WSelectList()
41 {
42   clear();
43 }
44
45 void WSelectList::clear()
46 {
47   int vsize = options.size();
48   for (int i = 0; i < vsize; i++)
49   {
50     delete[] options[i].text;
51   }
52   options.clear();
53
54   selectedOption = 0;
55   topOption = 0;
56   numOptionsDisplayable = 0;
57   numColumns = 0;
58 }
59
60 void WSelectList::setNoLoop()
61 {
62   noLoop = 1;
63 }
64
65 void WSelectList::setBackgroundColour(const DrawStyle& colour)
66 {
67   backgroundColour = colour;
68 }
69
70 void WSelectList::hintSetCurrent(int idx)
71 {
72   selectedOption = idx;
73   if (selectedOption >= options.size()) selectedOption = options.size() - 1;
74 }
75
76 void WSelectList::hintSetTop(int idx)
77 {
78   topOption = idx;
79 }
80
81 int WSelectList::addOption(const char* text, ULONG data, int selected)
82 {
83   int thisNewOption = options.size();
84
85   wsloption wslo;
86   wslo.text = new char[strlen(text) + 1];
87   strcpy(wslo.text, text);
88   wslo.data = data;
89   options.push_back(wslo);
90   if (selected) selectedOption = thisNewOption;
91   return thisNewOption;
92 }
93
94 void WSelectList::draw()
95 {
96   int fontHeight = getFontHeight();
97   int ySeperation = fontHeight + gap;
98
99   numOptionsDisplayable = (area.h - 5) / ySeperation;
100
101   if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
102   if (selectedOption == ((UINT)topOption - 1)) topOption--;
103   // if still not visible...
104   if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
105   {
106     topOption = selectedOption - (numOptionsDisplayable / 2);
107   }
108
109   if (topOption < 0) topOption = 0;
110
111
112   fillColour(backgroundColour);
113
114   UINT ypos = 5;
115   for (UINT i = topOption; i < (topOption + numOptionsDisplayable); i++)
116   {
117     if (i == options.size()) return;
118     if ((ypos + ySeperation) > area.h) break;
119
120     if (i == selectedOption && showseloption)
121     {
122
123       rectangle(0, ypos, area.w, fontHeight, darkseloption ? DrawStyle::SELECTDARKHIGHLIGHT: DrawStyle::SELECTHIGHLIGHT);
124
125       drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::DARKTEXT);
126     }
127     else
128     {
129
130       drawOptionLine(options[i].text, 5, ypos, area.w - 5, DrawStyle::LIGHTTEXT);
131     }
132     ypos += ySeperation;
133   }
134
135 }
136
137 void WSelectList::addColumn(int x)
138 {
139   if (numColumns == 10) return;
140   columns[numColumns++] = x;
141 }
142
143 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const DrawStyle& colour)
144 {
145   if (!numColumns)
146   {
147
148     drawText(text, xpos, ypos, width, colour);
149   }
150   else
151   {
152     char buffer[200];
153     strncpy(buffer, text, 199);
154     int currentColumn = 0;
155     char* pointer;
156
157     pointer = strtok(buffer, "\t");
158     while(pointer)
159     {
160
161       drawText(pointer, xpos + columns[currentColumn], ypos, width - columns[currentColumn], colour);
162
163       currentColumn++;
164       if (currentColumn == 10) return;
165       pointer = strtok(NULL, "\t");
166     }
167   }
168 }
169
170 void WSelectList::up()
171 {
172   if (selectedOption > 0)
173   {
174     selectedOption--;
175   }
176   else
177   {
178     if (!noLoop) selectedOption = options.size() - 1;
179   }
180 }
181
182 void WSelectList::down()
183 {
184   if (selectedOption < options.size() - 1)
185   {
186     selectedOption++;
187   }
188   else
189   {
190     if (!noLoop) selectedOption = 0;
191   }
192 }
193
194 void WSelectList::pageUp()
195 {
196   topOption -= numOptionsDisplayable;
197   if (topOption < 0) topOption = 0;
198
199   selectedOption = topOption;
200 }
201
202 void WSelectList::pageDown()
203 {
204   if ((topOption + numOptionsDisplayable) >= options.size())
205   {
206     selectedOption = options.size() - 1;
207   }
208   else
209   {
210     topOption += numOptionsDisplayable;
211     selectedOption = topOption;
212   }
213 }
214
215 int WSelectList::getTopOption()
216 {
217   return topOption;
218 }
219
220 int WSelectList::getNumOptions()
221 {
222   return options.size();
223 }
224
225 int WSelectList::getBottomOption()
226 {
227   UINT retval = topOption + numOptionsDisplayable;
228   if (retval > options.size()) return options.size();
229   else return retval;
230 }
231
232 int WSelectList::getCurrentOption()
233 {
234   return selectedOption;
235 }
236
237 ULONG WSelectList::getCurrentOptionData()
238 {
239   if (!options.size()) return 0;
240   return options[selectedOption].data;
241 }
242
243 bool WSelectList::mouseAndroidScroll(int x, int y,int sx, int sy)
244 {
245 /*      int fontHeight = getFontHeight();
246         int movelines= sy/fontHeight;
247
248         int seloption=selectedOption+movelines;
249         if (seloption<0) seloption=0;
250         else if (seloption>options.size()-1) seloption=options.size()-1;
251         selectedOption=seloption;*/
252     return false;
253
254 }
255
256 bool WSelectList::mouseMove(int x, int y)
257 {
258   int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
259   if (ml>=0 && ml!=(int)selectedOption)
260   {
261     selectedOption = ml;
262     return true;
263   }
264   return false;
265 }
266
267 bool WSelectList::mouseLBDOWN(int x, int y)
268 {
269   int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
270   if (ml == (int)selectedOption)
271   {
272     /* caller should generate a OK message*/
273     return true;
274   }
275   return false;
276 }
277
278 int WSelectList::getMouseLine(int x,int y)
279 {
280   int fontHeight = getFontHeight();
281   int ySeperation = fontHeight + gap;
282
283   if (y<0) return -1;
284   if (x<0 || x>(int)area.w) return -1;
285   if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
286
287   int cy = y - 5;
288
289   int selected=cy/ySeperation;
290   if (y<5) selected=-1;
291   if (selected> ((int)numOptionsDisplayable)) return -1;
292   /* Important: should be the same algorithm used in draw! */
293   if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
294   if (selectedOption == ((UINT)topOption - 1)) topOption--;
295   // if still not visible...
296   if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
297   {
298     topOption = selectedOption - (numOptionsDisplayable / 2);
299   }
300
301   if (topOption < 0) topOption = 0;
302
303   if ((selected+topOption >= (int) options.size()) ||
304       (selected + topOption < 0)) return -1;
305
306   return selected + topOption;
307 }