[guilib] fix labelcontrols with auto width always being marked as dirty if they speci...
[vuplus_xbmc] / xbmc / guilib / GUICheckMarkControl.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 "GUICheckMarkControl.h"
22 #include "GUIFontManager.h"
23 #include "Key.h"
24
25 using namespace std;
26
27 CGUICheckMarkControl::CGUICheckMarkControl(int parentID, int controlID, float posX, float posY, float width, float height, const CTextureInfo& textureCheckMark, const CTextureInfo& textureCheckMarkNF, float checkWidth, float checkHeight, const CLabelInfo &labelInfo)
28     : CGUIControl(parentID, controlID, posX, posY, width, height)
29     , m_imgCheckMark(posX, posY, checkWidth, checkHeight, textureCheckMark)
30     , m_imgCheckMarkNoFocus(posX, posY, checkWidth, checkHeight, textureCheckMarkNF)
31     , m_label(posX, posY, width, height, labelInfo)
32     , m_strLabel("")
33 {
34   m_bSelected = false;
35   m_label.GetLabelInfo().align |= XBFONT_CENTER_Y;
36   ControlType = GUICONTROL_CHECKMARK;
37 }
38
39 CGUICheckMarkControl::~CGUICheckMarkControl(void)
40 {}
41
42 void CGUICheckMarkControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
43 {
44   bool changed = false;
45
46   changed |= m_imgCheckMark.Process(currentTime);
47   changed |= m_imgCheckMarkNoFocus.Process(currentTime);
48   changed |= m_label.Process(currentTime);
49
50   if (changed)
51     MarkDirtyRegion();
52
53   CGUIControl::Process(currentTime, dirtyregions);
54 }
55
56 void CGUICheckMarkControl::Render()
57 {
58   m_label.SetText(m_strLabel);
59
60   float textWidth = m_label.GetTextWidth();
61   m_width = textWidth + 5 + m_imgCheckMark.GetWidth();
62   m_height = m_imgCheckMark.GetHeight();
63
64   float textPosX = m_posX;
65   float checkMarkPosX = m_posX;
66
67   if (m_label.GetLabelInfo().align & (XBFONT_RIGHT | XBFONT_CENTER_X))
68     textPosX += m_imgCheckMark.GetWidth() + 5;
69   else
70     checkMarkPosX += textWidth + 5;
71
72   m_label.SetMaxRect(textPosX, m_posY, textWidth, m_height);
73   m_label.SetColor(GetTextColor());
74   m_label.Render();
75
76   if (m_bSelected)
77   {
78     m_imgCheckMark.SetPosition(checkMarkPosX, m_posY);
79     m_imgCheckMark.Render();
80   }
81   else
82   {
83     m_imgCheckMarkNoFocus.SetPosition(checkMarkPosX, m_posY);
84     m_imgCheckMarkNoFocus.Render();
85   }
86   CGUIControl::Render();
87 }
88
89 CGUILabel::COLOR CGUICheckMarkControl::GetTextColor() const
90 {
91   if (IsDisabled())
92     return CGUILabel::COLOR_DISABLED;
93   else if (HasFocus())
94     return CGUILabel::COLOR_FOCUSED;
95   return CGUILabel::COLOR_TEXT;
96 }
97
98 bool CGUICheckMarkControl::OnAction(const CAction &action)
99 {
100   if (action.GetID() == ACTION_SELECT_ITEM)
101   {
102     m_bSelected = !m_bSelected;
103     CGUIMessage msg(GUI_MSG_CLICKED, GetID(), GetParentID(), action.GetID());
104     SendWindowMessage(msg);
105     return true;
106   }
107   return CGUIControl::OnAction(action);
108 }
109
110 bool CGUICheckMarkControl::OnMessage(CGUIMessage& message)
111 {
112   if ( message.GetControlId() == GetID() )
113   {
114     if (message.GetMessage() == GUI_MSG_LABEL_SET)
115     {
116       m_strLabel = message.GetLabel();
117       return true;
118     }
119   }
120   if (CGUIControl::OnMessage(message)) return true;
121   return false;
122 }
123
124 void CGUICheckMarkControl::AllocResources()
125 {
126   CGUIControl::AllocResources();
127   m_imgCheckMark.AllocResources();
128   m_imgCheckMarkNoFocus.AllocResources();
129 }
130
131 void CGUICheckMarkControl::FreeResources(bool immediately)
132 {
133   CGUIControl::FreeResources(immediately);
134   m_imgCheckMark.FreeResources(immediately);
135   m_imgCheckMarkNoFocus.FreeResources(immediately);
136 }
137
138 void CGUICheckMarkControl::DynamicResourceAlloc(bool bOnOff)
139 {
140   CGUIControl::DynamicResourceAlloc(bOnOff);
141   m_imgCheckMark.DynamicResourceAlloc(bOnOff);
142   m_imgCheckMarkNoFocus.DynamicResourceAlloc(bOnOff);
143 }
144
145 void CGUICheckMarkControl::SetInvalid()
146 {
147   CGUIControl::SetInvalid();
148   m_label.SetInvalid();
149   m_imgCheckMark.SetInvalid();
150   m_imgCheckMarkNoFocus.SetInvalid();
151 }
152
153 void CGUICheckMarkControl::SetSelected(bool bOnOff)
154 {
155   m_bSelected = bOnOff;
156 }
157
158 bool CGUICheckMarkControl::GetSelected() const
159 {
160   return m_bSelected;
161 }
162
163 EVENT_RESULT CGUICheckMarkControl::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
164 {
165   if (event.m_id == ACTION_MOUSE_LEFT_CLICK)
166   {
167     OnAction(CAction(ACTION_SELECT_ITEM));
168     return EVENT_RESULT_HANDLED;
169   }
170   return EVENT_RESULT_UNHANDLED;
171 }
172
173 void CGUICheckMarkControl::SetLabel(const string &label)
174 {
175   if (m_strLabel != label)
176   {
177     m_strLabel = label;
178     SetInvalid();
179   }
180 }
181
182 void CGUICheckMarkControl::PythonSetLabel(const CStdString &strFont, const string &strText, color_t textColor)
183 {
184   m_label.GetLabelInfo().font = g_fontManager.GetFont(strFont);
185   m_label.GetLabelInfo().textColor = textColor;
186   m_label.GetLabelInfo().focusedColor = textColor;
187   m_strLabel = strText;
188   SetInvalid();
189 }
190
191 void CGUICheckMarkControl::PythonSetDisabledColor(color_t disabledColor)
192 {
193   m_label.GetLabelInfo().disabledColor = disabledColor;
194 }
195
196 bool CGUICheckMarkControl::UpdateColors()
197 {
198   bool changed = CGUIControl::UpdateColors();
199   changed |= m_label.UpdateColors();
200   changed |= m_imgCheckMark.SetDiffuseColor(m_diffuseColor);
201   changed |= m_imgCheckMarkNoFocus.SetDiffuseColor(m_diffuseColor);
202
203   return changed;
204 }