[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / guilib / GUIToggleButtonControl.cpp
1 /*
2  *      Copyright (C) 2005-2013 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, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "GUIToggleButtonControl.h"
22 #include "GUIWindowManager.h"
23 #include "GUIDialog.h"
24 #include "utils/CharsetConverter.h"
25 #include "GUIInfoManager.h"
26
27 using namespace std;
28
29 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)
30     : CGUIButtonControl(parentID, controlID, posX, posY, width, height, textureFocus, textureNoFocus, labelInfo)
31     , m_selectButton(parentID, controlID, posX, posY, width, height, altTextureFocus, altTextureNoFocus, labelInfo)
32 {
33   m_toggleSelect = 0;
34   ControlType = GUICONTROL_TOGGLEBUTTON;
35 }
36
37 CGUIToggleButtonControl::~CGUIToggleButtonControl(void)
38 {
39 }
40
41 void CGUIToggleButtonControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
42 {
43   // ask our infoManager whether we are selected or not...
44   bool selected = m_bSelected;
45   if (m_toggleSelect)
46     selected = g_infoManager.GetBoolValue(m_toggleSelect);
47   if (selected != m_bSelected)
48   {
49     MarkDirtyRegion();
50     m_bSelected = selected;
51   }
52
53   if (m_bSelected)
54   {
55     // render our Alternate textures...
56     m_selectButton.SetFocus(HasFocus());
57     m_selectButton.SetVisible(IsVisible());
58     m_selectButton.SetEnabled(!IsDisabled());
59     m_selectButton.SetPulseOnSelect(m_pulseOnSelect);
60     m_selectButton.DoProcess(currentTime, dirtyregions);
61   }
62   CGUIButtonControl::Process(currentTime, dirtyregions);
63 }
64
65 void CGUIToggleButtonControl::Render()
66 {
67   if (m_bSelected)
68   {
69     m_selectButton.Render();
70     CGUIControl::Render();
71   }
72   else
73   { // render our Normal textures...
74     CGUIButtonControl::Render();
75   }
76 }
77
78 bool CGUIToggleButtonControl::OnAction(const CAction &action)
79 {
80   if (action.GetID() == ACTION_SELECT_ITEM)
81   {
82     m_bSelected = !m_bSelected;
83     SetInvalid();
84   }
85   return CGUIButtonControl::OnAction(action);
86 }
87
88 void CGUIToggleButtonControl::AllocResources()
89 {
90   CGUIButtonControl::AllocResources();
91   m_selectButton.AllocResources();
92 }
93
94 void CGUIToggleButtonControl::FreeResources(bool immediately)
95 {
96   CGUIButtonControl::FreeResources(immediately);
97   m_selectButton.FreeResources(immediately);
98 }
99
100 void CGUIToggleButtonControl::DynamicResourceAlloc(bool bOnOff)
101 {
102   CGUIButtonControl::DynamicResourceAlloc(bOnOff);
103   m_selectButton.DynamicResourceAlloc(bOnOff);
104 }
105
106 void CGUIToggleButtonControl::SetInvalid()
107 {
108   CGUIButtonControl::SetInvalid();
109   m_selectButton.SetInvalid();
110 }
111
112 void CGUIToggleButtonControl::SetPosition(float posX, float posY)
113 {
114   CGUIButtonControl::SetPosition(posX, posY);
115   m_selectButton.SetPosition(posX, posY);
116 }
117
118 void CGUIToggleButtonControl::SetWidth(float width)
119 {
120   CGUIButtonControl::SetWidth(width);
121   m_selectButton.SetWidth(width);
122 }
123
124 void CGUIToggleButtonControl::SetHeight(float height)
125 {
126   CGUIButtonControl::SetHeight(height);
127   m_selectButton.SetHeight(height);
128 }
129
130 bool CGUIToggleButtonControl::UpdateColors()
131 {
132   bool changed = CGUIButtonControl::UpdateColors();
133   changed |= m_selectButton.SetColorDiffuse(m_diffuseColor);
134   changed |= m_selectButton.UpdateColors();
135
136   return changed;
137 }
138
139 void CGUIToggleButtonControl::SetLabel(const string &strLabel)
140 {
141   CGUIButtonControl::SetLabel(strLabel);
142   m_selectButton.SetLabel(strLabel);
143 }
144
145 void CGUIToggleButtonControl::SetAltLabel(const string &label)
146 {
147   if (label.size())
148     m_selectButton.SetLabel(label);
149 }
150
151 CStdString CGUIToggleButtonControl::GetDescription() const
152 {
153   if (m_bSelected)
154     return m_selectButton.GetDescription();
155   return CGUIButtonControl::GetDescription();
156 }
157
158 void CGUIToggleButtonControl::SetAltClickActions(const CGUIAction &clickActions)
159 {
160   m_selectButton.SetClickActions(clickActions);
161 }
162
163 void CGUIToggleButtonControl::OnClick()
164 {
165   // the ! is here as m_bSelected gets updated before this is called
166   if (!m_bSelected && m_selectButton.HasClickActions())
167     m_selectButton.OnClick();
168   else
169     CGUIButtonControl::OnClick();
170 }
171
172 void CGUIToggleButtonControl::SetToggleSelect(const CStdString &toggleSelect)
173 {
174   m_toggleSelect = g_infoManager.Register(toggleSelect, GetParentID());
175 }