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 "GUIListContainer.h"
22 #include "GUIListItem.h"
25 CGUIListContainer::CGUIListContainer(int parentID, int controlID, float posX, float posY, float width, float height, ORIENTATION orientation, const CScroller& scroller, int preloadItems)
26 : CGUIBaseContainer(parentID, controlID, posX, posY, width, height, orientation, scroller, preloadItems)
28 ControlType = GUICONTAINER_LIST;
29 m_type = VIEW_TYPE_LIST;
32 CGUIListContainer::~CGUIListContainer(void)
36 bool CGUIListContainer::OnAction(const CAction &action)
38 switch (action.GetID())
43 { // already on the first page, so move to the first item
47 { // scroll up to the previous page
48 Scroll( -m_itemsPerPage);
53 case ACTION_PAGE_DOWN:
55 if (GetOffset() == (int)m_items.size() - m_itemsPerPage || (int)m_items.size() < m_itemsPerPage)
56 { // already at the last page, so move to the last item.
57 SetCursor(m_items.size() - GetOffset() - 1);
60 { // scroll down to the next page
61 Scroll(m_itemsPerPage);
66 // smooth scrolling (for analog controls)
67 case ACTION_SCROLL_UP:
69 m_analogScrollCount += action.GetAmount() * action.GetAmount();
71 while (m_analogScrollCount > 0.4)
74 m_analogScrollCount -= 0.4f;
75 if (GetOffset() > 0 && GetCursor() <= m_itemsPerPage / 2)
79 else if (GetCursor() > 0)
81 SetCursor(GetCursor() - 1);
87 case ACTION_SCROLL_DOWN:
89 m_analogScrollCount += action.GetAmount() * action.GetAmount();
91 while (m_analogScrollCount > 0.4)
94 m_analogScrollCount -= 0.4f;
95 if (GetOffset() + m_itemsPerPage < (int)m_items.size() && GetCursor() >= m_itemsPerPage / 2)
99 else if (GetCursor() < m_itemsPerPage - 1 && GetOffset() + GetCursor() < (int)m_items.size() - 1)
101 SetCursor(GetCursor() + 1);
108 return CGUIBaseContainer::OnAction(action);
111 bool CGUIListContainer::OnMessage(CGUIMessage& message)
113 if (message.GetControlId() == GetID() )
115 if (message.GetMessage() == GUI_MSG_LABEL_RESET)
119 else if (message.GetMessage() == GUI_MSG_SETFOCUS)
121 if (message.GetParam1()) // subfocus item is specified, so set the offset appropriately
123 int item = std::min(GetOffset() + (int)message.GetParam1() - 1, (int)m_items.size() - 1);
128 return CGUIBaseContainer::OnMessage(message);
131 bool CGUIListContainer::MoveUp(bool wrapAround)
135 SetCursor(GetCursor() - 1);
137 else if (GetCursor() == 0 && GetOffset())
139 ScrollToOffset(GetOffset() - 1);
143 if (m_items.size() > 0)
144 { // move 2 last item in list, and set our container moving up
145 int offset = m_items.size() - m_itemsPerPage;
146 if (offset < 0) offset = 0;
147 SetCursor(m_items.size() - offset - 1);
148 ScrollToOffset(offset);
149 SetContainerMoving(-1);
157 bool CGUIListContainer::MoveDown(bool wrapAround)
159 if (GetOffset() + GetCursor() + 1 < (int)m_items.size())
161 if (GetCursor() + 1 < m_itemsPerPage)
163 SetCursor(GetCursor() + 1);
167 ScrollToOffset(GetOffset() + 1);
171 { // move first item in list, and set our container moving in the "down" direction
174 SetContainerMoving(1);
181 // scrolls the said amount
182 void CGUIListContainer::Scroll(int amount)
184 // increase or decrease the offset
185 int offset = GetOffset() + amount;
186 if (offset > (int)m_items.size() - m_itemsPerPage)
188 offset = m_items.size() - m_itemsPerPage;
190 if (offset < 0) offset = 0;
191 ScrollToOffset(offset);
194 void CGUIListContainer::ValidateOffset()
196 if (!m_layout) return;
197 // first thing is we check the range of our offset
198 // don't validate offset if we are scrolling in case the tween image exceed <0, 1> range
199 int minOffset, maxOffset;
200 GetOffsetRange(minOffset, maxOffset);
201 if (GetOffset() > maxOffset || (!m_scroller.IsScrolling() && m_scroller.GetValue() > maxOffset * m_layout->Size(m_orientation)))
203 SetOffset(std::max(0, maxOffset));
204 m_scroller.SetValue(GetOffset() * m_layout->Size(m_orientation));
206 if (GetOffset() < 0 || (!m_scroller.IsScrolling() && m_scroller.GetValue() < 0))
209 m_scroller.SetValue(0);
213 void CGUIListContainer::SetCursor(int cursor)
215 if (cursor > m_itemsPerPage - 1) cursor = m_itemsPerPage - 1;
216 if (cursor < 0) cursor = 0;
218 SetContainerMoving(cursor - GetCursor());
219 CGUIBaseContainer::SetCursor(cursor);
222 void CGUIListContainer::SelectItem(int item)
224 // Check that our offset is valid
226 // only select an item if it's in a valid range
227 if (item >= 0 && item < (int)m_items.size())
229 // Select the item requested
230 if (item >= GetOffset() && item < GetOffset() + m_itemsPerPage)
231 { // the item is on the current page, so don't change it.
232 SetCursor(item - GetOffset());
234 else if (item < GetOffset())
235 { // item is on a previous page - make it the first item on the page
237 ScrollToOffset(item);
239 else // (item >= GetOffset()+m_itemsPerPage)
240 { // item is on a later page - make it the last item on the page
241 SetCursor(m_itemsPerPage - 1);
242 ScrollToOffset(item - GetCursor());
247 int CGUIListContainer::GetCursorFromPoint(const CPoint &point, CPoint *itemPoint) const
249 if (!m_focusedLayout || !m_layout)
253 float pos = (m_orientation == VERTICAL) ? point.y : point.x;
254 while (row < m_itemsPerPage + 1) // 1 more to ensure we get the (possible) half item at the end.
256 const CGUIListItemLayout *layout = (row == GetCursor()) ? m_focusedLayout : m_layout;
257 if (pos < layout->Size(m_orientation) && row + GetOffset() < (int)m_items.size())
258 { // found correct "row" -> check horizontal
259 if (!InsideLayout(layout, point))
263 *itemPoint = m_orientation == VERTICAL ? CPoint(point.x, pos) : CPoint(pos, point.y);
267 pos -= layout->Size(m_orientation);
272 bool CGUIListContainer::SelectItemFromPoint(const CPoint &point)
275 int row = GetCursorFromPoint(point, &itemPoint);
280 CGUIListItemLayout *focusedLayout = GetFocusedLayout();
282 focusedLayout->SelectItemFromPoint(itemPoint);
286 //#ifdef PRE_SKIN_VERSION_9_10_COMPATIBILITY
287 CGUIListContainer::CGUIListContainer(int parentID, int controlID, float posX, float posY, float width, float height,
288 const CLabelInfo& labelInfo, const CLabelInfo& labelInfo2,
289 const CTextureInfo& textureButton, const CTextureInfo& textureButtonFocus,
290 float textureHeight, float itemWidth, float itemHeight, float spaceBetweenItems)
291 : CGUIBaseContainer(parentID, controlID, posX, posY, width, height, VERTICAL, 200, 0)
293 CGUIListItemLayout layout;
294 layout.CreateListControlLayouts(width, textureHeight + spaceBetweenItems, false, labelInfo, labelInfo2, textureButton, textureButtonFocus, textureHeight, itemWidth, itemHeight, "", "");
295 m_layouts.push_back(layout);
296 CStdString condition;
297 condition.Format("control.hasfocus(%i)", controlID);
298 CStdString condition2 = "!" + condition;
299 CGUIListItemLayout focusLayout;
300 focusLayout.CreateListControlLayouts(width, textureHeight + spaceBetweenItems, true, labelInfo, labelInfo2, textureButton, textureButtonFocus, textureHeight, itemWidth, itemHeight, condition2, condition);
301 m_focusedLayouts.push_back(focusLayout);
302 m_height = floor(m_height / (textureHeight + spaceBetweenItems)) * (textureHeight + spaceBetweenItems);
303 ControlType = GUICONTAINER_LIST;
307 bool CGUIListContainer::HasNextPage() const
309 return (GetOffset() != (int)m_items.size() - m_itemsPerPage && (int)m_items.size() >= m_itemsPerPage);
312 bool CGUIListContainer::HasPreviousPage() const
314 return (GetOffset() > 0);