merge the formfield patch from ooo-build
[ooovba.git] / svx / source / svdraw / svdlayer.cxx
blob7895583833c4f0f9c5ac15eb50ca8210f4675c44
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svdlayer.cxx,v $
10 * $Revision: 1.16 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <com/sun/star/uno/Sequence.hxx>
35 #include <svx/svdlayer.hxx>
36 #include <svx/svdmodel.hxx> // fuer Broadcasting
37 #include "svdglob.hxx" // StringCache
38 #include "svdstr.hrc" // Namen aus der Resource
40 ////////////////////////////////////////////////////////////////////////////////////////////////////
41 // SetOfByte
42 ////////////////////////////////////////////////////////////////////////////////////////////////////
44 sal_Bool SetOfByte::IsEmpty() const
46 for(sal_uInt16 i(0); i < 32; i++)
48 if(aData[i] != 0)
49 return sal_False;
52 return sal_True;
55 sal_Bool SetOfByte::IsFull() const
57 for(sal_uInt16 i(0); i < 32; i++)
59 if(aData[i] != 0xFF)
60 return sal_False;
63 return sal_True;
66 sal_uInt16 SetOfByte::GetSetCount() const
68 sal_uInt16 nRet(0);
70 for(sal_uInt16 i(0); i < 32; i++)
72 sal_uInt8 a(aData[i]);
74 if(a != 0)
76 if(a & 0x80) nRet++;
77 if(a & 0x40) nRet++;
78 if(a & 0x20) nRet++;
79 if(a & 0x10) nRet++;
80 if(a & 0x08) nRet++;
81 if(a & 0x04) nRet++;
82 if(a & 0x02) nRet++;
83 if(a & 0x01) nRet++;
87 return nRet;
90 sal_uInt8 SetOfByte::GetSetBit(sal_uInt16 nNum) const
92 nNum++;
93 sal_uInt16 i(0), j(0);
94 sal_uInt16 nRet(0);
96 while(j < nNum && i < 256)
98 if(IsSet(sal_uInt8(i)))
99 j++;
100 i++;
103 if(j == nNum)
104 nRet = i - 1;
106 return sal_uInt8(nRet);
109 sal_uInt16 SetOfByte::GetClearCount() const
111 return sal_uInt16(256 - GetSetCount());
114 sal_uInt8 SetOfByte::GetClearBit(sal_uInt16 nNum) const
116 nNum++;
117 sal_uInt16 i(0), j(0);
118 sal_uInt16 nRet(0);
120 while(j < nNum && i < 256)
122 if(!IsSet(sal_uInt8(i)))
123 j++;
124 i++;
127 if(j == nNum)
128 nRet = i - 1;
130 return sal_uInt8(nRet);
133 void SetOfByte::operator&=(const SetOfByte& r2ndSet)
135 for(sal_uInt16 i(0); i < 32; i++)
137 aData[i] &= r2ndSet.aData[i];
141 void SetOfByte::operator|=(const SetOfByte& r2ndSet)
143 for(sal_uInt16 i(0); i < 32; i++)
145 aData[i] |= r2ndSet.aData[i];
149 /** initialize this set with a uno sequence of sal_Int8
151 void SetOfByte::PutValue( const com::sun::star::uno::Any & rAny )
153 com::sun::star::uno::Sequence< sal_Int8 > aSeq;
154 if( rAny >>= aSeq )
156 sal_Int16 nCount = (sal_Int16)aSeq.getLength();
157 if( nCount > 32 )
158 nCount = 32;
160 sal_Int16 nIndex;
161 for( nIndex = 0; nIndex < nCount; nIndex++ )
163 aData[nIndex] = static_cast<BYTE>(aSeq[nIndex]);
166 for( ; nIndex < 32; nIndex++ )
168 aData[nIndex] = 0;
173 /** returns a uno sequence of sal_Int8
175 void SetOfByte::QueryValue( com::sun::star::uno::Any & rAny ) const
177 sal_Int16 nNumBytesSet = 0;
178 sal_Int16 nIndex;
179 for( nIndex = 31; nIndex >= 00; nIndex-- )
181 if( 0 != aData[nIndex] )
183 nNumBytesSet = nIndex + 1;
184 break;
188 com::sun::star::uno::Sequence< sal_Int8 > aSeq( nNumBytesSet );
190 for( nIndex = 0; nIndex < nNumBytesSet; nIndex++ )
192 aSeq[nIndex] = static_cast<sal_Int8>(aData[nIndex]);
195 rAny <<= aSeq;
198 ////////////////////////////////////////////////////////////////////////////////////////////////////
199 // SdrLayer
200 ////////////////////////////////////////////////////////////////////////////////////////////////////
202 void SdrLayer::SetStandardLayer(FASTBOOL bStd)
204 nType=(UINT16)bStd;
205 if (bStd) {
206 aName=ImpGetResStr(STR_StandardLayerName);
208 if (pModel!=NULL) {
209 SdrHint aHint(HINT_LAYERCHG);
210 pModel->Broadcast(aHint);
211 pModel->SetChanged();
215 void SdrLayer::SetName(const XubString& rNewName)
217 if(!rNewName.Equals(aName))
219 aName = rNewName;
220 nType = 0; // Userdefined
222 if(pModel)
224 SdrHint aHint(HINT_LAYERCHG);
226 pModel->Broadcast(aHint);
227 pModel->SetChanged();
232 bool SdrLayer::operator==(const SdrLayer& rCmpLayer) const
234 return (nID == rCmpLayer.nID
235 && nType == rCmpLayer.nType
236 && aName.Equals(rCmpLayer.aName));
239 ////////////////////////////////////////////////////////////////////////////////////////////////////
240 // SdrLayerAdmin
241 ////////////////////////////////////////////////////////////////////////////////////////////////////
243 SdrLayerAdmin::SdrLayerAdmin(SdrLayerAdmin* pNewParent):
244 aLayer(1024,16,16),
245 aLSets(1024,16,16),
246 pModel(NULL)
248 sal_Char aTextControls[] = "Controls";
249 aControlLayerName = String(aTextControls, sizeof(aTextControls-1));
250 pParent=pNewParent;
253 SdrLayerAdmin::SdrLayerAdmin(const SdrLayerAdmin& rSrcLayerAdmin):
254 aLayer(1024,16,16),
255 aLSets(1024,16,16),
256 pParent(NULL),
257 pModel(NULL)
259 sal_Char aTextControls[] = "Controls";
260 aControlLayerName = String(aTextControls, sizeof(aTextControls-1));
261 *this = rSrcLayerAdmin;
264 SdrLayerAdmin::~SdrLayerAdmin()
266 ClearLayer();
269 void SdrLayerAdmin::ClearLayer()
271 SdrLayer* pL;
272 pL=(SdrLayer*)aLayer.First();
273 while (pL!=NULL) {
274 delete pL;
275 pL=(SdrLayer*)aLayer.Next();
277 aLayer.Clear();
280 const SdrLayerAdmin& SdrLayerAdmin::operator=(const SdrLayerAdmin& rSrcLayerAdmin)
282 ClearLayer();
283 pParent=rSrcLayerAdmin.pParent;
284 USHORT i;
285 USHORT nAnz=rSrcLayerAdmin.GetLayerCount();
286 for (i=0; i<nAnz; i++) {
287 aLayer.Insert(new SdrLayer(*rSrcLayerAdmin.GetLayer(i)),CONTAINER_APPEND);
289 return *this;
292 bool SdrLayerAdmin::operator==(const SdrLayerAdmin& rCmpLayerAdmin) const
294 if (pParent!=rCmpLayerAdmin.pParent ||
295 aLayer.Count()!=rCmpLayerAdmin.aLayer.Count() ||
296 aLSets.Count()!=rCmpLayerAdmin.aLSets.Count()) return FALSE;
297 FASTBOOL bOk=TRUE;
298 USHORT nAnz=GetLayerCount();
299 USHORT i=0;
300 while (bOk && i<nAnz) {
301 bOk=*GetLayer(i)==*rCmpLayerAdmin.GetLayer(i);
302 i++;
304 return bOk;
307 void SdrLayerAdmin::SetModel(SdrModel* pNewModel)
309 if (pNewModel!=pModel) {
310 pModel=pNewModel;
311 USHORT nAnz=GetLayerCount();
312 USHORT i;
313 for (i=0; i<nAnz; i++) {
314 GetLayer(i)->SetModel(pNewModel);
319 void SdrLayerAdmin::Broadcast() const
321 if (pModel!=NULL) {
322 SdrHint aHint(HINT_LAYERORDERCHG);
323 pModel->Broadcast(aHint);
324 pModel->SetChanged();
328 SdrLayer* SdrLayerAdmin::RemoveLayer(USHORT nPos)
330 SdrLayer* pRetLayer=(SdrLayer*)(aLayer.Remove(nPos));
331 Broadcast();
332 return pRetLayer;
335 SdrLayer* SdrLayerAdmin::NewLayer(const XubString& rName, USHORT nPos)
337 SdrLayerID nID=GetUniqueLayerID();
338 SdrLayer* pLay=new SdrLayer(nID,rName);
339 pLay->SetModel(pModel);
340 aLayer.Insert(pLay,nPos);
341 Broadcast();
342 return pLay;
345 SdrLayer* SdrLayerAdmin::NewStandardLayer(USHORT nPos)
347 SdrLayerID nID=GetUniqueLayerID();
348 SdrLayer* pLay=new SdrLayer(nID,String());
349 pLay->SetStandardLayer();
350 pLay->SetModel(pModel);
351 aLayer.Insert(pLay,nPos);
352 Broadcast();
353 return pLay;
356 SdrLayer* SdrLayerAdmin::MoveLayer(USHORT nPos, USHORT nNewPos)
358 SdrLayer* pLayer=(SdrLayer*)(aLayer.Remove(nPos));
359 if (pLayer!=NULL) {
360 aLayer.Insert(pLayer,nNewPos);
363 Broadcast();
364 return pLayer;
367 void SdrLayerAdmin::MoveLayer(SdrLayer* pLayer, USHORT nNewPos)
369 ULONG nPos=aLayer.GetPos(pLayer);
370 if (nPos!=CONTAINER_ENTRY_NOTFOUND) {
371 aLayer.Remove(nPos);
372 aLayer.Insert(pLayer,nNewPos);
373 Broadcast();
377 USHORT SdrLayerAdmin::GetLayerPos(SdrLayer* pLayer) const
379 ULONG nRet=SDRLAYER_NOTFOUND;
380 if (pLayer!=NULL) {
381 nRet=aLayer.GetPos(pLayer);
382 if (nRet==CONTAINER_ENTRY_NOTFOUND) {
383 nRet=SDRLAYER_NOTFOUND;
386 return USHORT(nRet);
389 const SdrLayer* SdrLayerAdmin::GetLayer(const XubString& rName, FASTBOOL /*bInherited*/) const
391 UINT16 i(0);
392 const SdrLayer* pLay = NULL;
394 while(i < GetLayerCount() && !pLay)
396 if(rName.Equals(GetLayer(i)->GetName()))
397 pLay = GetLayer(i);
398 else
399 i++;
402 if(!pLay && pParent)
404 pLay = pParent->GetLayer(rName, TRUE);
407 return pLay;
410 SdrLayerID SdrLayerAdmin::GetLayerID(const XubString& rName, FASTBOOL bInherited) const
412 SdrLayerID nRet=SDRLAYER_NOTFOUND;
413 const SdrLayer* pLay=GetLayer(rName,bInherited);
414 if (pLay!=NULL) nRet=pLay->GetID();
415 return nRet;
418 const SdrLayer* SdrLayerAdmin::GetLayerPerID(USHORT nID) const
420 USHORT i=0;
421 const SdrLayer* pLay=NULL;
422 while (i<GetLayerCount() && pLay==NULL) {
423 if (nID==GetLayer(i)->GetID()) pLay=GetLayer(i);
424 else i++;
426 return pLay;
429 // Globale LayerID's beginnen mit 0 aufsteigend.
430 // Lokale LayerID's beginnen mit 254 absteigend.
431 // 255 ist reserviert fuer SDRLAYER_NOTFOUND
433 SdrLayerID SdrLayerAdmin::GetUniqueLayerID() const
435 SetOfByte aSet;
436 sal_Bool bDown = (pParent == NULL);
437 USHORT j;
438 for (j=0; j<GetLayerCount(); j++)
440 aSet.Set(GetLayer((sal_uInt16)j)->GetID());
442 SdrLayerID i;
443 if (!bDown)
445 i=254;
446 while (i && aSet.IsSet(BYTE(i)))
447 --i;
448 if (i == 0)
449 i=254;
451 else
453 i=0;
454 while (i<=254 && aSet.IsSet(BYTE(i)))
455 i++;
456 if (i>254)
457 i=0;
459 return i;