33fd1c5d661bd60d7055f49bcfa5c86887d0a748
[vuplus_xbmc] / guilib / GUIToggleButtonControl.cpp
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "GUIToggleButtonControl.h"
23 #include "GUIWindowManager.h"
24 #include "GUIDialog.h"
25 #include "utils/CharsetConverter.h"
26 #include "utils/GUIInfoManager.h"
27
28 using namespace std;
29
30 CGUIToggleButtonControl::CGUIToggleButtonControl(int parentID, int controlID, float posX, float posY, float width, float height, const CTextureInfo& textureFocus, const CTextureInfo& textureNoFocus, const CTextureInfo& altTextureFocus, const CTextureInfo& altTextureNoFocus, const CLabelInfo &labelInfo)
31     : CGUIButtonControl(parentID, controlID, posX, posY, width, height, textureFocus, textureNoFocus, labelInfo)
32     , m_selectButton(parentID, controlID, posX, posY, width, height, altTextureFocus, altTextureNoFocus, labelInfo)
33 {
34   m_toggleSelect = 0;
35   ControlType = GUICONTROL_TOGGLEBUTTON;
36 }
37
38 CGUIToggleButtonControl::~CGUIToggleButtonControl(void)
39 {
40 }
41
42 void CGUIToggleButtonControl::Render()
43 {
44   // ask our infoManager whether we are selected or not...
45   if (m_toggleSelect)
46     m_bSelected = g_infoManager.GetBool(m_toggleSelect, m_parentID);
47
48   if (m_bSelected)
49   {
50     // render our Alternate textures...
51     m_selectButton.SetFocus(HasFocus());
52     m_selectButton.SetVisible(IsVisible());
53     m_selectButton.SetEnabled(!IsDisabled());
54     m_selectButton.SetPulseOnSelect(m_pulseOnSelect);
55     m_selectButton.Render();
56     CGUIControl::Render();
57   }
58   else
59   { // render our Normal textures...
60     CGUIButtonControl::Render();
61   }
62 }
63
64 bool CGUIToggleButtonControl::OnAction(const CAction &action)
65 {
66   if (action.GetID() == ACTION_SELECT_ITEM)
67   {
68     m_bSelected = !m_bSelected;
69   }
70   return CGUIButtonControl::OnAction(action);
71 }
72
73 void CGUIToggleButtonControl::AllocResources()
74 {
75   CGUIButtonControl::AllocResources();
76   m_selectButton.AllocResources();
77 }
78
79 void CGUIToggleButtonControl::FreeResources(bool immediately)
80 {
81   CGUIButtonControl::FreeResources(immediately);
82   m_selectButton.FreeResources(immediately);
83 }
84
85 void CGUIToggleButtonControl::DynamicResourceAlloc(bool bOnOff)
86 {
87   CGUIButtonControl::DynamicResourceAlloc(bOnOff);
88   m_selectButton.DynamicResourceAlloc(bOnOff);
89 }
90
91 void CGUIToggleButtonControl::SetInvalid()
92 {
93   CGUIButtonControl::SetInvalid();
94   m_selectButton.SetInvalid();
95 }
96
97 void CGUIToggleButtonControl::SetPosition(float posX, float posY)
98 {
99   CGUIButtonControl::SetPosition(posX, posY);
100   m_selectButton.SetPosition(posX, posY);
101 }
102
103 void CGUIToggleButtonControl::SetWidth(float width)
104 {
105   CGUIButtonControl::SetWidth(width);
106   m_selectButton.SetWidth(width);
107 }
108
109 void CGUIToggleButtonControl::SetHeight(float height)
110 {
111   CGUIButtonControl::SetHeight(height);
112   m_selectButton.SetHeight(height);
113 }
114
115 void CGUIToggleButtonControl::UpdateColors()
116 {
117   CGUIButtonControl::UpdateColors();
118   m_selectButton.UpdateColors();
119 }
120
121 void CGUIToggleButtonControl::SetLabel(const string &strLabel)
122 {
123   CGUIButtonControl::SetLabel(strLabel);
124   m_selectButton.SetLabel(strLabel);
125 }
126
127 void CGUIToggleButtonControl::SetAltLabel(const string &label)
128 {
129   if (label.size())
130     m_selectButton.SetLabel(label);
131 }
132
133 CStdString CGUIToggleButtonControl::GetDescription() const
134 {
135   if (m_bSelected)
136     return m_selectButton.GetDescription();
137   return CGUIButtonControl::GetDescription();
138 }
139
140 void CGUIToggleButtonControl::SetAltClickActions(const vector<CGUIActionDescriptor> &clickActions)
141 {
142   m_selectButton.SetClickActions(clickActions);
143 }
144
145 void CGUIToggleButtonControl::OnClick()
146 {
147   // the ! is here as m_bSelected gets updated before this is called
148   if (!m_bSelected && m_selectButton.HasClickActions())
149     m_selectButton.OnClick();
150   else
151     CGUIButtonControl::OnClick();
152 }