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 "GUIDialogSelect.h"
22 #include "guilib/GUIWindowManager.h"
24 #include "guilib/Key.h"
25 #include "guilib/LocalizeStrings.h"
26 #include "utils/StringUtils.h"
28 #define CONTROL_HEADING 1
29 #define CONTROL_LIST 3
30 #define CONTROL_NUMBEROFFILES 2
31 #define CONTROL_BUTTON 5
32 #define CONTROL_DETAILS 6
34 CGUIDialogSelect::CGUIDialogSelect(void)
35 : CGUIDialogBoxBase(WINDOW_DIALOG_SELECT, "DialogSelect.xml")
37 m_bButtonEnabled = false;
40 m_vecList = new CFileItemList;
41 m_selectedItems = new CFileItemList;
42 m_multiSelection = false;
44 m_loadType = KEEP_IN_MEMORY;
47 CGUIDialogSelect::~CGUIDialogSelect(void)
50 delete m_selectedItems;
53 bool CGUIDialogSelect::OnMessage(CGUIMessage& message)
55 switch ( message.GetMessage() )
57 case GUI_MSG_WINDOW_DEINIT:
59 CGUIDialog::OnMessage(message);
60 m_viewControl.Clear();
62 m_bButtonEnabled = false;
64 m_multiSelection = false;
66 // construct selected items list
67 m_selectedItems->Clear();
69 for (int i = 0 ; i < m_vecList->Size() ; i++)
71 CFileItemPtr item = m_vecList->Get(i);
72 if (item->IsSelected())
74 m_selectedItems->Add(item);
75 if (m_iSelected == -1)
83 SET_CONTROL_LABEL(CONTROL_BUTTON, "");
88 case GUI_MSG_WINDOW_INIT:
90 m_bButtonPressed = false;
92 CGUIDialog::OnMessage(message);
100 int iControl = message.GetSenderId();
101 if (m_viewControl.HasControl(CONTROL_LIST))
103 int iAction = message.GetParam1();
104 if (ACTION_SELECT_ITEM == iAction || ACTION_MOUSE_LEFT_CLICK == iAction)
106 int iSelected = m_viewControl.GetSelectedItem();
107 if(iSelected >= 0 && iSelected < (int)m_vecList->Size())
109 CFileItemPtr item(m_vecList->Get(iSelected));
110 if (m_multiSelection)
111 item->Select(!item->IsSelected());
114 for (int i = 0 ; i < m_vecList->Size() ; i++)
115 m_vecList->Get(i)->Select(false);
123 if (CONTROL_BUTTON == iControl)
126 m_bButtonPressed = true;
127 if (m_multiSelection)
133 case GUI_MSG_SETFOCUS:
135 // make sure the additional button is focused in case the list is empty
136 // (otherwise it is impossible to navigate to the additional button)
137 if (m_vecList->IsEmpty() && m_bButtonEnabled &&
138 m_viewControl.HasControl(message.GetControlId()))
140 SET_CONTROL_FOCUS(CONTROL_BUTTON, 0);
143 if (m_viewControl.HasControl(message.GetControlId()) && m_viewControl.GetCurrentControl() != message.GetControlId())
145 m_viewControl.SetFocused();
152 return CGUIDialog::OnMessage(message);
155 bool CGUIDialogSelect::OnBack(int actionID)
158 m_selectedItems->Clear();
159 m_bConfirmed = false;
160 return CGUIDialog::OnBack(actionID);
163 void CGUIDialogSelect::Reset()
165 m_bButtonEnabled = false;
166 m_useDetails = false;
167 m_multiSelection = false;
170 m_selectedItems->Clear();
173 int CGUIDialogSelect::Add(const CStdString& strLabel)
175 CFileItemPtr pItem(new CFileItem(strLabel));
176 m_vecList->Add(pItem);
177 return m_vecList->Size() - 1;
180 void CGUIDialogSelect::Add(const CFileItemList& items)
182 for (int i=0;i<items.Size();++i)
184 CFileItemPtr item = items[i];
189 int CGUIDialogSelect::Add(const CFileItem* pItem)
191 CFileItemPtr item(new CFileItem(*pItem));
192 m_vecList->Add(item);
193 return m_vecList->Size() - 1;
196 void CGUIDialogSelect::SetItems(CFileItemList* pList)
198 // need to make internal copy of list to be sure dialog is owner of it
201 m_vecList->Copy(*pList);
204 int CGUIDialogSelect::GetSelectedLabel() const
209 const CFileItemPtr CGUIDialogSelect::GetSelectedItem()
211 return m_selectedItems->Size() > 0 ? m_selectedItems->Get(0) : CFileItemPtr(new CFileItem);
214 const CStdString& CGUIDialogSelect::GetSelectedLabelText()
216 return GetSelectedItem()->GetLabel();
219 const CFileItemList& CGUIDialogSelect::GetSelectedItems() const
221 return *m_selectedItems;
224 void CGUIDialogSelect::EnableButton(bool enable, int string)
226 m_bButtonEnabled = enable;
227 m_buttonString = string;
233 bool CGUIDialogSelect::IsButtonPressed()
235 return m_bButtonPressed;
238 void CGUIDialogSelect::Sort(bool bSortOrder /*=true*/)
240 m_vecList->Sort(SortByLabel, bSortOrder ? SortOrderAscending : SortOrderDescending);
243 void CGUIDialogSelect::SetSelected(int iSelected)
245 if (iSelected < 0 || iSelected >= (int)m_vecList->Size() ||
246 m_vecList->Get(iSelected).get() == NULL)
249 // only set m_iSelected if there is no multi-select
250 // or if it doesn't have a valid value yet
251 // or if the current value is bigger than the new one
252 // so that we always focus the item nearest to the beginning of the list
253 if (!m_multiSelection || m_iSelected < 0 || m_iSelected > iSelected)
254 m_iSelected = iSelected;
255 m_vecList->Get(iSelected)->Select(true);
256 m_selectedItems->Add(m_vecList->Get(iSelected));
259 void CGUIDialogSelect::SetSelected(const CStdString &strSelectedLabel)
261 if (strSelectedLabel.empty())
264 for (int index = 0; index < m_vecList->Size(); index++)
266 if (strSelectedLabel.Equals(m_vecList->Get(index)->GetLabel()))
274 void CGUIDialogSelect::SetSelected(std::vector<int> selectedIndexes)
276 if (selectedIndexes.empty())
279 for (std::vector<int>::const_iterator it = selectedIndexes.begin(); it != selectedIndexes.end(); it++)
283 void CGUIDialogSelect::SetSelected(const std::vector<CStdString> &selectedLabels)
285 if (selectedLabels.empty())
288 for (std::vector<CStdString>::const_iterator it = selectedLabels.begin(); it != selectedLabels.end(); it++)
292 void CGUIDialogSelect::SetUseDetails(bool useDetails)
294 m_useDetails = useDetails;
297 void CGUIDialogSelect::SetMultiSelection(bool multiSelection)
299 m_multiSelection = multiSelection;
302 CGUIControl *CGUIDialogSelect::GetFirstFocusableControl(int id)
304 if (m_viewControl.HasControl(id))
305 id = m_viewControl.GetCurrentControl();
306 return CGUIDialogBoxBase::GetFirstFocusableControl(id);
309 void CGUIDialogSelect::OnWindowLoaded()
311 CGUIDialogBoxBase::OnWindowLoaded();
312 m_viewControl.Reset();
313 m_viewControl.SetParentWindow(GetID());
314 m_viewControl.AddView(GetControl(CONTROL_LIST));
315 m_viewControl.AddView(GetControl(CONTROL_DETAILS));
318 void CGUIDialogSelect::OnInitWindow()
320 m_viewControl.SetItems(*m_vecList);
321 m_selectedItems->Clear();
322 if (m_iSelected == -1)
324 for(int i = 0 ; i < m_vecList->Size(); i++)
326 if (m_vecList->Get(i)->IsSelected())
333 m_viewControl.SetCurrentView(m_useDetails ? CONTROL_DETAILS : CONTROL_LIST);
335 CStdString items = StringUtils::Format("%i %s", m_vecList->Size(), g_localizeStrings.Get(127).c_str());
336 SET_CONTROL_LABEL(CONTROL_NUMBEROFFILES, items);
338 if (m_multiSelection)
339 EnableButton(true, 186);
342 CGUIDialogBoxBase::OnInitWindow();
344 // if m_iSelected < 0 focus first item
345 m_viewControl.SetSelectedItem(std::max(m_iSelected, 0));
348 void CGUIDialogSelect::OnWindowUnload()
350 CGUIDialog::OnWindowUnload();
351 m_viewControl.Reset();
354 void CGUIDialogSelect::SetupButton()
356 if (m_bButtonEnabled)
358 SET_CONTROL_LABEL(CONTROL_BUTTON, g_localizeStrings.Get(m_buttonString));
359 SET_CONTROL_VISIBLE(CONTROL_BUTTON);
362 SET_CONTROL_HIDDEN(CONTROL_BUTTON);