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