Bump version to 5.0-14
[LibreOffice.git] / svtools / source / contnr / treelistentry.cxx
blob3b33cf788068f241706db64f6164d470d06a0855
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 #include <vcl/svapp.hxx>
24 #include <vcl/settings.hxx>
26 #include <limits>
28 void SvTreeListEntry::ClearChildren()
30 maChildren.clear();
33 void SvTreeListEntry::SetListPositions()
35 SvTreeListEntries::iterator it = maChildren.begin(), itEnd = maChildren.end();
36 sal_uLong nCur = 0;
37 for (; it != itEnd; ++it)
39 SvTreeListEntry& rEntry = *it;
40 rEntry.nListPos &= 0x80000000;
41 rEntry.nListPos |= nCur;
42 ++nCur;
45 nListPos &= (~0x80000000); // remove the invalid bit.
48 void SvTreeListEntry::InvalidateChildrensListPositions()
50 nListPos |= 0x80000000;
53 SvTreeListEntry::SvTreeListEntry()
54 : pParent(NULL)
55 , nAbsPos(0)
56 , nListPos(0)
57 , bIsMarked(false)
58 , pUserData(NULL)
59 , nEntryFlags(SvTLEntryFlags::NONE)
60 , maBackColor(Application::GetSettings().GetStyleSettings().GetWindowColor())
64 SvTreeListEntry::SvTreeListEntry(const SvTreeListEntry& r)
65 : pParent(NULL)
66 , nAbsPos(r.nAbsPos)
67 , nListPos(r.nListPos & 0x7FFFFFFF)
68 , bIsMarked(r.bIsMarked)
69 , pUserData(r.pUserData)
70 , nEntryFlags(r.nEntryFlags)
71 , maBackColor(Application::GetSettings().GetStyleSettings().GetWindowColor())
73 SvTreeListEntries::const_iterator it = r.maChildren.begin(), itEnd = r.maChildren.end();
74 for (; it != itEnd; ++it)
75 maChildren.push_back(new SvTreeListEntry(*it));
78 SvTreeListEntry::~SvTreeListEntry()
80 #ifdef DBG_UTIL
81 pParent = 0;
82 #endif
84 maChildren.clear();
85 maItems.clear();
88 bool SvTreeListEntry::HasChildren() const
90 return !maChildren.empty();
93 bool SvTreeListEntry::HasChildListPos() const
95 if( pParent && !(pParent->nListPos & 0x80000000) )
96 return true;
97 else return false;
100 sal_uLong SvTreeListEntry::GetChildListPos() const
102 if( pParent && (pParent->nListPos & 0x80000000) )
103 pParent->SetListPositions();
104 return ( nListPos & 0x7fffffff );
109 void SvTreeListEntry::Clone(SvTreeListEntry* pSource)
111 nListPos &= 0x80000000;
112 nListPos |= ( pSource->nListPos & 0x7fffffff);
113 nAbsPos = pSource->nAbsPos;
115 maItems.clear();
116 ItemsType::iterator it = pSource->maItems.begin(), itEnd = pSource->maItems.end();
117 for (; it != itEnd; ++it)
119 SvLBoxItem* pItem = &(*it);
120 SvLBoxItem* pNewItem = pItem->Create();
121 pNewItem->Clone(pItem);
122 maItems.push_back(pNewItem);
125 pUserData = pSource->GetUserData();
126 nEntryFlags = pSource->nEntryFlags;
129 size_t SvTreeListEntry::ItemCount() const
131 return maItems.size();
134 void SvTreeListEntry::AddItem( SvLBoxItem* pItem )
136 maItems.push_back( pItem );
139 void SvTreeListEntry::EnableChildrenOnDemand( bool bEnable )
141 if ( bEnable )
142 nEntryFlags |= SvTLEntryFlags::CHILDREN_ON_DEMAND;
143 else
144 nEntryFlags &= (~SvTLEntryFlags::CHILDREN_ON_DEMAND);
147 void SvTreeListEntry::ReplaceItem( SvLBoxItem* pNewItem, size_t nPos )
149 DBG_ASSERT(pNewItem,"ReplaceItem:No Item");
150 if (nPos >= maItems.size())
152 // Out of bound. Bail out.
153 delete pNewItem;
154 return;
157 maItems.erase(maItems.begin()+nPos);
158 maItems.insert(maItems.begin()+nPos, pNewItem);
161 const SvLBoxItem* SvTreeListEntry::GetItem( size_t nPos ) const
163 return &maItems[nPos];
166 SvLBoxItem* SvTreeListEntry::GetItem( size_t nPos )
168 return &maItems[nPos];
171 namespace {
173 class FindByType : std::unary_function<SvLBoxItem, void>
175 sal_uInt16 mnId;
176 public:
177 FindByType(sal_uInt16 nId) : mnId(nId) {}
178 bool operator() (const SvLBoxItem& rItem) const
180 return rItem.GetType() == mnId;
184 class FindByPointer : std::unary_function<SvLBoxItem, void>
186 const SvLBoxItem* mpItem;
187 public:
188 FindByPointer(const SvLBoxItem* p) : mpItem(p) {}
189 bool operator() (const SvLBoxItem& rItem) const
191 return &rItem == mpItem;
197 const SvLBoxItem* SvTreeListEntry::GetFirstItem( sal_uInt16 nId ) const
199 ItemsType::const_iterator it = std::find_if(maItems.begin(), maItems.end(), FindByType(nId));
200 return it == maItems.end() ? NULL : &(*it);
203 SvLBoxItem* SvTreeListEntry::GetFirstItem( sal_uInt16 nId )
205 ItemsType::iterator it = std::find_if(maItems.begin(), maItems.end(), FindByType(nId));
206 return it == maItems.end() ? NULL : &(*it);
209 size_t SvTreeListEntry::GetPos( const SvLBoxItem* pItem ) const
211 ItemsType::const_iterator it = std::find_if(maItems.begin(), maItems.end(), FindByPointer(pItem));
212 return it == maItems.end() ? ITEM_NOT_FOUND : std::distance(maItems.begin(), it);
216 void SvTreeListEntry::SetUserData( void* pPtr )
218 pUserData = pPtr;
221 bool SvTreeListEntry::HasChildrenOnDemand() const
223 return (bool)(nEntryFlags & SvTLEntryFlags::CHILDREN_ON_DEMAND);
226 bool SvTreeListEntry::HasInUseEmphasis() const
228 return (bool)(nEntryFlags & SvTLEntryFlags::IN_USE);
232 void SvTreeListEntry::SetFlags( SvTLEntryFlags nFlags )
234 nEntryFlags = nFlags;
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */