summaryrefslogtreecommitdiffstats
path: root/xbmc/guilib/GUIControlLookup.cpp
blob: 8c572bb331dc1adee1823d87eb34874c0f8e086b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GUIControlLookup.h"

CGUIControlLookup::CGUIControlLookup(const CGUIControlLookup& from) : CGUIControl(from)
{
}

CGUIControl *CGUIControlLookup::GetControl(int iControl, std::vector<CGUIControl*> *idCollector)
{
  if (idCollector)
    idCollector->clear();

  CGUIControl* pPotential(nullptr);

  LookupMap::const_iterator first = m_lookup.find(iControl);
  if (first != m_lookup.end())
  {
    LookupMap::const_iterator last = m_lookup.upper_bound(iControl);
    for (LookupMap::const_iterator i = first; i != last; ++i)
    {
      CGUIControl *control = i->second;
      if (control->IsVisible())
        return control;
      else if (idCollector)
        idCollector->push_back(control);
      else if (!pPotential)
        pPotential = control;
    }
  }
  return pPotential;
}

bool CGUIControlLookup::IsValidControl(const CGUIControl *control) const
{
  if (control->GetID())
  {
    for (const auto &i : m_lookup)
    {
      if (control == i.second)
        return true;
    }
  }
  return false;
}

void CGUIControlLookup::AddLookup(CGUIControl *control)
{
  CGUIControlLookup *lookupControl(dynamic_cast<CGUIControlLookup*>(control));

  if (lookupControl)
  { // first add all the subitems of this group (if they exist)
    const LookupMap &map(lookupControl->GetLookup());
    for (const auto &i : map)
      m_lookup.insert(m_lookup.upper_bound(i.first), std::make_pair(i.first, i.second));
  }
  if (control->GetID())
    m_lookup.insert(m_lookup.upper_bound(control->GetID()), std::make_pair(control->GetID(), control));
  // ensure that our size is what it should be
  if (m_parentControl && (lookupControl = dynamic_cast<CGUIControlLookup*>(m_parentControl)))
    lookupControl->AddLookup(control);
}

void CGUIControlLookup::RemoveLookup(CGUIControl *control)
{
  CGUIControlLookup *lookupControl(dynamic_cast<CGUIControlLookup*>(control));
  if (lookupControl)
  { // remove the group's lookup
    const LookupMap &map(lookupControl->GetLookup());
    for (const auto &i : map)
    { // remove this control
      for (LookupMap::iterator it = m_lookup.begin(); it != m_lookup.end(); ++it)
      {
        if (i.second == it->second)
        {
          m_lookup.erase(it);
          break;
        }
      }
    }
  }
  // remove the actual control
  if (control->GetID())
  {
    for (LookupMap::iterator it = m_lookup.begin(); it != m_lookup.end(); ++it)
    {
      if (control == it->second)
      {
        m_lookup.erase(it);
        break;
      }
    }
  }
  if (m_parentControl && (lookupControl = dynamic_cast<CGUIControlLookup*>(m_parentControl)))
    lookupControl->RemoveLookup(control);
}

void CGUIControlLookup::RemoveLookup()
{
  CGUIControlLookup *lookupControl;
  if (m_parentControl && (lookupControl = dynamic_cast<CGUIControlLookup*>(m_parentControl)))
  {
    const LookupMap map(m_lookup);
    for (const auto &i : map)
      lookupControl->RemoveLookup(i.second);
  }
}