[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / music / dialogs / GUIDialogVisualisationPresetList.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 "GUIDialogVisualisationPresetList.h"
22 #include "addons/Visualisation.h"
23 #include "guilib/GUIWindowManager.h"
24 #include "guilib/GUIListContainer.h"
25 #include "GUIUserMessages.h"
26 #include "FileItem.h"
27 #include "guilib/Key.h"
28 #include "guilib/LocalizeStrings.h"
29 #include "utils/StringUtils.h"
30
31 #define CONTROL_LIST           2
32 #define CONTROL_PRESETS_LABEL  3
33 #define CONTROL_NONE_AVAILABLE 4
34
35 using ADDON::CVisualisation;
36
37 CGUIDialogVisualisationPresetList::CGUIDialogVisualisationPresetList(void)
38     : CGUIDialog(WINDOW_DIALOG_VIS_PRESET_LIST, "VisualisationPresetList.xml")
39 {
40   m_currentPreset = 0;
41   m_vecPresets = new CFileItemList;
42   m_viz = NULL;
43   m_loadType = KEEP_IN_MEMORY;
44 }
45
46 CGUIDialogVisualisationPresetList::~CGUIDialogVisualisationPresetList(void)
47 {
48   delete m_vecPresets;
49 }
50
51 bool CGUIDialogVisualisationPresetList::OnMessage(CGUIMessage &message)
52 {
53   switch (message.GetMessage())
54   {
55   case GUI_MSG_CLICKED:
56     {
57       if (message.GetSenderId() == CONTROL_LIST && (message.GetParam1() == ACTION_SELECT_ITEM ||
58                                                     message.GetParam1() == ACTION_MOUSE_LEFT_CLICK))
59       {
60         //clicked - ask for the preset to be changed to the new one
61         CGUIListContainer *pList = (CGUIListContainer *)GetControl(CONTROL_LIST);
62         if (pList)
63         {
64           int iItem = pList->GetSelectedItem();
65           if (m_viz)
66             m_viz->OnAction(VIS_ACTION_LOAD_PRESET, (void *)&iItem);
67         }
68         return true;
69       }
70     }
71     break;
72   case GUI_MSG_WINDOW_DEINIT:
73   case GUI_MSG_VISUALISATION_UNLOADING:
74     {
75       m_viz = NULL;
76       CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_LIST);
77       OnMessage(msg);
78       Update();
79     }
80     break;
81   case GUI_MSG_VISUALISATION_LOADED:
82     {
83       SetVisualisation((CVisualisation*)message.GetPointer());
84     }
85     break;
86   }
87   return CGUIDialog::OnMessage(message);
88 }
89
90 void CGUIDialogVisualisationPresetList::SetVisualisation(CVisualisation* vis)
91 {
92   m_viz = NULL;
93   if (vis)
94   {
95     m_viz = vis;
96     Update();
97   }
98 }
99
100 void CGUIDialogVisualisationPresetList::FrameMove()
101 {
102   //FIXME we shouldn't have to check preset each frame
103   // a viz callback could push GUI_MSG_VISUALISATION_UPDATED
104   if (m_viz)
105   {
106     unsigned preset = m_viz->GetPreset();
107     if (preset != m_currentPreset && preset < (unsigned int)m_vecPresets->Size())
108     {
109       m_vecPresets->Get(m_currentPreset)->Select(false);
110       m_currentPreset = preset;
111       m_vecPresets->Get(m_currentPreset)->Select(true);
112     }
113   }
114   CGUIDialog::FrameMove();
115 }
116
117 void CGUIDialogVisualisationPresetList::OnInitWindow()
118 {
119   CGUIMessage msg(GUI_MSG_GET_VISUALISATION, 0, 0);
120   g_windowManager.SendMessage(msg);
121   SetVisualisation((CVisualisation*)msg.GetPointer());
122   CGUIDialog::OnInitWindow();
123 }
124
125 void CGUIDialogVisualisationPresetList::Update()
126 {
127   m_vecPresets->Clear();
128   CStdString strHeading;
129   if (m_viz)
130   {
131     strHeading = StringUtils::Format(g_localizeStrings.Get(13407).c_str(), m_viz->Name().c_str());
132
133     //clear filelist
134     CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_LIST);
135     OnMessage(msg);
136     std::vector<CStdString> presets;
137     if (m_viz->GetPresetList(presets))
138     {
139       m_currentPreset = m_viz->GetPreset();
140       for (unsigned i = 0; i < presets.size(); i++)
141       {
142         CFileItemPtr pItem(new CFileItem(presets[i]));
143         if (i == m_currentPreset)
144           pItem->Select(true);
145         pItem->RemoveExtension();
146         pItem->SetLabel2(" ");
147         m_vecPresets->Add(pItem);
148       }
149       CGUIMessage msg(GUI_MSG_LABEL_BIND, GetID(), CONTROL_LIST, 0, 0, m_vecPresets);
150       OnMessage(msg);
151     }
152   }
153
154   // update our dialog's label
155   SET_CONTROL_LABEL(CONTROL_PRESETS_LABEL, strHeading);
156
157   // if there is no presets, add a label saying so
158   if (m_vecPresets->Size() == 0)
159   {
160     SET_CONTROL_VISIBLE(CONTROL_NONE_AVAILABLE);
161   }
162   else
163   {
164     SET_CONTROL_HIDDEN(CONTROL_NONE_AVAILABLE);
165     CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), CONTROL_LIST, m_currentPreset);
166     OnMessage(msg);
167   }
168 }
169