LibreOffice Module vcl (master)  1
treelistentry.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <memory>
21 #include <vcl/treelistentry.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/settings.hxx>
24 #include <tools/debug.hxx>
25 
27 {
28  m_Children.clear();
29 }
30 
32 {
33  sal_uLong nCur = 0;
34  for (auto const& pEntry : m_Children)
35  {
36  SvTreeListEntry& rEntry = *pEntry;
37  rEntry.nListPos &= 0x80000000;
38  rEntry.nListPos |= nCur;
39  ++nCur;
40  }
41 
42  nListPos &= (~0x80000000); // remove the invalid bit.
43 }
44 
46 {
47  nListPos |= 0x80000000;
48 }
49 
51  : pParent(nullptr)
52  , nAbsPos(0)
53  , nListPos(0)
54  , pUserData(nullptr)
56  , maBackColor(Application::GetSettings().GetStyleSettings().GetWindowColor())
57 {
58 }
59 
61 {
62 #ifdef DBG_UTIL
63  pParent = nullptr;
64 #endif
65 
66  m_Children.clear();
67  m_Items.clear();
68 }
69 
71 {
72  return !m_Children.empty();
73 }
74 
76 {
77  return pParent && !(pParent->nListPos & 0x80000000);
78 }
79 
81 {
82  if( pParent && (pParent->nListPos & 0x80000000) )
84  return ( nListPos & 0x7fffffff );
85 }
86 
87 
89 {
90  nListPos &= 0x80000000;
91  nListPos |= ( pSource->nListPos & 0x7fffffff);
92  nAbsPos = pSource->nAbsPos;
93 
94  m_Items.clear();
95  for (auto const& it : pSource->m_Items)
96  {
97  SvLBoxItem* pItem = &(*it);
98  std::unique_ptr<SvLBoxItem> pNewItem(pItem->Clone(pItem));
99  m_Items.push_back(std::move(pNewItem));
100  }
101 
102  pUserData = pSource->GetUserData();
103  nEntryFlags = pSource->nEntryFlags;
104 }
105 
107 {
108  return m_Items.size();
109 }
110 
111 void SvTreeListEntry::AddItem(std::unique_ptr<SvLBoxItem> pItem)
112 {
113  m_Items.push_back(std::move(pItem));
114 }
115 
117 {
118  if ( bEnable )
120  else
122 }
123 
124 void SvTreeListEntry::ReplaceItem(std::unique_ptr<SvLBoxItem> pNewItem, size_t const nPos)
125 {
126  DBG_ASSERT(pNewItem,"ReplaceItem:No Item");
127  if (nPos >= m_Items.size())
128  {
129  // Out of bound. Bail out.
130  pNewItem.reset();
131  return;
132  }
133 
134  m_Items.erase(m_Items.begin()+nPos);
135  m_Items.insert(m_Items.begin()+nPos, std::move(pNewItem));
136 }
137 
138 const SvLBoxItem& SvTreeListEntry::GetItem( size_t nPos ) const
139 {
140  return *m_Items[nPos];
141 }
142 
144 {
145  return *m_Items[nPos];
146 }
147 
148 namespace {
149 
150 class FindByType
151 {
152  SvLBoxItemType const meType;
153 public:
154  explicit FindByType(SvLBoxItemType eType) : meType(eType) {}
155  bool operator() (const std::unique_ptr<SvLBoxItem>& rpItem) const
156  {
157  return rpItem->GetType() == meType;
158  }
159 };
160 
161 class FindByPointer
162 {
163  const SvLBoxItem* mpItem;
164 public:
165  explicit FindByPointer(const SvLBoxItem* p) : mpItem(p) {}
166  bool operator() (const std::unique_ptr<SvLBoxItem>& rpItem) const
167  {
168  return rpItem.get() == mpItem;
169  }
170 };
171 
172 }
173 
175 {
176  ItemsType::const_iterator it = std::find_if(m_Items.begin(), m_Items.end(), FindByType(eType));
177  return (it == m_Items.end()) ? nullptr : (*it).get();
178 }
179 
181 {
182  ItemsType::iterator it = std::find_if(m_Items.begin(), m_Items.end(), FindByType(eType));
183  return (it == m_Items.end()) ? nullptr : (*it).get();
184 }
185 
186 size_t SvTreeListEntry::GetPos( const SvLBoxItem* pItem ) const
187 {
188  ItemsType::const_iterator it = std::find_if(m_Items.begin(), m_Items.end(), FindByPointer(pItem));
189  return it == m_Items.end() ? ITEM_NOT_FOUND : std::distance(m_Items.begin(), it);
190 }
191 
192 
194 {
195  pUserData = pPtr;
196 }
197 
199 {
200  return static_cast<bool>(nEntryFlags & SvTLEntryFlags::CHILDREN_ON_DEMAND);
201 }
202 
204 {
205  nEntryFlags = nFlags;
206 }
207 
209 {
212  nPos++;
213  return (nPos < rList.size()) ? rList[nPos].get() : nullptr;
214 }
215 
217 {
220  if ( nPos == 0 )
221  return nullptr;
222  nPos--;
223  return rList[nPos].get();
224 }
225 
226 
228 {
229  SvTreeListEntries& rChildren = pParent->m_Children;
230  return (rChildren.empty()) ? nullptr : rChildren.back().get();
231 }
232 
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddItem(std::unique_ptr< SvLBoxItem > pItem)
static const size_t ITEM_NOT_FOUND
void SetUserData(void *pPtr)
virtual std::unique_ptr< SvLBoxItem > Clone(SvLBoxItem const *pSource) const =0
PrimitiveType meType
sal_uIntPtr sal_uLong
void SetFlags(SvTLEntryFlags nFlags)
NONE
SvTLEntryFlags
SvLBoxItemType
SvTreeListEntries m_Children
SvTreeListEntry * NextSibling() const
void InvalidateChildrensListPositions()
size_t ItemCount() const
#define DBG_ASSERT(sCon, aError)
SvTreeListEntry * PrevSibling() const
void Clone(SvTreeListEntry *pSource)
void * GetUserData() const
SvTLEntryFlags nEntryFlags
size_t GetPos(const SvLBoxItem *pItem) const
Base class used mainly for the LibreOffice Desktop class.
Definition: svapp.hxx:238
const SvLBoxItem & GetItem(size_t nPos) const
sal_uLong GetChildListPos() const
bool HasChildListPos() const
bool HasChildrenOnDemand() const
SvTreeListEntry * LastSibling() const
virtual ~SvTreeListEntry()
bool HasChildren() const
void ReplaceItem(std::unique_ptr< SvLBoxItem > pNewItem, size_t nPos)
sal_uLong nListPos
const SvLBoxItem * GetFirstItem(SvLBoxItemType eType) const
sal_Int32 nPos
SvTreeListEntry * pParent
std::vector< std::unique_ptr< SvTreeListEntry > > SvTreeListEntries
void EnableChildrenOnDemand(bool bEnable=true)