]> git.vomp.tv Git - vompclient.git/blob - wtabbar.cc
More compiler warning fixes
[vompclient.git] / wtabbar.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 "wtabbar.h"
22 #include "surface.h"
23 #include "remote.h"
24 #include "wbutton.h"
25 #include "colour.h"
26
27 WTabBar::WTabBar()
28 {
29   buttonBarActive = true;
30   visiblePane = 0;
31   setBackgroundColour(DrawStyle::TABVIEWBACKGROUND);
32   
33   symbolLeft.setPosition(0, 4);
34   symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
35   symbolLeft.nextSymbol = WSymbol::LEFTARROW;
36   add(&symbolLeft);
37
38   symbolRight.setPosition(0, 4); // will be reset in addtab
39   symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
40   symbolRight.nextSymbol = WSymbol::RIGHTARROW;
41   add(&symbolRight);
42 }
43
44 WTabBar::~WTabBar()
45 {
46   for (UINT i = 0; i < tabs.size(); i++)
47   {
48     delete tabs[i].button;
49     delete tabs[i].pane; //moved from vopts MR
50   }
51 }
52
53 void WTabBar::addTab(const char* name, Boxx* boxx)
54 {
55   TabDetails td;
56   td.name = name;
57   td.nameWidth = 0;
58   
59   for(UINT i = 0; i < strlen(name); i++)
60   {
61     td.nameWidth += charWidth(name[i]);
62   }
63
64   UINT newButtonX = 22;
65   for(UINT i = 0; i < tabs.size(); i++) newButtonX += tabs[i].button->getWidth() + 10;
66
67   WButton* newButton = new WButton();
68   newButton->setText(name);
69   newButton->setPosition(newButtonX, 2);
70   newButton->setSize(td.nameWidth + 6, getFontHeight());
71   if ((newButtonX + newButton->getWidth()) > (getWidth() - 22)) newButton->setVisible(false);
72   add(newButton);
73     
74   boxx->setPosition(0, 34);
75   boxx->setSize(getWidth(), getHeight()-34);
76   add(boxx);
77
78   td.button = newButton;
79   td.pane = boxx;
80   tabs.push_back(td);
81     
82   if (tabs.size() == 1)
83   {
84     newButton->setActive(true);
85     boxx->setVisible(true);
86   }
87   else
88   {
89     newButton->setActive(false);
90     boxx->setVisible(false);
91     symbolRight.nextColour = DrawStyle::SELECTHIGHLIGHT;  // if this is the 2nd+ tab, ensure right arrow goes active
92   }
93
94   // WTabbar doesn't know its width during the constructor
95   // So this is moved to here for now
96   symbolRight.setPosition(getWidth() - 28, 4);
97 }
98
99 int WTabBar::handleCommand(int command)
100 {
101   // This returns 0 for not handled, and 1 for handled and please-update-me, 2 for handled-no-update
102
103   // if the focus is in a pane, send the command there
104   if (!buttonBarActive)
105   {
106     // Send the command to the pane
107     // Return code should be:
108       // 0 - not handled
109       // 1 - handled - stop command here
110       // 4 - handled - pane is returning control to here
111           // 5 - activate button bar and let button process the call, this for simple up and down scroll views
112       // FIXME standardise these
113       
114     int paneRetCode = tabs[visiblePane].pane->handleCommand(command);
115     if (paneRetCode == 0)
116     {
117       return 0;
118     }
119     else if (paneRetCode == 1)
120     {
121       draw();
122       return 1;
123     }
124     else if (paneRetCode == 4)
125     {
126       buttonBarActive = true;
127       tabs[visiblePane].button->setActive(true);      
128       if (visiblePane != 0) symbolLeft.nextColour = DrawStyle::SELECTHIGHLIGHT;
129       if (visiblePane != tabs.size() - 1) symbolRight.nextColour = DrawStyle::SELECTHIGHLIGHT;
130       draw();
131       return 1;
132     } else if (paneRetCode == 5)
133     {
134         buttonBarActive = true;
135         tabs[visiblePane].button->setActive(true);
136         if (visiblePane != 0) symbolLeft.nextColour = DrawStyle::SELECTHIGHLIGHT;
137         if (visiblePane != tabs.size() - 1) symbolRight.nextColour = DrawStyle::SELECTHIGHLIGHT;
138         draw();
139         // no return!
140     }
141   }
142
143   if (buttonBarActive)
144   {
145     switch(command)
146     {
147       case Remote::DF_LEFT:
148       case Remote::LEFT:
149       {
150         if (!left()) return 2;
151         draw();
152         return 1;
153       }
154       case Remote::DF_RIGHT:
155       case Remote::RIGHT:
156       {
157         if (!right()) return 2;
158         draw();
159         return 1;
160       }
161       case Remote::UP:
162       case Remote::DF_UP:
163       case Remote::DOWN:
164       case Remote::DF_DOWN:
165       {
166         buttonBarActive = false;
167         tabs[visiblePane].button->dim();
168         symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
169         symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
170         if (tabs[visiblePane].pane->handleCommand(command) == 1) // shouldn't return anything else at this point?!
171         {
172           draw();
173           return 1;
174         }
175       }    
176     }  
177   }
178   return 0;  
179 }  
180
181 // When there are too many tabs to display simultaneously, extend this to shift the tabs left and right
182
183 bool WTabBar::left()
184 {
185   if (visiblePane == 0) return false;
186   tabs[visiblePane].button->setActive(false);
187   tabs[visiblePane].pane->setVisible(false);
188   --visiblePane;
189   tabs[visiblePane].button->setActive(true);
190   tabs[visiblePane].pane->setVisible(true);
191
192   if (visiblePane == 0) symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
193   symbolRight.nextColour = DrawStyle::SELECTHIGHLIGHT;
194
195   // Move the buttons..
196   if (tabs[visiblePane].button->getVisible() == false)
197   {
198     tabs[visiblePane].button->setVisible(true);
199     // We have gone << past the last visible button
200     UINT newButtonX = 22;
201     UINT j;
202     for(j = visiblePane; j < tabs.size(); j++) // start with the button just uncovered
203     {
204       if ((newButtonX + tabs[j].button->getWidth()) > (getWidth() - 22)) break; // this one too big.
205       tabs[j].button->setPosition(newButtonX, 2);
206       newButtonX += tabs[j].button->getWidth() + 10;
207     }
208     for(; j < tabs.size(); j++) // hide the rest
209     {
210       tabs[j].button->setVisible(false);
211     }
212   }
213
214   return true;
215 }
216
217 bool WTabBar::right()
218 {
219   if (visiblePane == tabs.size() - 1) return false;
220   tabs[visiblePane].button->setActive(false);
221   tabs[visiblePane].pane->setVisible(false);
222   ++visiblePane;
223   tabs[visiblePane].button->setActive(true);
224   tabs[visiblePane].pane->setVisible(true);
225   
226   if (visiblePane == tabs.size() - 1) symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
227   symbolLeft.nextColour = DrawStyle::SELECTHIGHLIGHT;
228
229   // Move the buttons..
230   if (tabs[visiblePane].button->getVisible() == false)
231   {
232     tabs[visiblePane].button->setVisible(true);
233     // We have gone >> past the last visible button
234     // Move all buttons left, but need to work out which one to finally display as first on the left
235     UINT displayWidth = getWidth() - 44;
236     UINT widthSum = tabs[visiblePane].button->getWidth(); // start with width of newly shown button (on the right)
237     int startWith = 0;
238     int i;
239     for(i = visiblePane - 1; i >= 0; i--)
240     {
241       widthSum += tabs[i].button->getWidth() + 10; // add on width needed to display also the previous button
242       if (widthSum > displayWidth) // then it's too much
243       {
244         startWith = i + 1;
245         break;
246       }
247     }
248     
249     for(i = 0; i < startWith; i++) // For each button to the left of startWith...
250     {
251       tabs[i].button->setVisible(false);
252     }
253     
254     UINT newButtonX = 22;
255     for(UINT j = startWith; j <= visiblePane; j++) // startWith -> the one we just uncovered
256     {
257       tabs[j].button->setPosition(newButtonX, 2);
258       newButtonX += tabs[j].button->getWidth() + 10;
259     }      
260   }
261   
262   return true;
263 }
264
265 bool WTabBar::mouseMove(int x, int y) {
266     if (tabs[visiblePane].pane->mouseMove(x,y)) {
267         if (buttonBarActive){
268             buttonBarActive = false;
269             tabs[visiblePane].button->dim();
270             symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
271             symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
272             
273         }
274         draw();
275         return true;
276     }
277     return false;
278 }
279
280 bool WTabBar::mouseAndroidScroll(int x, int y, int sx, int sy) {
281         if (tabs[visiblePane].pane->mouseAndroidScroll(x, y,sx,sy)) {
282                 draw();
283                 return true;
284         }
285         return false;
286 }
287
288 bool WTabBar::mouseLBDOWN(int x, int y) {
289     UINT i;
290     for (i=0;i<tabs.size();i++) {
291         if (tabs[i].button->getVisible()) {
292             if (tabs[i].button->mouseMove(x,y)) {
293                 tabs[visiblePane].button->setActive(false);
294                 tabs[visiblePane].pane->setVisible(false);
295                 tabs[visiblePane].pane->deactivateAllControls();
296                 visiblePane=i;
297                 tabs[visiblePane].button->setActive(true);
298                 tabs[visiblePane].pane->setVisible(true);
299                 tabs[visiblePane].pane->deactivateAllControls();
300                 buttonBarActive = true;
301                 draw();
302                 return true;
303             }
304         }
305     }
306     if (tabs[visiblePane].pane->mouseLBDOWN(x,y)) {
307         buttonBarActive = false;
308         tabs[visiblePane].button->dim();
309         symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
310         symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
311         draw();
312         return true;
313     }
314     if (symbolLeft.mouseLBDOWN(x,y)) {
315         left();
316         draw();
317         return true;
318     }
319     if (symbolRight.mouseLBDOWN(x,y)) {
320         right();
321         draw();
322         return true;
323     }
324     return false;
325 }
326
327 void WTabBar::activateFocus(bool active)
328 {
329         if (active)
330         {
331                 tabs[visiblePane].button->setActive(true);
332                 buttonBarActive = true;
333
334         } else {
335                 tabs[visiblePane].button->setActive(false);
336                 buttonBarActive = false;
337
338         }
339 }
340