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/>.
22 #include "GUIListContainer.h"
23 #include "GUIListItem.h"
26 CGUIListContainer::CGUIListContainer(int parentID, int controlID, float posX, float posY, float width, float height, ORIENTATION orientation, const CScroller& scroller, int preloadItems)
27 : CGUIBaseContainer(parentID, controlID, posX, posY, width, height, orientation, scroller, preloadItems)
29 ControlType = GUICONTAINER_LIST;
30 m_type = VIEW_TYPE_LIST;
33 CGUIListContainer::~CGUIListContainer(void)
37 bool CGUIListContainer::OnAction(const CAction &action)
39 switch (action.GetID())
44 { // already on the first page, so move to the first item
48 { // scroll up to the previous page
49 Scroll( -m_itemsPerPage);
54 case ACTION_PAGE_DOWN:
56 if (GetOffset() == (int)m_items.size() - m_itemsPerPage || (int)m_items.size() < m_itemsPerPage)
57 { // already at the last page, so move to the last item.
58 SetCursor(m_items.size() - GetOffset() - 1);
61 { // scroll down to the next page
62 Scroll(m_itemsPerPage);
67 // smooth scrolling (for analog controls)
68 case ACTION_SCROLL_UP:
70 m_analogScrollCount += action.GetAmount() * action.GetAmount();
72 while (m_analogScrollCount > 0.4)
75 m_analogScrollCount -= 0.4f;
76 if (GetOffset() > 0 && GetCursor() <= m_itemsPerPage / 2)
80 else if (GetCursor() > 0)
82 SetCursor(GetCursor() - 1);
88 case ACTION_SCROLL_DOWN:
90 m_analogScrollCount += action.GetAmount() * action.GetAmount();
92 while (m_analogScrollCount > 0.4)
95 m_analogScrollCount -= 0.4f;
96 if (GetOffset() + m_itemsPerPage < (int)m_items.size() && GetCursor() >= m_itemsPerPage / 2)
100 else if (GetCursor() < m_itemsPerPage - 1 && GetOffset() + GetCursor() < (int)m_items.size() - 1)
102 SetCursor(GetCursor() + 1);
109 return CGUIBaseContainer::OnAction(action);
112 bool CGUIListContainer::OnMessage(CGUIMessage& message)
114 if (message.GetControlId() == GetID() )
116 if (message.GetMessage() == GUI_MSG_LABEL_RESET)
120 else if (message.GetMessage() == GUI_MSG_SETFOCUS)
122 if (message.GetParam1()) // subfocus item is specified, so set the offset appropriately
124 int item = std::min(GetOffset() + (int)message.GetParam1() - 1, (int)m_items.size() - 1);
129 return CGUIBaseContainer::OnMessage(message);
132 bool CGUIListContainer::MoveUp(bool wrapAround)
136 SetCursor(GetCursor() - 1);
138 else if (GetCursor() == 0 && GetOffset())
140 ScrollToOffset(GetOffset() - 1);
144 if (m_items.size() > 0)
145 { // move 2 last item in list, and set our container moving up
146 int offset = m_items.size() - m_itemsPerPage;
147 if (offset < 0) offset = 0;
148 SetCursor(m_items.size() - offset - 1);
149 ScrollToOffset(offset);
150 SetContainerMoving(-1);
158 bool CGUIListContainer::MoveDown(bool wrapAround)
160 if (GetOffset() + GetCursor() + 1 < (int)m_items.size())
162 if (GetCursor() + 1 < m_itemsPerPage)
164 SetCursor(GetCursor() + 1);
168 ScrollToOffset(GetOffset() + 1);
172 { // move first item in list, and set our container moving in the "down" direction
175 SetContainerMoving(1);
182 // scrolls the said amount
183 void CGUIListContainer::Scroll(int amount)
185 // increase or decrease the offset
186 int offset = GetOffset() + amount;
187 if (offset > (int)m_items.size() - m_itemsPerPage)
189 offset = m_items.size() - m_itemsPerPage;
191 if (offset < 0) offset = 0;
192 ScrollToOffset(offset);
195 void CGUIListContainer::ValidateOffset()
197 if (!m_layout) return;
198 // first thing is we check the range of our offset
199 // don't validate offset if we are scrolling in case the tween image exceed <0, 1> range
200 int minOffset, maxOffset;
201 GetOffsetRange(minOffset, maxOffset);
202 if (GetOffset() > maxOffset || (!m_scroller.IsScrolling() && m_scroller.GetValue() > maxOffset * m_layout->Size(m_orientation)))
204 SetOffset(std::max(0, maxOffset));
205 m_scroller.SetValue(GetOffset() * m_layout->Size(m_orientation));
207 if (GetOffset() < 0 || (!m_scroller.IsScrolling() && m_scroller.GetValue() < 0))
210 m_scroller.SetValue(0);
214 void CGUIListContainer::SetCursor(int cursor)
216 if (cursor > m_itemsPerPage - 1) cursor = m_itemsPerPage - 1;
217 if (cursor < 0) cursor = 0;
219 SetContainerMoving(cursor - GetCursor());
220 CGUIBaseContainer::SetCursor(cursor);
223 void CGUIListContainer::SelectItem(int item)
225 // Check that our offset is valid
227 // only select an item if it's in a valid range
228 if (item >= 0 && item < (int)m_items.size())
230 // Select the item requested
231 if (item >= GetOffset() && item < GetOffset() + m_itemsPerPage)
232 { // the item is on the current page, so don't change it.
233 SetCursor(item - GetOffset());
235 else if (item < GetOffset())
236 { // item is on a previous page - make it the first item on the page
238 ScrollToOffset(item);
240 else // (item >= GetOffset()+m_itemsPerPage)
241 { // item is on a later page - make it the last item on the page
242 SetCursor(m_itemsPerPage - 1);
243 ScrollToOffset(item - GetCursor());
248 int CGUIListContainer::GetCursorFromPoint(const CPoint &point, CPoint *itemPoint) const
250 if (!m_focusedLayout || !m_layout)
254 float pos = (m_orientation == VERTICAL) ? point.y : point.x;
255 while (row < m_itemsPerPage + 1) // 1 more to ensure we get the (possible) half item at the end.
257 const CGUIListItemLayout *layout = (row == GetCursor()) ? m_focusedLayout : m_layout;
258 if (pos < layout->Size(m_orientation) && row + GetOffset() < (int)m_items.size())
259 { // found correct "row" -> check horizontal
260 if (!InsideLayout(layout, point))
264 *itemPoint = m_orientation == VERTICAL ? CPoint(point.x, pos) : CPoint(pos, point.y);
268 pos -= layout->Size(m_orientation);
273 bool CGUIListContainer::SelectItemFromPoint(const CPoint &point)
276 int row = GetCursorFromPoint(point, &itemPoint);
281 CGUIListItemLayout *focusedLayout = GetFocusedLayout();
283 focusedLayout->SelectItemFromPoint(itemPoint);
287 //#ifdef PRE_SKIN_VERSION_9_10_COMPATIBILITY
288 CGUIListContainer::CGUIListContainer(int parentID, int controlID, float posX, float posY, float width, float height,
289 const CLabelInfo& labelInfo, const CLabelInfo& labelInfo2,
290 const CTextureInfo& textureButton, const CTextureInfo& textureButtonFocus,
291 float textureHeight, float itemWidth, float itemHeight, float spaceBetweenItems)
292 : CGUIBaseContainer(parentID, controlID, posX, posY, width, height, VERTICAL, 200, 0)
294 CGUIListItemLayout layout;
295 layout.CreateListControlLayouts(width, textureHeight + spaceBetweenItems, false, labelInfo, labelInfo2, textureButton, textureButtonFocus, textureHeight, itemWidth, itemHeight, "", "");
296 m_layouts.push_back(layout);
297 CStdString condition;
298 condition.Format("control.hasfocus(%i)", controlID);
299 CStdString condition2 = "!" + condition;
300 CGUIListItemLayout focusLayout;
301 focusLayout.CreateListControlLayouts(width, textureHeight + spaceBetweenItems, true, labelInfo, labelInfo2, textureButton, textureButtonFocus, textureHeight, itemWidth, itemHeight, condition2, condition);
302 m_focusedLayouts.push_back(focusLayout);
303 m_height = floor(m_height / (textureHeight + spaceBetweenItems)) * (textureHeight + spaceBetweenItems);
304 ControlType = GUICONTAINER_LIST;
308 bool CGUIListContainer::HasNextPage() const
310 return (GetOffset() != (int)m_items.size() - m_itemsPerPage && (int)m_items.size() >= m_itemsPerPage);
313 bool CGUIListContainer::HasPreviousPage() const
315 return (GetOffset() > 0);