2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
21 #include "GUIButtonControl.h"
22 #include "GUIWindowManager.h"
23 #include "GUIDialog.h"
24 #include "GUIFontManager.h"
29 CGUIButtonControl::CGUIButtonControl(int parentID, int controlID, float posX, float posY, float width, float height, const CTextureInfo& textureFocus, const CTextureInfo& textureNoFocus, const CLabelInfo& labelInfo)
30 : CGUIControl(parentID, controlID, posX, posY, width, height)
31 , m_imgFocus(posX, posY, width, height, textureFocus)
32 , m_imgNoFocus(posX, posY, width, height, textureNoFocus)
33 , m_label(posX, posY, width, height, labelInfo)
34 , m_label2(posX, posY, width, height, labelInfo)
39 ControlType = GUICONTROL_BUTTON;
42 CGUIButtonControl::~CGUIButtonControl(void)
46 void CGUIButtonControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
50 m_imgFocus.SetWidth(m_width);
51 m_imgFocus.SetHeight(m_height);
53 m_imgNoFocus.SetWidth(m_width);
54 m_imgNoFocus.SetHeight(m_height);
59 unsigned int alphaChannel = m_alpha;
62 unsigned int alphaCounter = m_focusCounter + 2;
63 if ((alphaCounter % 128) >= 64)
64 alphaChannel = alphaCounter % 64;
66 alphaChannel = 63 - (alphaCounter % 64);
69 alphaChannel = (unsigned int)((float)m_alpha * (float)alphaChannel / 255.0f);
71 if (m_imgFocus.SetAlpha((unsigned char)alphaChannel))
74 m_imgFocus.SetVisible(true);
75 m_imgNoFocus.SetVisible(false);
80 m_imgFocus.SetVisible(false);
81 m_imgNoFocus.SetVisible(true);
84 m_imgFocus.Process(currentTime);
85 m_imgNoFocus.Process(currentTime);
87 ProcessText(currentTime);
88 CGUIControl::Process(currentTime, dirtyregions);
91 void CGUIButtonControl::Render()
94 m_imgNoFocus.Render();
97 CGUIControl::Render();
100 void CGUIButtonControl::RenderText()
106 CGUILabel::COLOR CGUIButtonControl::GetTextColor() const
109 return CGUILabel::COLOR_DISABLED;
111 return CGUILabel::COLOR_FOCUSED;
112 return CGUILabel::COLOR_TEXT;
115 void CGUIButtonControl::ProcessText(unsigned int currentTime)
117 CRect labelRenderRect = m_label.GetRenderRect();
118 CRect label2RenderRect = m_label2.GetRenderRect();
120 bool changed = m_label.SetMaxRect(m_posX, m_posY, m_width, m_height);
121 changed |= m_label.SetText(m_info.GetLabel(m_parentID));
122 changed |= m_label.SetScrolling(HasFocus());
124 // render the second label if it exists
125 CStdString label2(m_info2.GetLabel(m_parentID));
126 changed |= m_label2.SetMaxRect(m_posX, m_posY, m_width, m_height);
127 changed |= m_label2.SetText(label2);
130 changed |= m_label2.SetAlign(XBFONT_RIGHT | (m_label.GetLabelInfo().align & XBFONT_CENTER_Y) | XBFONT_TRUNCATED);
131 changed |= m_label2.SetScrolling(HasFocus());
133 // If overlapping was corrected - compare render rects to determine
134 // if they changed since last frame.
135 if (CGUILabel::CheckAndCorrectOverlap(m_label, m_label2))
136 changed |= (m_label.GetRenderRect() != labelRenderRect ||
137 m_label2.GetRenderRect() != label2RenderRect);
139 changed |= m_label2.SetColor(GetTextColor());
140 changed |= m_label2.Process(currentTime);
142 changed |= m_label.SetColor(GetTextColor());
143 changed |= m_label.Process(currentTime);
148 bool CGUIButtonControl::OnAction(const CAction &action)
150 if (action.GetID() == ACTION_SELECT_ITEM)
155 return CGUIControl::OnAction(action);
158 bool CGUIButtonControl::OnMessage(CGUIMessage& message)
160 if (message.GetControlId() == GetID())
162 if (message.GetMessage() == GUI_MSG_LABEL_SET)
164 SetLabel(message.GetLabel());
167 if (message.GetMessage() == GUI_MSG_LABEL2_SET)
169 SetLabel2(message.GetLabel());
172 if (message.GetMessage() == GUI_MSG_SELECTED)
179 if (message.GetMessage() == GUI_MSG_DESELECTED)
188 return CGUIControl::OnMessage(message);
191 void CGUIButtonControl::AllocResources()
193 CGUIControl::AllocResources();
195 m_imgFocus.AllocResources();
196 m_imgNoFocus.AllocResources();
198 m_width = m_imgFocus.GetWidth();
200 m_height = m_imgFocus.GetHeight();
203 void CGUIButtonControl::FreeResources(bool immediately)
205 CGUIControl::FreeResources(immediately);
206 m_imgFocus.FreeResources(immediately);
207 m_imgNoFocus.FreeResources(immediately);
210 void CGUIButtonControl::DynamicResourceAlloc(bool bOnOff)
212 CGUIControl::DynamicResourceAlloc(bOnOff);
213 m_imgFocus.DynamicResourceAlloc(bOnOff);
214 m_imgNoFocus.DynamicResourceAlloc(bOnOff);
217 void CGUIButtonControl::SetInvalid()
219 CGUIControl::SetInvalid();
220 m_label.SetInvalid();
221 m_label2.SetInvalid();
222 m_imgFocus.SetInvalid();
223 m_imgNoFocus.SetInvalid();
226 void CGUIButtonControl::SetLabel(const string &label)
227 { // NOTE: No fallback for buttons at this point
228 m_info.SetLabel(label, "", GetParentID());
232 void CGUIButtonControl::SetLabel2(const string &label2)
233 { // NOTE: No fallback for buttons at this point
234 m_info2.SetLabel(label2, "", GetParentID());
238 void CGUIButtonControl::SetPosition(float posX, float posY)
240 CGUIControl::SetPosition(posX, posY);
241 m_imgFocus.SetPosition(posX, posY);
242 m_imgNoFocus.SetPosition(posX, posY);
245 void CGUIButtonControl::SetAlpha(unsigned char alpha)
247 if (m_alpha != alpha)
252 bool CGUIButtonControl::UpdateColors()
254 bool changed = CGUIControl::UpdateColors();
255 changed |= m_label.UpdateColors();
256 changed |= m_imgFocus.SetDiffuseColor(m_diffuseColor);
257 changed |= m_imgNoFocus.SetDiffuseColor(m_diffuseColor);
262 CRect CGUIButtonControl::CalcRenderRegion() const
264 CRect buttonRect = CGUIControl::CalcRenderRegion();
265 CRect textRect = m_label.GetRenderRect();
266 buttonRect.Union(textRect);
270 EVENT_RESULT CGUIButtonControl::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
272 if (event.m_id == ACTION_MOUSE_LEFT_CLICK)
274 OnAction(CAction(ACTION_SELECT_ITEM));
275 return EVENT_RESULT_HANDLED;
277 return EVENT_RESULT_UNHANDLED;
280 CStdString CGUIButtonControl::GetDescription() const
282 CStdString strLabel(m_info.GetLabel(m_parentID));
286 CStdString CGUIButtonControl::GetLabel2() const
288 CStdString strLabel(m_info2.GetLabel(m_parentID));
292 void CGUIButtonControl::PythonSetLabel(const CStdString &strFont, const string &strText, color_t textColor, color_t shadowColor, color_t focusedColor)
294 m_label.GetLabelInfo().font = g_fontManager.GetFont(strFont);
295 m_label.GetLabelInfo().textColor = textColor;
296 m_label.GetLabelInfo().focusedColor = focusedColor;
297 m_label.GetLabelInfo().shadowColor = shadowColor;
301 void CGUIButtonControl::PythonSetDisabledColor(color_t disabledColor)
303 m_label.GetLabelInfo().disabledColor = disabledColor;
306 void CGUIButtonControl::OnClick()
308 // Save values, as the click message may deactivate the window
309 int controlID = GetID();
310 int parentID = GetParentID();
311 CGUIAction clickActions = m_clickActions;
313 // button selected, send a message
314 CGUIMessage msg(GUI_MSG_CLICKED, controlID, parentID, 0);
315 SendWindowMessage(msg);
317 clickActions.ExecuteActions(controlID, parentID);
320 void CGUIButtonControl::OnFocus()
322 m_focusActions.ExecuteActions(GetID(), GetParentID());
325 void CGUIButtonControl::OnUnFocus()
327 m_unfocusActions.ExecuteActions(GetID(), GetParentID());
330 void CGUIButtonControl::SetSelected(bool bSelected)
332 if (m_bSelected != bSelected)
334 m_bSelected = bSelected;