1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "svtools/treelistentry.hxx"
21 #include "svtools/treelist.hxx"
25 size_t SvTreeListEntry::ITEM_NOT_FOUND
= std::numeric_limits
<size_t>::max();
27 void SvTreeListEntry::ClearChildren()
32 void SvTreeListEntry::SetListPositions()
34 SvTreeListEntries::iterator it
= maChildren
.begin(), itEnd
= maChildren
.end();
36 for (; it
!= itEnd
; ++it
)
38 SvTreeListEntry
& rEntry
= *it
;
39 rEntry
.nListPos
&= 0x80000000;
40 rEntry
.nListPos
|= nCur
;
44 nListPos
&= (~0x80000000); // remove the invalid bit.
47 void SvTreeListEntry::InvalidateChildrensListPositions()
49 nListPos
|= 0x80000000;
52 SvTreeListEntry::SvTreeListEntry() :
61 SvTreeListEntry::SvTreeListEntry(const SvTreeListEntry
& r
) :
64 nListPos(r
.nListPos
& 0x7FFFFFFF)
66 SvTreeListEntries::const_iterator it
= r
.maChildren
.begin(), itEnd
= r
.maChildren
.end();
67 for (; it
!= itEnd
; ++it
)
68 maChildren
.push_back(new SvTreeListEntry(*it
));
71 SvTreeListEntry::~SvTreeListEntry()
81 bool SvTreeListEntry::HasChildren() const
83 return !maChildren
.empty();
86 bool SvTreeListEntry::HasChildListPos() const
88 if( pParent
&& !(pParent
->nListPos
& 0x80000000) )
93 sal_uLong
SvTreeListEntry::GetChildListPos() const
95 if( pParent
&& (pParent
->nListPos
& 0x80000000) )
96 pParent
->SetListPositions();
97 return ( nListPos
& 0x7fffffff );
100 SvTreeListEntries
& SvTreeListEntry::GetChildEntries()
105 const SvTreeListEntries
& SvTreeListEntry::GetChildEntries() const
110 void SvTreeListEntry::Clone(SvTreeListEntry
* pSource
)
112 nListPos
&= 0x80000000;
113 nListPos
|= ( pSource
->nListPos
& 0x7fffffff);
114 nAbsPos
= pSource
->nAbsPos
;
116 SvLBoxItem
* pNewItem
;
118 ItemsType::iterator it
= pSource
->maItems
.begin(), itEnd
= pSource
->maItems
.end();
119 for (; it
!= itEnd
; ++it
)
121 SvLBoxItem
* pItem
= &(*it
);
122 pNewItem
= pItem
->Create();
123 pNewItem
->Clone(pItem
);
124 maItems
.push_back(pNewItem
);
127 pUserData
= pSource
->GetUserData();
128 nEntryFlags
= pSource
->nEntryFlags
;
131 size_t SvTreeListEntry::ItemCount() const
133 return maItems
.size();
136 void SvTreeListEntry::AddItem( SvLBoxItem
* pItem
)
138 maItems
.push_back( pItem
);
141 void SvTreeListEntry::EnableChildrenOnDemand( bool bEnable
)
144 nEntryFlags
|= SV_ENTRYFLAG_CHILDREN_ON_DEMAND
;
146 nEntryFlags
&= (~SV_ENTRYFLAG_CHILDREN_ON_DEMAND
);
149 void SvTreeListEntry::ReplaceItem( SvLBoxItem
* pNewItem
, size_t nPos
)
151 DBG_ASSERT(pNewItem
,"ReplaceItem:No Item");
152 if (nPos
>= maItems
.size())
154 // Out of bound. Bail out.
159 maItems
.erase(maItems
.begin()+nPos
);
160 maItems
.insert(maItems
.begin()+nPos
, pNewItem
);
163 const SvLBoxItem
* SvTreeListEntry::GetItem( size_t nPos
) const
165 return &maItems
[nPos
];
168 SvLBoxItem
* SvTreeListEntry::GetItem( size_t nPos
)
170 return &maItems
[nPos
];
175 class FindByType
: std::unary_function
<SvLBoxItem
, void>
179 FindByType(sal_uInt16 nId
) : mnId(nId
) {}
180 bool operator() (const SvLBoxItem
& rItem
) const
182 return rItem
.GetType() == mnId
;
186 class FindByPointer
: std::unary_function
<SvLBoxItem
, void>
188 const SvLBoxItem
* mpItem
;
190 FindByPointer(const SvLBoxItem
* p
) : mpItem(p
) {}
191 bool operator() (const SvLBoxItem
& rItem
) const
193 return &rItem
== mpItem
;
199 const SvLBoxItem
* SvTreeListEntry::GetFirstItem( sal_uInt16 nId
) const
201 ItemsType::const_iterator it
= std::find_if(maItems
.begin(), maItems
.end(), FindByType(nId
));
202 return it
== maItems
.end() ? NULL
: &(*it
);
205 SvLBoxItem
* SvTreeListEntry::GetFirstItem( sal_uInt16 nId
)
207 ItemsType::iterator it
= std::find_if(maItems
.begin(), maItems
.end(), FindByType(nId
));
208 return it
== maItems
.end() ? NULL
: &(*it
);
211 size_t SvTreeListEntry::GetPos( const SvLBoxItem
* pItem
) const
213 ItemsType::const_iterator it
= std::find_if(maItems
.begin(), maItems
.end(), FindByPointer(pItem
));
214 return it
== maItems
.end() ? ITEM_NOT_FOUND
: std::distance(maItems
.begin(), it
);
217 void* SvTreeListEntry::GetUserData() const
222 void SvTreeListEntry::SetUserData( void* pPtr
)
227 bool SvTreeListEntry::HasChildrenOnDemand() const
229 return (bool)((nEntryFlags
& SV_ENTRYFLAG_CHILDREN_ON_DEMAND
)!=0);
232 bool SvTreeListEntry::HasInUseEmphasis() const
234 return (bool)((nEntryFlags
& SV_ENTRYFLAG_IN_USE
)!=0);
237 sal_uInt16
SvTreeListEntry::GetFlags() const
242 void SvTreeListEntry::SetFlags( sal_uInt16 nFlags
)
244 nEntryFlags
= nFlags
;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */