]> git.vomp.tv Git - vompclient-marten.git/blob - wtabbar.cc
Fix bug in demuxer widescreen signaling
[vompclient-marten.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::VIEWBACKGROUND);
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       // FIXME standardise these
112       
113     int paneRetCode = tabs[visiblePane].pane->handleCommand(command);
114     if (paneRetCode == 0)
115     {
116       return 0;
117     }
118     else if (paneRetCode == 1)
119     {
120       draw();
121       return 1;
122     }
123     else if (paneRetCode == 4)
124     {
125       buttonBarActive = true;
126       tabs[visiblePane].button->setActive(true);      
127       if (visiblePane != 0) symbolLeft.nextColour = DrawStyle::SELECTHIGHLIGHT;
128       if (visiblePane != tabs.size() - 1) symbolRight.nextColour = DrawStyle::SELECTHIGHLIGHT;
129       draw();
130       return 1;
131     }
132   }
133
134   if (buttonBarActive)
135   {
136     switch(command)
137     {
138       case Remote::DF_LEFT:
139       case Remote::LEFT:
140       {
141         if (!left()) return 2;
142         draw();
143         return 1;
144       }
145       case Remote::DF_RIGHT:
146       case Remote::RIGHT:
147       {
148         if (!right()) return 2;
149         draw();
150         return 1;
151       }
152       case Remote::DOWN:
153       case Remote::DF_DOWN:
154       {
155         buttonBarActive = false;
156         tabs[visiblePane].button->dim();
157         symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
158         symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
159         if (tabs[visiblePane].pane->handleCommand(command) == 1) // shouldn't return anything else at this point?!
160         {
161           draw();
162           return 1;
163         }
164       }    
165     }  
166   }
167   return 0;  
168 }  
169
170 // When there are too many tabs to display simultaneously, extend this to shift the tabs left and right
171
172 bool WTabBar::left()
173 {
174   if (visiblePane == 0) return false;
175   tabs[visiblePane].button->setActive(false);
176   tabs[visiblePane].pane->setVisible(false);
177   --visiblePane;
178   tabs[visiblePane].button->setActive(true);
179   tabs[visiblePane].pane->setVisible(true);
180
181   if (visiblePane == 0) symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
182   symbolRight.nextColour = DrawStyle::SELECTHIGHLIGHT;
183
184   // Move the buttons..
185   if (tabs[visiblePane].button->getVisible() == false)
186   {
187     tabs[visiblePane].button->setVisible(true);
188     // We have gone << past the last visible button
189     UINT newButtonX = 22;
190     UINT j;
191     for(j = visiblePane; j < tabs.size(); j++) // start with the button just uncovered
192     {
193       if ((newButtonX + tabs[j].button->getWidth()) > (getWidth() - 22)) break; // this one too big.
194       tabs[j].button->setPosition(newButtonX, 2);
195       newButtonX += tabs[j].button->getWidth() + 10;
196     }
197     for(; j < tabs.size(); j++) // hide the rest
198     {
199       tabs[j].button->setVisible(false);
200     }
201   }
202
203   return true;
204 }
205
206 bool WTabBar::right()
207 {
208   if (visiblePane == tabs.size() - 1) return false;
209   tabs[visiblePane].button->setActive(false);
210   tabs[visiblePane].pane->setVisible(false);
211   ++visiblePane;
212   tabs[visiblePane].button->setActive(true);
213   tabs[visiblePane].pane->setVisible(true);
214   
215   if (visiblePane == tabs.size() - 1) symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
216   symbolLeft.nextColour = DrawStyle::SELECTHIGHLIGHT;
217
218   // Move the buttons..
219   if (tabs[visiblePane].button->getVisible() == false)
220   {
221     tabs[visiblePane].button->setVisible(true);
222     // We have gone >> past the last visible button
223     // Move all buttons left, but need to work out which one to finally display as first on the left
224     UINT displayWidth = getWidth() - 44;
225     UINT widthSum = tabs[visiblePane].button->getWidth(); // start with width of newly shown button (on the right)
226     int startWith = 0;
227     int i;
228     for(i = visiblePane - 1; i >= 0; i--)
229     {
230       widthSum += tabs[i].button->getWidth() + 10; // add on width needed to display also the previous button
231       if (widthSum > displayWidth) // then it's too much
232       {
233         startWith = i + 1;
234         break;
235       }
236     }
237     
238     for(i = 0; i < startWith; i++) // For each button to the left of startWith...
239     {
240       tabs[i].button->setVisible(false);
241     }
242     
243     UINT newButtonX = 22;
244     for(UINT j = startWith; j <= visiblePane; j++) // startWith -> the one we just uncovered
245     {
246       tabs[j].button->setPosition(newButtonX, 2);
247       newButtonX += tabs[j].button->getWidth() + 10;
248     }      
249   }
250   
251   return true;
252 }
253
254 bool WTabBar::mouseMove(int x, int y) {
255     if (tabs[visiblePane].pane->mouseMove(x,y)) {
256         if (buttonBarActive){
257             buttonBarActive = false;
258             tabs[visiblePane].button->dim();
259             symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
260             symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
261             
262         }
263         draw();
264         return true;
265     }
266     return false;
267 }
268
269 bool WTabBar::mouseLBDOWN(int x, int y) {
270     UINT i;
271     for (i=0;i<tabs.size();i++) {
272         if (tabs[i].button->getVisible()) {
273             if (tabs[i].button->mouseMove(x,y)) {
274                 tabs[visiblePane].button->setActive(false);
275                 tabs[visiblePane].pane->setVisible(false);
276                 tabs[visiblePane].pane->deactivateAllControls();
277                 visiblePane=i;
278                 tabs[visiblePane].button->setActive(true);
279                 tabs[visiblePane].pane->setVisible(true);
280                 tabs[visiblePane].pane->deactivateAllControls();
281                 buttonBarActive = true;
282                 draw();
283                 return true;
284             }
285         }
286     }
287     if (tabs[visiblePane].pane->mouseLBDOWN(x,y)) {
288         buttonBarActive = false;
289         tabs[visiblePane].button->dim();
290         symbolLeft.nextColour = DrawStyle::BUTTONBACKGROUND;
291         symbolRight.nextColour = DrawStyle::BUTTONBACKGROUND;
292         draw();
293         return true;
294     }
295     if (symbolLeft.mouseLBDOWN(x,y)) {
296         left();
297         draw();
298         return true;
299     }
300     if (symbolRight.mouseLBDOWN(x,y)) {
301         right();
302         draw();
303         return true;
304     }
305     return false;
306 }
307