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