Line data Source code
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 "svtools/treelistentry.hxx"
21 : #include "svtools/treelist.hxx"
22 :
23 : #include <limits>
24 :
25 : size_t SvTreeListEntry::ITEM_NOT_FOUND = std::numeric_limits<size_t>::max();
26 :
27 0 : void SvTreeListEntry::ClearChildren()
28 : {
29 0 : maChildren.clear();
30 0 : }
31 :
32 0 : void SvTreeListEntry::SetListPositions()
33 : {
34 0 : SvTreeListEntries::iterator it = maChildren.begin(), itEnd = maChildren.end();
35 0 : sal_uLong nCur = 0;
36 0 : for (; it != itEnd; ++it)
37 : {
38 0 : SvTreeListEntry& rEntry = *it;
39 0 : rEntry.nListPos &= 0x80000000;
40 0 : rEntry.nListPos |= nCur;
41 0 : ++nCur;
42 : }
43 :
44 0 : nListPos &= (~0x80000000); // remove the invalid bit.
45 0 : }
46 :
47 0 : void SvTreeListEntry::InvalidateChildrensListPositions()
48 : {
49 0 : nListPos |= 0x80000000;
50 0 : }
51 :
52 0 : SvTreeListEntry::SvTreeListEntry() :
53 : pParent(NULL),
54 : nAbsPos(0),
55 : nListPos(0),
56 : pUserData(NULL),
57 0 : nEntryFlags(0)
58 : {
59 0 : }
60 :
61 0 : SvTreeListEntry::SvTreeListEntry(const SvTreeListEntry& r) :
62 : pParent(NULL),
63 : nAbsPos(r.nAbsPos),
64 0 : nListPos(r.nListPos & 0x7FFFFFFF)
65 : {
66 0 : SvTreeListEntries::const_iterator it = r.maChildren.begin(), itEnd = r.maChildren.end();
67 0 : for (; it != itEnd; ++it)
68 0 : maChildren.push_back(new SvTreeListEntry(*it));
69 0 : }
70 :
71 0 : SvTreeListEntry::~SvTreeListEntry()
72 : {
73 : #ifdef DBG_UTIL
74 : pParent = 0;
75 : #endif
76 :
77 0 : maChildren.clear();
78 0 : maItems.clear();
79 0 : }
80 :
81 0 : bool SvTreeListEntry::HasChildren() const
82 : {
83 0 : return !maChildren.empty();
84 : }
85 :
86 0 : bool SvTreeListEntry::HasChildListPos() const
87 : {
88 0 : if( pParent && !(pParent->nListPos & 0x80000000) )
89 0 : return true;
90 0 : else return false;
91 : }
92 :
93 0 : sal_uLong SvTreeListEntry::GetChildListPos() const
94 : {
95 0 : if( pParent && (pParent->nListPos & 0x80000000) )
96 0 : pParent->SetListPositions();
97 0 : return ( nListPos & 0x7fffffff );
98 : }
99 :
100 0 : SvTreeListEntries& SvTreeListEntry::GetChildEntries()
101 : {
102 0 : return maChildren;
103 : }
104 :
105 0 : const SvTreeListEntries& SvTreeListEntry::GetChildEntries() const
106 : {
107 0 : return maChildren;
108 : }
109 :
110 0 : void SvTreeListEntry::Clone(SvTreeListEntry* pSource)
111 : {
112 0 : nListPos &= 0x80000000;
113 0 : nListPos |= ( pSource->nListPos & 0x7fffffff);
114 0 : nAbsPos = pSource->nAbsPos;
115 :
116 : SvLBoxItem* pNewItem;
117 0 : maItems.clear();
118 0 : ItemsType::iterator it = pSource->maItems.begin(), itEnd = pSource->maItems.end();
119 0 : for (; it != itEnd; ++it)
120 : {
121 0 : SvLBoxItem* pItem = &(*it);
122 0 : pNewItem = pItem->Create();
123 0 : pNewItem->Clone(pItem);
124 0 : maItems.push_back(pNewItem);
125 : }
126 :
127 0 : pUserData = pSource->GetUserData();
128 0 : nEntryFlags = pSource->nEntryFlags;
129 0 : }
130 :
131 0 : size_t SvTreeListEntry::ItemCount() const
132 : {
133 0 : return maItems.size();
134 : }
135 :
136 0 : void SvTreeListEntry::AddItem( SvLBoxItem* pItem )
137 : {
138 0 : maItems.push_back( pItem );
139 0 : }
140 :
141 0 : void SvTreeListEntry::EnableChildrenOnDemand( bool bEnable )
142 : {
143 0 : if ( bEnable )
144 0 : nEntryFlags |= SV_ENTRYFLAG_CHILDREN_ON_DEMAND;
145 : else
146 0 : nEntryFlags &= (~SV_ENTRYFLAG_CHILDREN_ON_DEMAND);
147 0 : }
148 :
149 0 : void SvTreeListEntry::ReplaceItem( SvLBoxItem* pNewItem, size_t nPos )
150 : {
151 : DBG_ASSERT(pNewItem,"ReplaceItem:No Item");
152 0 : if (nPos >= maItems.size())
153 : {
154 : // Out of bound. Bail out.
155 0 : delete pNewItem;
156 0 : return;
157 : }
158 :
159 0 : maItems.erase(maItems.begin()+nPos);
160 0 : maItems.insert(maItems.begin()+nPos, pNewItem);
161 : }
162 :
163 0 : const SvLBoxItem* SvTreeListEntry::GetItem( size_t nPos ) const
164 : {
165 0 : return &maItems[nPos];
166 : }
167 :
168 0 : SvLBoxItem* SvTreeListEntry::GetItem( size_t nPos )
169 : {
170 0 : return &maItems[nPos];
171 : }
172 :
173 : namespace {
174 :
175 : class FindByType : std::unary_function<SvLBoxItem, void>
176 : {
177 : sal_uInt16 mnId;
178 : public:
179 0 : FindByType(sal_uInt16 nId) : mnId(nId) {}
180 0 : bool operator() (const SvLBoxItem& rItem) const
181 : {
182 0 : return rItem.GetType() == mnId;
183 : }
184 : };
185 :
186 : class FindByPointer : std::unary_function<SvLBoxItem, void>
187 : {
188 : const SvLBoxItem* mpItem;
189 : public:
190 0 : FindByPointer(const SvLBoxItem* p) : mpItem(p) {}
191 0 : bool operator() (const SvLBoxItem& rItem) const
192 : {
193 0 : return &rItem == mpItem;
194 : }
195 : };
196 :
197 : }
198 :
199 0 : const SvLBoxItem* SvTreeListEntry::GetFirstItem( sal_uInt16 nId ) const
200 : {
201 0 : ItemsType::const_iterator it = std::find_if(maItems.begin(), maItems.end(), FindByType(nId));
202 0 : return it == maItems.end() ? NULL : &(*it);
203 : }
204 :
205 0 : SvLBoxItem* SvTreeListEntry::GetFirstItem( sal_uInt16 nId )
206 : {
207 0 : ItemsType::iterator it = std::find_if(maItems.begin(), maItems.end(), FindByType(nId));
208 0 : return it == maItems.end() ? NULL : &(*it);
209 : }
210 :
211 0 : size_t SvTreeListEntry::GetPos( const SvLBoxItem* pItem ) const
212 : {
213 0 : ItemsType::const_iterator it = std::find_if(maItems.begin(), maItems.end(), FindByPointer(pItem));
214 0 : return it == maItems.end() ? ITEM_NOT_FOUND : std::distance(maItems.begin(), it);
215 : }
216 :
217 0 : void* SvTreeListEntry::GetUserData() const
218 : {
219 0 : return pUserData;
220 : }
221 :
222 0 : void SvTreeListEntry::SetUserData( void* pPtr )
223 : {
224 0 : pUserData = pPtr;
225 0 : }
226 :
227 0 : bool SvTreeListEntry::HasChildrenOnDemand() const
228 : {
229 0 : return (bool)((nEntryFlags & SV_ENTRYFLAG_CHILDREN_ON_DEMAND)!=0);
230 : }
231 :
232 0 : bool SvTreeListEntry::HasInUseEmphasis() const
233 : {
234 0 : return (bool)((nEntryFlags & SV_ENTRYFLAG_IN_USE)!=0);
235 : }
236 :
237 0 : sal_uInt16 SvTreeListEntry::GetFlags() const
238 : {
239 0 : return nEntryFlags;
240 : }
241 :
242 0 : void SvTreeListEntry::SetFlags( sal_uInt16 nFlags )
243 : {
244 0 : nEntryFlags = nFlags;
245 0 : }
246 :
247 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|