]> git.vomp.tv Git - vompclient.git/blob - wselectlist.cc
Extended recordings menu, including artwork from tvscraper
[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 void WSelectList::drawOptionLine(char* text, int xpos, int ypos, int width, const DrawStyle& colour, TVMediaInfo* pict)
149 {
150   UINT curline = 0;
151   UINT taboffset = 0;
152   int fontHeight = getFontHeight();
153   float ypos_mod= ypos + (linesPerOption-floor(linesPerOption))*((float)fontHeight)*0.5f;
154
155   if (pict)
156   {
157           drawTVMedia(*pict, xpos,ypos,0,fontHeight*linesPerOption,TopLeft);
158           taboffset++;
159   }
160
161   if (!numColumns && linesPerOption == 1)
162   {
163
164     drawText(text, xpos, ypos, width, colour);
165   }
166   else
167   {
168     char buffer[200];
169     strncpy(buffer, text, 199);
170     int currentColumn = taboffset;
171     char* pointer;
172
173     pointer = strtok(buffer, "\t\n");
174     while(pointer)
175     {
176       drawText(pointer, xpos + columns[currentColumn], ypos_mod + curline * fontHeight, width - columns[currentColumn], colour);
177
178       pointer = strtok(NULL, "\t\n");
179       if (pointer) {
180           char delimiter = text[pointer - buffer-1];
181           if (delimiter == '\t') currentColumn++;
182           else if (delimiter == '\n' ){
183                   currentColumn = taboffset;
184                   curline++;
185           }
186       }
187       if (curline >= linesPerOption) return;
188
189       if (currentColumn == 10) return;
190     }
191   }
192 }
193
194 void WSelectList::up()
195 {
196   if (selectedOption > 0)
197   {
198     selectedOption--;
199   }
200   else
201   {
202     if (!noLoop) selectedOption = options.size() - 1;
203   }
204 }
205
206 void WSelectList::down()
207 {
208   if (selectedOption < options.size() - 1)
209   {
210     selectedOption++;
211   }
212   else
213   {
214     if (!noLoop) selectedOption = 0;
215   }
216 }
217
218 void WSelectList::pageUp()
219 {
220   topOption -= numOptionsDisplayable;
221   if (topOption < 0) topOption = 0;
222
223   selectedOption = topOption;
224 }
225
226 void WSelectList::pageDown()
227 {
228   if ((topOption + numOptionsDisplayable) >= options.size())
229   {
230     selectedOption = options.size() - 1;
231   }
232   else
233   {
234     topOption += numOptionsDisplayable;
235     selectedOption = topOption;
236   }
237 }
238
239 int WSelectList::getTopOption()
240 {
241   return topOption;
242 }
243
244 int WSelectList::getNumOptions()
245 {
246   return options.size();
247 }
248
249 int WSelectList::getBottomOption()
250 {
251   UINT retval = topOption + numOptionsDisplayable;
252   if (retval > options.size()) return options.size();
253   else return retval;
254 }
255
256 int WSelectList::getCurrentOption()
257 {
258   return selectedOption;
259 }
260
261 ULONG WSelectList::getCurrentOptionData()
262 {
263   if (!options.size()) return 0;
264   return options[selectedOption].data;
265 }
266
267 bool WSelectList::mouseAndroidScroll(int x, int y,int sx, int sy)
268 {
269 /*      int fontHeight = getFontHeight();
270         int movelines= sy/fontHeight;
271
272         int seloption=selectedOption+movelines;
273         if (seloption<0) seloption=0;
274         else if (seloption>options.size()-1) seloption=options.size()-1;
275         selectedOption=seloption;*/
276     return false;
277
278 }
279
280 bool WSelectList::mouseMove(int x, int y)
281 {
282   int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
283   if (ml>=0 && ml!=(int)selectedOption)
284   {
285     selectedOption = ml;
286     return true;
287   }
288   return false;
289 }
290
291 bool WSelectList::mouseLBDOWN(int x, int y)
292 {
293   int ml = getMouseLine(x-getRootBoxOffsetX(), y-getRootBoxOffsetY());
294   if (ml == (int)selectedOption)
295   {
296     /* caller should generate a OK message*/
297     return true;
298   }
299   return false;
300 }
301
302 int WSelectList::getMouseLine(int x,int y)
303 {
304   int fontHeight = getFontHeight();
305   int ySeperation = fontHeight + gap;
306
307   if (y<0) return -1;
308   if (x<0 || x>(int)area.w) return -1;
309   if (y>(int)(10+numOptionsDisplayable*ySeperation)) return -1;
310
311   int cy = y - 5;
312
313   int selected=cy/ySeperation;
314   if (y<5) selected=-1;
315   if (selected> ((int)numOptionsDisplayable)) return -1;
316   /* Important: should be the same algorithm used in draw! */
317   if (selectedOption == (topOption + numOptionsDisplayable)) topOption++;
318   if (selectedOption == ((UINT)topOption - 1)) topOption--;
319   // if still not visible...
320   if ((selectedOption < (UINT)topOption) || (selectedOption > (topOption + numOptionsDisplayable)))
321   {
322     topOption = selectedOption - (numOptionsDisplayable / 2);
323   }
324
325   if (topOption < 0) topOption = 0;
326
327   if ((selected+topOption >= (int) options.size()) ||
328       (selected + topOption < 0)) return -1;
329
330   return selected + topOption;
331 }