Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / svdraw / svdlayer.cxx
blob73b566a0339b8dc6ac3d969765ded9580beea463
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);
145 // SdrLayerAdmin
148 SdrLayerAdmin::SdrLayerAdmin(SdrLayerAdmin* pNewParent):
149 aLayer(),
150 pParent(pNewParent),
151 pModel(NULL),
152 maControlLayerName("Controls")
156 SdrLayerAdmin::SdrLayerAdmin(const SdrLayerAdmin& rSrcLayerAdmin):
157 aLayer(),
158 pParent(NULL),
159 pModel(NULL),
160 maControlLayerName("Controls")
162 *this = rSrcLayerAdmin;
165 SdrLayerAdmin::~SdrLayerAdmin()
167 ClearLayer();
170 void SdrLayerAdmin::ClearLayer()
172 for( std::vector<SdrLayer*>::const_iterator it = aLayer.begin(); it != aLayer.end(); ++it )
173 delete *it;
174 aLayer.clear();
177 const SdrLayerAdmin& SdrLayerAdmin::operator=(const SdrLayerAdmin& rSrcLayerAdmin)
179 ClearLayer();
180 pParent=rSrcLayerAdmin.pParent;
181 sal_uInt16 i;
182 sal_uInt16 nAnz=rSrcLayerAdmin.GetLayerCount();
183 for (i=0; i<nAnz; i++) {
184 aLayer.push_back(new SdrLayer(*rSrcLayerAdmin.GetLayer(i)));
186 return *this;
189 bool SdrLayerAdmin::operator==(const SdrLayerAdmin& rCmpLayerAdmin) const
191 if (pParent!=rCmpLayerAdmin.pParent ||
192 aLayer.size()!=rCmpLayerAdmin.aLayer.size())
193 return false;
194 bool bOk = true;
195 sal_uInt16 nAnz=GetLayerCount();
196 sal_uInt16 i=0;
197 while (bOk && i<nAnz) {
198 bOk=*GetLayer(i)==*rCmpLayerAdmin.GetLayer(i);
199 i++;
201 return bOk;
204 void SdrLayerAdmin::SetModel(SdrModel* pNewModel)
206 if (pNewModel!=pModel) {
207 pModel=pNewModel;
208 sal_uInt16 nAnz=GetLayerCount();
209 sal_uInt16 i;
210 for (i=0; i<nAnz; i++) {
211 GetLayer(i)->SetModel(pNewModel);
216 void SdrLayerAdmin::Broadcast() const
218 if (pModel!=NULL) {
219 SdrHint aHint(HINT_LAYERORDERCHG);
220 pModel->Broadcast(aHint);
221 pModel->SetChanged();
225 SdrLayer* SdrLayerAdmin::RemoveLayer(sal_uInt16 nPos)
227 SdrLayer* pRetLayer=aLayer[nPos];
228 aLayer.erase(aLayer.begin()+nPos);
229 Broadcast();
230 return pRetLayer;
233 SdrLayer* SdrLayerAdmin::NewLayer(const OUString& rName, sal_uInt16 nPos)
235 SdrLayerID nID=GetUniqueLayerID();
236 SdrLayer* pLay=new SdrLayer(nID,rName);
237 pLay->SetModel(pModel);
238 if(nPos==0xFFFF)
239 aLayer.push_back(pLay);
240 else
241 aLayer.insert(aLayer.begin() + nPos, pLay);
242 Broadcast();
243 return pLay;
246 SdrLayer* SdrLayerAdmin::NewStandardLayer(sal_uInt16 nPos)
248 SdrLayerID nID=GetUniqueLayerID();
249 SdrLayer* pLay=new SdrLayer(nID,OUString());
250 pLay->SetStandardLayer();
251 pLay->SetModel(pModel);
252 if(nPos==0xFFFF)
253 aLayer.push_back(pLay);
254 else
255 aLayer.insert(aLayer.begin() + nPos, pLay);
256 Broadcast();
257 return pLay;
260 sal_uInt16 SdrLayerAdmin::GetLayerPos(SdrLayer* pLayer) const
262 sal_uIntPtr nRet=SDRLAYER_NOTFOUND;
263 if (pLayer!=NULL) {
264 std::vector<SdrLayer*>::const_iterator it = std::find(aLayer.begin(), aLayer.end(), pLayer);
265 if (it==aLayer.end()) {
266 nRet=SDRLAYER_NOTFOUND;
267 } else {
268 nRet=it - aLayer.begin();
271 return sal_uInt16(nRet);
274 SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName, bool bInherited)
276 return (SdrLayer*)(((const SdrLayerAdmin*)this)->GetLayer(rName, bInherited));
279 const SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName, bool /*bInherited*/) const
281 sal_uInt16 i(0);
282 const SdrLayer* pLay = NULL;
284 while(i < GetLayerCount() && !pLay)
286 if (rName == GetLayer(i)->GetName())
287 pLay = GetLayer(i);
288 else
289 i++;
292 if(!pLay && pParent)
294 pLay = pParent->GetLayer(rName, true);
297 return pLay;
300 SdrLayerID SdrLayerAdmin::GetLayerID(const OUString& rName, bool bInherited) const
302 SdrLayerID nRet=SDRLAYER_NOTFOUND;
303 const SdrLayer* pLay=GetLayer(rName,bInherited);
304 if (pLay!=NULL) nRet=pLay->GetID();
305 return nRet;
308 const SdrLayer* SdrLayerAdmin::GetLayerPerID(sal_uInt16 nID) const
310 sal_uInt16 i=0;
311 const SdrLayer* pLay=NULL;
312 while (i<GetLayerCount() && pLay==NULL) {
313 if (nID==GetLayer(i)->GetID()) pLay=GetLayer(i);
314 else i++;
316 return pLay;
319 // Global LayerIDs begin at 0 and increase,
320 // local LayerIDs begin at 254 and decrease;
321 // 255 is reserved for SDRLAYER_NOTFOUND.
323 SdrLayerID SdrLayerAdmin::GetUniqueLayerID() const
325 SetOfByte aSet;
326 bool bDown = (pParent == NULL);
327 sal_uInt16 j;
328 for (j=0; j<GetLayerCount(); j++)
330 aSet.Set(GetLayer((sal_uInt16)j)->GetID());
332 SdrLayerID i;
333 if (!bDown)
335 i=254;
336 while (i && aSet.IsSet(sal_uInt8(i)))
337 --i;
338 if (i == 0)
339 i=254;
341 else
343 i=0;
344 while (i<=254 && aSet.IsSet(sal_uInt8(i)))
345 i++;
346 if (i>254)
347 i=0;
349 return i;
352 void SdrLayerAdmin::SetControlLayerName(const OUString& rNewName)
354 maControlLayerName = rNewName;
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */