| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "GUIViewControl.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "GUIInfoManager.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "guilib/GUIComponent.h" |
| 15 |
#include "guilib/GUIWindowManager.h" |
| 16 |
#include "guilib/IGUIContainer.h" |
| 17 |
#include "guilib/LocalizeStrings.h" |
| 18 |
#include "guilib/WindowIDs.h" |
| 19 |
#include "utils/StringUtils.h" |
| 20 |
#include "utils/URIUtils.h" |
| 21 |
|
| 22 |
#include <utility> |
| 23 |
|
| 24 |
CGUIViewControl::CGUIViewControl(void) |
| 25 |
{ |
| 26 |
m_viewAsControl = -1; |
| 27 |
m_parentWindow = WINDOW_INVALID; |
| 28 |
m_fileItems = nullptr; |
| 29 |
Reset(); |
| 30 |
} |
| 31 |
|
| 32 |
CGUIViewControl::~CGUIViewControl(void) = default; |
| 33 |
|
| 34 |
void CGUIViewControl::Reset() |
| 35 |
{ |
| 36 |
m_currentView = -1; |
| 37 |
m_visibleViews.clear(); |
| 38 |
m_allViews.clear(); |
| 39 |
} |
| 40 |
|
| 41 |
void CGUIViewControl::AddView(const CGUIControl *control) |
| 42 |
{ |
| 43 |
if (!control || !control->IsContainer()) return; |
| 44 |
m_allViews.push_back(const_cast<CGUIControl*>(control)); |
| 45 |
} |
| 46 |
|
| 47 |
void CGUIViewControl::SetViewControlID(int control) |
| 48 |
{ |
| 49 |
m_viewAsControl = control; |
| 50 |
} |
| 51 |
|
| 52 |
void CGUIViewControl::SetParentWindow(int window) |
| 53 |
{ |
| 54 |
m_parentWindow = window; |
| 55 |
} |
| 56 |
|
| 57 |
void CGUIViewControl::SetCurrentView(int viewMode, bool bRefresh /* = false */) |
| 58 |
{ |
| 59 |
// grab the previous control |
| 60 |
CGUIControl *previousView = nullptr; |
| 61 |
if (m_currentView >= 0 && m_currentView < (int)m_visibleViews.size()) |
| 62 |
previousView = m_visibleViews[m_currentView]; |
| 63 |
|
| 64 |
UpdateViewVisibility(); |
| 65 |
|
| 66 |
// viewMode is of the form TYPE << 16 | ID |
| 67 |
VIEW_TYPE type = (VIEW_TYPE)(viewMode >> 16); |
| 68 |
int id = viewMode & 0xffff; |
| 69 |
|
| 70 |
// first find a view that matches this view, if possible... |
| 71 |
int newView = GetView(type, id); |
| 72 |
if (newView < 0) // no suitable view that matches both id and type, so try just type |
| 73 |
newView = GetView(type, 0); |
| 74 |
if (newView < 0 && type == VIEW_TYPE_BIG_ICON) // try icon view if they want big icon |
| 75 |
newView = GetView(VIEW_TYPE_ICON, 0); |
| 76 |
if (newView < 0 && type == VIEW_TYPE_BIG_INFO) |
| 77 |
newView = GetView(VIEW_TYPE_INFO, 0); |
| 78 |
if (newView < 0) // try a list view |
| 79 |
newView = GetView(VIEW_TYPE_LIST, 0); |
| 80 |
if (newView < 0) // try anything! |
| 81 |
newView = GetView(VIEW_TYPE_NONE, 0); |
| 82 |
|
| 83 |
if (newView < 0) |
| 84 |
return; |
| 85 |
|
| 86 |
m_currentView = newView; |
| 87 |
CGUIControl *pNewView = m_visibleViews[m_currentView]; |
| 88 |
|
| 89 |
// make only current control visible... |
| 90 |
for (ciViews view = m_allViews.begin(); view != m_allViews.end(); ++view) |
| 91 |
(*view)->SetVisible(false); |
| 92 |
pNewView->SetVisible(true); |
| 93 |
|
| 94 |
if (!bRefresh && pNewView == previousView) |
| 95 |
return; // no need to actually update anything (other than visibility above) |
| 96 |
|
| 97 |
// CLog::Log(LOGDEBUG,"SetCurrentView: Oldview: {}, Newview :{}", m_currentView, viewMode); |
| 98 |
|
| 99 |
bool hasFocus(false); |
| 100 |
int item = -1; |
| 101 |
if (previousView) |
| 102 |
{ // have an old view - let's clear it out and hide it. |
| 103 |
hasFocus = previousView->HasFocus(); |
| 104 |
item = GetSelectedItem(previousView); |
| 105 |
CGUIMessage msg(GUI_MSG_LABEL_RESET, m_parentWindow, previousView->GetID()); |
| 106 |
previousView->OnMessage(msg); |
| 107 |
} |
| 108 |
|
| 109 |
// Update it with the contents |
| 110 |
UpdateContents(pNewView, item); |
| 111 |
|
| 112 |
// and focus if necessary |
| 113 |
if (hasFocus) |
| 114 |
{ |
| 115 |
CGUIMessage msg(GUI_MSG_SETFOCUS, m_parentWindow, pNewView->GetID(), 0); |
| 116 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg, m_parentWindow); |
| 117 |
} |
| 118 |
|
| 119 |
UpdateViewAsControl(static_cast<IGUIContainer*>(pNewView)->GetLabel()); |
| 120 |
} |
| 121 |
|
| 122 |
void CGUIViewControl::SetItems(CFileItemList &items) |
| 123 |
{ |
| 124 |
// CLog::Log(LOGDEBUG,"SetItems: {}", m_currentView); |
| 125 |
m_fileItems = &items; |
| 126 |
// update our current view control... |
| 127 |
UpdateView(); |
| 128 |
} |
| 129 |
|
| 130 |
void CGUIViewControl::UpdateContents(const CGUIControl *control, int currentItem) const |
| 131 |
{ |
| 132 |
if (!control || !m_fileItems) return; |
| 133 |
CGUIMessage msg(GUI_MSG_LABEL_BIND, m_parentWindow, control->GetID(), currentItem, 0, m_fileItems); |
| 134 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg, m_parentWindow); |
| 135 |
} |
| 136 |
|
| 137 |
void CGUIViewControl::UpdateView() |
| 138 |
{ |
| 139 |
// CLog::Log(LOGDEBUG,"UpdateView: {}", m_currentView); |
| 140 |
if (m_currentView < 0 || m_currentView >= (int)m_visibleViews.size()) |
| 141 |
return; // no valid current view! |
| 142 |
|
| 143 |
CGUIControl *pControl = m_visibleViews[m_currentView]; |
| 144 |
// get the currently selected item |
| 145 |
int item = GetSelectedItem(pControl); |
| 146 |
UpdateContents(pControl, item < 0 ? 0 : item); |
| 147 |
} |
| 148 |
|
| 149 |
int CGUIViewControl::GetSelectedItem(const CGUIControl *control) const |
| 150 |
{ |
| 151 |
if (!control || !m_fileItems) |
| 152 |
return -1; |
| 153 |
|
| 154 |
CGUIMessage msg(GUI_MSG_ITEM_SELECTED, m_parentWindow, control->GetID()); |
| 155 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg, m_parentWindow); |
| 156 |
|
| 157 |
int iItem = msg.GetParam1(); |
| 158 |
if (iItem >= m_fileItems->Size()) |
| 159 |
return -1; |
| 160 |
|
| 161 |
return iItem; |
| 162 |
} |
| 163 |
|
| 164 |
int CGUIViewControl::GetSelectedItem() const |
| 165 |
{ |
| 166 |
if (m_currentView < 0 || m_currentView >= (int)m_visibleViews.size()) |
| 167 |
return -1; // no valid current view! |
| 168 |
|
| 169 |
return GetSelectedItem(m_visibleViews[m_currentView]); |
| 170 |
} |
| 171 |
|
| 172 |
std::string CGUIViewControl::GetSelectedItemPath() const |
| 173 |
{ |
| 174 |
if (m_currentView < 0 || (size_t)m_currentView >= m_visibleViews.size()) |
| 175 |
return ""; |
| 176 |
|
| 177 |
int selectedItem = GetSelectedItem(m_visibleViews[m_currentView]); |
| 178 |
if (selectedItem > -1) |
| 179 |
{ |
| 180 |
CFileItemPtr fileItem = m_fileItems->Get(selectedItem); |
| 181 |
if (fileItem) |
| 182 |
return fileItem->GetPath(); |
| 183 |
} |
| 184 |
|
| 185 |
return ""; |
| 186 |
} |
| 187 |
|
| 188 |
void CGUIViewControl::SetSelectedItem(int item) |
| 189 |
{ |
| 190 |
if (!m_fileItems || item < 0 || item >= m_fileItems->Size()) |
| 191 |
return; |
| 192 |
|
| 193 |
if (m_currentView < 0 || m_currentView >= (int)m_visibleViews.size()) |
| 194 |
return; // no valid current view! |
| 195 |
|
| 196 |
CGUIMessage msg(GUI_MSG_ITEM_SELECT, m_parentWindow, m_visibleViews[m_currentView]->GetID(), item); |
| 197 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg, m_parentWindow); |
| 198 |
} |
| 199 |
|
| 200 |
void CGUIViewControl::SetSelectedItem(const std::string &itemPath) |
| 201 |
{ |
| 202 |
if (!m_fileItems || itemPath.empty()) |
| 203 |
return; |
| 204 |
|
| 205 |
std::string comparePath(itemPath); |
| 206 |
URIUtils::RemoveSlashAtEnd(comparePath); |
| 207 |
|
| 208 |
int item = -1; |
| 209 |
for (int i = 0; i < m_fileItems->Size(); ++i) |
| 210 |
{ |
| 211 |
std::string strPath =(*m_fileItems)[i]->GetPath(); |
| 212 |
URIUtils::RemoveSlashAtEnd(strPath); |
| 213 |
if (strPath == comparePath) |
| 214 |
{ |
| 215 |
item = i; |
| 216 |
break; |
| 217 |
} |
| 218 |
} |
| 219 |
SetSelectedItem(item); |
| 220 |
} |
| 221 |
|
| 222 |
void CGUIViewControl::SetFocused() |
| 223 |
{ |
| 224 |
if (m_currentView < 0 || m_currentView >= (int)m_visibleViews.size()) |
| 225 |
return; // no valid current view! |
| 226 |
|
| 227 |
CGUIMessage msg(GUI_MSG_SETFOCUS, m_parentWindow, m_visibleViews[m_currentView]->GetID(), 0); |
| 228 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg, m_parentWindow); |
| 229 |
} |
| 230 |
|
| 231 |
bool CGUIViewControl::HasControl(int viewControlID) const |
| 232 |
{ |
| 233 |
// run through our controls, checking for the id |
| 234 |
for (ciViews it = m_allViews.begin(); it != m_allViews.end(); ++it) |
| 235 |
{ |
| 236 |
if ((*it)->GetID() == viewControlID) |
| 237 |
return true; |
| 238 |
} |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
int CGUIViewControl::GetCurrentControl() const |
| 243 |
{ |
| 244 |
if (m_currentView < 0 || m_currentView >= (int)m_visibleViews.size()) |
| 245 |
return -1; // no valid current view! |
| 246 |
|
| 247 |
return m_visibleViews[m_currentView]->GetID(); |
| 248 |
} |
| 249 |
|
| 250 |
// returns the number-th view's viewmode (type and id) |
| 251 |
int CGUIViewControl::GetViewModeNumber(int number) const |
| 252 |
{ |
| 253 |
IGUIContainer *nextView = nullptr; |
| 254 |
if (number >= 0 && number < (int)m_visibleViews.size()) |
| 255 |
nextView = static_cast<IGUIContainer*>(m_visibleViews[number]); |
| 256 |
else if (m_visibleViews.size()) |
| 257 |
nextView = static_cast<IGUIContainer*>(m_visibleViews[0]); |
| 258 |
if (nextView) |
| 259 |
return (nextView->GetType() << 16) | nextView->GetID(); |
| 260 |
return 0; // no view modes :( |
| 261 |
} |
| 262 |
|
| 263 |
// returns the amount of visible views |
| 264 |
int CGUIViewControl::GetViewModeCount() const |
| 265 |
{ |
| 266 |
return static_cast<int>(m_visibleViews.size()); |
| 267 |
} |
| 268 |
|
| 269 |
int CGUIViewControl::GetViewModeByID(int id) const |
| 270 |
{ |
| 271 |
for (unsigned int i = 0; i < m_visibleViews.size(); ++i) |
| 272 |
{ |
| 273 |
IGUIContainer *view = static_cast<IGUIContainer*>(m_visibleViews[i]); |
| 274 |
if (view->GetID() == id) |
| 275 |
return (view->GetType() << 16) | view->GetID(); |
| 276 |
} |
| 277 |
return 0; // no view modes :( |
| 278 |
} |
| 279 |
|
| 280 |
// returns the next viewmode in the cycle |
| 281 |
int CGUIViewControl::GetNextViewMode(int direction) const |
| 282 |
{ |
| 283 |
if (!m_visibleViews.size()) |
| 284 |
return 0; // no view modes :( |
| 285 |
|
| 286 |
int viewNumber = (m_currentView + direction) % (int)m_visibleViews.size(); |
| 287 |
if (viewNumber < 0) viewNumber += m_visibleViews.size(); |
| 288 |
IGUIContainer *nextView = static_cast<IGUIContainer*>(m_visibleViews[viewNumber]); |
| 289 |
return (nextView->GetType() << 16) | nextView->GetID(); |
| 290 |
} |
| 291 |
|
| 292 |
void CGUIViewControl::Clear() |
| 293 |
{ |
| 294 |
if (m_currentView < 0 || m_currentView >= (int)m_visibleViews.size()) |
| 295 |
return; // no valid current view! |
| 296 |
|
| 297 |
CGUIMessage msg(GUI_MSG_LABEL_RESET, m_parentWindow, m_visibleViews[m_currentView]->GetID(), 0); |
| 298 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg, m_parentWindow); |
| 299 |
} |
| 300 |
|
| 301 |
int CGUIViewControl::GetView(VIEW_TYPE type, int id) const |
| 302 |
{ |
| 303 |
for (int i = 0; i < (int)m_visibleViews.size(); i++) |
| 304 |
{ |
| 305 |
IGUIContainer *view = static_cast<IGUIContainer*>(m_visibleViews[i]); |
| 306 |
if ((type == VIEW_TYPE_NONE || type == view->GetType()) && (!id || view->GetID() == id)) |
| 307 |
return i; |
| 308 |
} |
| 309 |
return -1; |
| 310 |
} |
| 311 |
|
| 312 |
void CGUIViewControl::UpdateViewAsControl(const std::string &viewLabel) |
| 313 |
{ |
| 314 |
// the view as control could be a select/spin/dropdown button |
| 315 |
std::vector< std::pair<std::string, int> > labels; |
| 316 |
for (unsigned int i = 0; i < m_visibleViews.size(); i++) |
| 317 |
{ |
| 318 |
IGUIContainer *view = static_cast<IGUIContainer*>(m_visibleViews[i]); |
| 319 |
std::string label = StringUtils::Format(g_localizeStrings.Get(534), |
| 320 |
view->GetLabel()); // View: {} |
| 321 |
labels.emplace_back(std::move(label), i); |
| 322 |
} |
| 323 |
CGUIMessage msg(GUI_MSG_SET_LABELS, m_parentWindow, m_viewAsControl, m_currentView); |
| 324 |
msg.SetPointer(&labels); |
| 325 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg, m_parentWindow); |
| 326 |
|
| 327 |
// otherwise it's just a normal button |
| 328 |
std::string label = StringUtils::Format(g_localizeStrings.Get(534), viewLabel); // View: {} |
| 329 |
CGUIMessage msgSet(GUI_MSG_LABEL_SET, m_parentWindow, m_viewAsControl); |
| 330 |
msgSet.SetLabel(label); |
| 331 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msgSet, m_parentWindow); |
| 332 |
} |
| 333 |
|
| 334 |
void CGUIViewControl::UpdateViewVisibility() |
| 335 |
{ |
| 336 |
// first reset our infomanager cache, as it's likely that the vis conditions |
| 337 |
// used for views (i.e. based on contenttype) may have changed |
| 338 |
CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager(); |
| 339 |
infoMgr.ResetCache(); |
| 340 |
infoMgr.GetInfoProviders().GetGUIControlsInfoProvider().ResetContainerMovingCache(); |
| 341 |
m_visibleViews.clear(); |
| 342 |
for (unsigned int i = 0; i < m_allViews.size(); i++) |
| 343 |
{ |
| 344 |
CGUIControl *view = m_allViews[i]; |
| 345 |
if (view->HasVisibleCondition()) |
| 346 |
{ |
| 347 |
view->UpdateVisibility(nullptr); |
| 348 |
if (view->IsVisibleFromSkin()) |
| 349 |
m_visibleViews.push_back(view); |
| 350 |
} |
| 351 |
else |
| 352 |
m_visibleViews.push_back(view); |
| 353 |
} |
| 354 |
} |