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