bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / svdraw / svdlayer.cxx
blob46092955266f3f63051d14612bb6301f8c2ff0d4
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 <com/sun/star/uno/Sequence.hxx>
22 #include <svx/svdlayer.hxx>
23 #include <svx/svdmodel.hxx>
24 #include "svdglob.hxx"
25 #include "svx/svdstr.hrc"
27 bool SetOfByte::IsEmpty() const
29 for(sal_uInt16 i(0); i < 32; i++)
31 if(aData[i] != 0)
32 return false;
35 return true;
38 void SetOfByte::operator&=(const SetOfByte& r2ndSet)
40 for(sal_uInt16 i(0); i < 32; i++)
42 aData[i] &= r2ndSet.aData[i];
46 void SetOfByte::operator|=(const SetOfByte& r2ndSet)
48 for(sal_uInt16 i(0); i < 32; i++)
50 aData[i] |= r2ndSet.aData[i];
54 /** initialize this set with a uno sequence of sal_Int8
56 void SetOfByte::PutValue( const com::sun::star::uno::Any & rAny )
58 com::sun::star::uno::Sequence< sal_Int8 > aSeq;
59 if( rAny >>= aSeq )
61 sal_Int16 nCount = (sal_Int16)aSeq.getLength();
62 if( nCount > 32 )
63 nCount = 32;
65 sal_Int16 nIndex;
66 for( nIndex = 0; nIndex < nCount; nIndex++ )
68 aData[nIndex] = static_cast<sal_uInt8>(aSeq[nIndex]);
71 for( ; nIndex < 32; nIndex++ )
73 aData[nIndex] = 0;
78 /** returns a uno sequence of sal_Int8
80 void SetOfByte::QueryValue( com::sun::star::uno::Any & rAny ) const
82 sal_Int16 nNumBytesSet = 0;
83 sal_Int16 nIndex;
84 for( nIndex = 31; nIndex >= 00; nIndex-- )
86 if( 0 != aData[nIndex] )
88 nNumBytesSet = nIndex + 1;
89 break;
93 com::sun::star::uno::Sequence< sal_Int8 > aSeq( nNumBytesSet );
95 for( nIndex = 0; nIndex < nNumBytesSet; nIndex++ )
97 aSeq[nIndex] = static_cast<sal_Int8>(aData[nIndex]);
100 rAny <<= aSeq;
103 SdrLayer::SdrLayer(SdrLayerID nNewID, const OUString& rNewName) :
104 maName(rNewName), pModel(NULL), nType(0), nID(nNewID)
108 void SdrLayer::SetStandardLayer(bool bStd)
110 nType=(sal_uInt16)bStd;
111 if (bStd) {
112 maName = ImpGetResStr(STR_StandardLayerName);
114 if (pModel!=NULL) {
115 SdrHint aHint(HINT_LAYERCHG);
116 pModel->Broadcast(aHint);
117 pModel->SetChanged();
121 void SdrLayer::SetName(const OUString& rNewName)
123 if (rNewName == maName)
124 return;
126 maName = rNewName;
127 nType = 0; // user defined
129 if (pModel)
131 SdrHint aHint(HINT_LAYERCHG);
132 pModel->Broadcast(aHint);
133 pModel->SetChanged();
137 bool SdrLayer::operator==(const SdrLayer& rCmpLayer) const
139 return (nID == rCmpLayer.nID
140 && nType == rCmpLayer.nType
141 && maName == rCmpLayer.maName);
144 SdrLayerAdmin::SdrLayerAdmin(SdrLayerAdmin* pNewParent):
145 aLayer(),
146 pParent(pNewParent),
147 pModel(NULL),
148 maControlLayerName("Controls")
152 SdrLayerAdmin::SdrLayerAdmin(const SdrLayerAdmin& rSrcLayerAdmin):
153 aLayer(),
154 pParent(NULL),
155 pModel(NULL),
156 maControlLayerName("Controls")
158 *this = rSrcLayerAdmin;
161 SdrLayerAdmin::~SdrLayerAdmin()
163 ClearLayer();
166 void SdrLayerAdmin::ClearLayer()
168 for( std::vector<SdrLayer*>::const_iterator it = aLayer.begin(); it != aLayer.end(); ++it )
169 delete *it;
170 aLayer.clear();
173 const SdrLayerAdmin& SdrLayerAdmin::operator=(const SdrLayerAdmin& rSrcLayerAdmin)
175 ClearLayer();
176 pParent=rSrcLayerAdmin.pParent;
177 sal_uInt16 i;
178 sal_uInt16 nCount=rSrcLayerAdmin.GetLayerCount();
179 for (i=0; i<nCount; i++) {
180 aLayer.push_back(new SdrLayer(*rSrcLayerAdmin.GetLayer(i)));
182 return *this;
185 bool SdrLayerAdmin::operator==(const SdrLayerAdmin& rCmpLayerAdmin) const
187 if (pParent!=rCmpLayerAdmin.pParent ||
188 aLayer.size()!=rCmpLayerAdmin.aLayer.size())
189 return false;
190 bool bOk = true;
191 sal_uInt16 nCount=GetLayerCount();
192 sal_uInt16 i=0;
193 while (bOk && i<nCount) {
194 bOk=*GetLayer(i)==*rCmpLayerAdmin.GetLayer(i);
195 i++;
197 return bOk;
200 void SdrLayerAdmin::SetModel(SdrModel* pNewModel)
202 if (pNewModel!=pModel) {
203 pModel=pNewModel;
204 sal_uInt16 nCount=GetLayerCount();
205 sal_uInt16 i;
206 for (i=0; i<nCount; i++) {
207 GetLayer(i)->SetModel(pNewModel);
212 void SdrLayerAdmin::Broadcast() const
214 if (pModel!=NULL) {
215 SdrHint aHint(HINT_LAYERORDERCHG);
216 pModel->Broadcast(aHint);
217 pModel->SetChanged();
221 SdrLayer* SdrLayerAdmin::RemoveLayer(sal_uInt16 nPos)
223 SdrLayer* pRetLayer=aLayer[nPos];
224 aLayer.erase(aLayer.begin()+nPos);
225 Broadcast();
226 return pRetLayer;
229 SdrLayer* SdrLayerAdmin::NewLayer(const OUString& rName, sal_uInt16 nPos)
231 SdrLayerID nID=GetUniqueLayerID();
232 SdrLayer* pLay=new SdrLayer(nID,rName);
233 pLay->SetModel(pModel);
234 if(nPos==0xFFFF)
235 aLayer.push_back(pLay);
236 else
237 aLayer.insert(aLayer.begin() + nPos, pLay);
238 Broadcast();
239 return pLay;
242 SdrLayer* SdrLayerAdmin::NewStandardLayer(sal_uInt16 nPos)
244 SdrLayerID nID=GetUniqueLayerID();
245 SdrLayer* pLay=new SdrLayer(nID,OUString());
246 pLay->SetStandardLayer();
247 pLay->SetModel(pModel);
248 if(nPos==0xFFFF)
249 aLayer.push_back(pLay);
250 else
251 aLayer.insert(aLayer.begin() + nPos, pLay);
252 Broadcast();
253 return pLay;
256 sal_uInt16 SdrLayerAdmin::GetLayerPos(SdrLayer* pLayer) const
258 sal_uIntPtr nRet=SDRLAYER_NOTFOUND;
259 if (pLayer!=NULL) {
260 std::vector<SdrLayer*>::const_iterator it = std::find(aLayer.begin(), aLayer.end(), pLayer);
261 if (it==aLayer.end()) {
262 nRet=SDRLAYER_NOTFOUND;
263 } else {
264 nRet=it - aLayer.begin();
267 return sal_uInt16(nRet);
270 SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName, bool bInherited)
272 return const_cast<SdrLayer*>(((const SdrLayerAdmin*)this)->GetLayer(rName, bInherited));
275 const SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName, bool /*bInherited*/) const
277 sal_uInt16 i(0);
278 const SdrLayer* pLay = NULL;
280 while(i < GetLayerCount() && !pLay)
282 if (rName == GetLayer(i)->GetName())
283 pLay = GetLayer(i);
284 else
285 i++;
288 if(!pLay && pParent)
290 pLay = pParent->GetLayer(rName, true);
293 return pLay;
296 SdrLayerID SdrLayerAdmin::GetLayerID(const OUString& rName, bool bInherited) const
298 SdrLayerID nRet=SDRLAYER_NOTFOUND;
299 const SdrLayer* pLay=GetLayer(rName,bInherited);
300 if (pLay!=NULL) nRet=pLay->GetID();
301 return nRet;
304 const SdrLayer* SdrLayerAdmin::GetLayerPerID(sal_uInt16 nID) const
306 sal_uInt16 i=0;
307 const SdrLayer* pLay=NULL;
308 while (i<GetLayerCount() && pLay==NULL) {
309 if (nID==GetLayer(i)->GetID()) pLay=GetLayer(i);
310 else i++;
312 return pLay;
315 // Global LayerIDs begin at 0 and increase,
316 // local LayerIDs begin at 254 and decrease;
317 // 255 is reserved for SDRLAYER_NOTFOUND.
319 SdrLayerID SdrLayerAdmin::GetUniqueLayerID() const
321 SetOfByte aSet;
322 bool bDown = (pParent == NULL);
323 sal_uInt16 j;
324 for (j=0; j<GetLayerCount(); j++)
326 aSet.Set(GetLayer((sal_uInt16)j)->GetID());
328 SdrLayerID i;
329 if (!bDown)
331 i=254;
332 while (i && aSet.IsSet(sal_uInt8(i)))
333 --i;
334 if (i == 0)
335 i=254;
337 else
339 i=0;
340 while (i<=254 && aSet.IsSet(sal_uInt8(i)))
341 i++;
342 if (i>254)
343 i=0;
345 return i;
348 void SdrLayerAdmin::SetControlLayerName(const OUString& rNewName)
350 maControlLayerName = rNewName;
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */