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