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