[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / guilib / GUIListItemLayout.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 "GUIListItemLayout.h"
22 #include "FileItem.h"
23 #include "GUIControlFactory.h"
24 #include "GUIInfoManager.h"
25 #include "GUIListLabel.h"
26 #include "GUIImage.h"
27 #include "utils/XBMCTinyXML.h"
28
29 using namespace std;
30
31 CGUIListItemLayout::CGUIListItemLayout()
32 : m_group(0, 0, 0, 0, 0, 0)
33 {
34   m_width = 0;
35   m_height = 0;
36   m_condition = 0;
37   m_focused = false;
38   m_invalidated = true;
39   m_group.SetPushUpdates(true);
40 }
41
42 CGUIListItemLayout::CGUIListItemLayout(const CGUIListItemLayout &from)
43 : m_group(from.m_group), m_isPlaying(from.m_isPlaying)
44 {
45   m_width = from.m_width;
46   m_height = from.m_height;
47   m_focused = from.m_focused;
48   m_condition = from.m_condition;
49   m_invalidated = true;
50 }
51
52 CGUIListItemLayout::~CGUIListItemLayout()
53 {
54 }
55
56 bool CGUIListItemLayout::IsAnimating(ANIMATION_TYPE animType)
57 {
58   return m_group.IsAnimating(animType);
59 }
60
61 void CGUIListItemLayout::ResetAnimation(ANIMATION_TYPE animType)
62 {
63   return m_group.ResetAnimation(animType);
64 }
65
66 float CGUIListItemLayout::Size(ORIENTATION orientation) const
67 {
68   return (orientation == HORIZONTAL) ? m_width : m_height;
69 }
70
71 void CGUIListItemLayout::Process(CGUIListItem *item, int parentID, unsigned int currentTime, CDirtyRegionList &dirtyregions)
72 {
73   if (m_invalidated)
74   { // need to update our item
75     m_invalidated = false;
76     // could use a dynamic cast here if RTTI was enabled.  As it's not,
77     // let's use a static cast with a virtual base function
78     CFileItem *fileItem = item->IsFileItem() ? (CFileItem *)item : new CFileItem(*item);
79     m_isPlaying.Update(item);
80     m_group.SetInvalid();
81     m_group.UpdateInfo(fileItem);
82     // delete our temporary fileitem
83     if (!item->IsFileItem())
84       delete fileItem;
85   }
86
87   // update visibility, and render
88   m_group.SetState(item->IsSelected() || m_isPlaying, m_focused);
89   m_group.UpdateVisibility(item);
90   m_group.DoProcess(currentTime, dirtyregions);
91 }
92
93 void CGUIListItemLayout::Render(CGUIListItem *item, int parentID)
94 {
95   m_group.DoRender();
96 }
97
98 void CGUIListItemLayout::SetFocusedItem(unsigned int focus)
99 {
100   m_group.SetFocusedItem(focus);
101 }
102
103 unsigned int CGUIListItemLayout::GetFocusedItem() const
104 {
105   return m_group.GetFocusedItem();
106 }
107
108 void CGUIListItemLayout::SetWidth(float width)
109 {
110   m_group.EnlargeWidth(width - m_width);
111   m_width = width;
112   SetInvalid();
113 }
114
115 void CGUIListItemLayout::SetHeight(float height)
116 {
117   m_group.EnlargeHeight(height - m_height);
118   m_height = height;
119   SetInvalid();
120 }
121
122 void CGUIListItemLayout::SelectItemFromPoint(const CPoint &point)
123 {
124   m_group.SelectItemFromPoint(point);
125 }
126
127 bool CGUIListItemLayout::MoveLeft()
128 {
129   return m_group.MoveLeft();
130 }
131
132 bool CGUIListItemLayout::MoveRight()
133 {
134   return m_group.MoveRight();
135 }
136
137 bool CGUIListItemLayout::CheckCondition()
138 {
139   return !m_condition || g_infoManager.GetBoolValue(m_condition);
140 }
141
142 void CGUIListItemLayout::LoadControl(TiXmlElement *child, CGUIControlGroup *group)
143 {
144   if (!group) return;
145
146   CRect rect(group->GetXPosition(), group->GetYPosition(), group->GetXPosition() + group->GetWidth(), group->GetYPosition() + group->GetHeight());
147
148   CGUIControlFactory factory;
149   CGUIControl *control = factory.Create(0, rect, child, true);  // true indicating we're inside a list for the
150                                                                 // different label control + defaults.
151   if (control)
152   {
153     group->AddControl(control);
154     if (control->IsGroup())
155     {
156       TiXmlElement *grandChild = child->FirstChildElement("control");
157       while (grandChild)
158       {
159         LoadControl(grandChild, (CGUIControlGroup *)control);
160         grandChild = grandChild->NextSiblingElement("control");
161       }
162     }
163   }
164 }
165
166 void CGUIListItemLayout::LoadLayout(TiXmlElement *layout, int context, bool focused)
167 {
168   m_focused = focused;
169   layout->QueryFloatAttribute("width", &m_width);
170   layout->QueryFloatAttribute("height", &m_height);
171   const char *condition = layout->Attribute("condition");
172   if (condition)
173     m_condition = g_infoManager.Register(condition, context);
174   m_isPlaying.Parse("listitem.isplaying", context);
175   TiXmlElement *child = layout->FirstChildElement("control");
176   m_group.SetWidth(m_width);
177   m_group.SetHeight(m_height);
178   while (child)
179   {
180     LoadControl(child, &m_group);
181     child = child->NextSiblingElement("control");
182   }
183   // ensure width and height are valid
184   m_width = std::max(1.0f, m_width);
185   m_height = std::max(1.0f, m_height);
186 }
187
188 //#ifdef PRE_SKIN_VERSION_9_10_COMPATIBILITY
189 void CGUIListItemLayout::CreateListControlLayouts(float width, float height, bool focused, const CLabelInfo &labelInfo, const CLabelInfo &labelInfo2, const CTextureInfo &texture, const CTextureInfo &textureFocus, float texHeight, float iconWidth, float iconHeight, const CStdString &nofocusCondition, const CStdString &focusCondition)
190 {
191   m_width = width;
192   m_height = height;
193   m_focused = focused;
194   m_isPlaying.Parse("listitem.isplaying", 0);
195   CGUIImage *tex = new CGUIImage(0, 0, 0, 0, width, texHeight, texture);
196   tex->SetVisibleCondition(nofocusCondition);
197   m_group.AddControl(tex);
198   if (focused)
199   {
200     CGUIImage *tex = new CGUIImage(0, 0, 0, 0, width, texHeight, textureFocus);
201     tex->SetVisibleCondition(focusCondition);
202     m_group.AddControl(tex);
203   }
204   CGUIImage *image = new CGUIImage(0, 0, 8, 0, iconWidth, texHeight, CTextureInfo(""));
205   image->SetInfo(CGUIInfoLabel("$INFO[ListItem.Icon]", "", m_group.GetParentID()));
206   image->SetAspectRatio(CAspectRatio::AR_KEEP);
207   m_group.AddControl(image);
208   float x = iconWidth + labelInfo.offsetX + 10;
209   CGUIListLabel *label = new CGUIListLabel(0, 0, x, labelInfo.offsetY, width - x - 18, height, labelInfo, CGUIInfoLabel("$INFO[ListItem.Label]", "", m_group.GetParentID()), false);
210   m_group.AddControl(label);
211   x = labelInfo2.offsetX ? labelInfo2.offsetX : m_width - 16;
212   label = new CGUIListLabel(0, 0, x, labelInfo2.offsetY, x - iconWidth - 20, height, labelInfo2, CGUIInfoLabel("$INFO[ListItem.Label2]", "", m_group.GetParentID()), false);
213   m_group.AddControl(label);
214 }
215 //#endif
216
217 void CGUIListItemLayout::FreeResources(bool immediately)
218 {
219   m_group.FreeResources(immediately);
220 }
221
222 #ifdef _DEBUG
223 void CGUIListItemLayout::DumpTextureUse()
224 {
225   m_group.DumpTextureUse();
226 }
227 #endif