xref: /trunk/main/svx/source/svdraw/svdlayer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 #include <com/sun/star/uno/Sequence.hxx>
31 
32 #include <svx/svdlayer.hxx>
33 #include <svx/svdmodel.hxx> // fuer Broadcasting
34 #include "svx/svdglob.hxx"  // StringCache
35 #include "svx/svdstr.hrc"   // Namen aus der Resource
36 
37 ////////////////////////////////////////////////////////////////////////////////////////////////////
38 // SetOfByte
39 ////////////////////////////////////////////////////////////////////////////////////////////////////
40 
41 sal_Bool SetOfByte::IsEmpty() const
42 {
43     for(sal_uInt16 i(0); i < 32; i++)
44     {
45         if(aData[i] != 0)
46             return sal_False;
47     }
48 
49     return sal_True;
50 }
51 
52 sal_Bool SetOfByte::IsFull() const
53 {
54     for(sal_uInt16 i(0); i < 32; i++)
55     {
56         if(aData[i] != 0xFF)
57             return sal_False;
58     }
59 
60     return sal_True;
61 }
62 
63 sal_uInt16 SetOfByte::GetSetCount() const
64 {
65     sal_uInt16 nRet(0);
66 
67     for(sal_uInt16 i(0); i < 32; i++)
68     {
69         sal_uInt8 a(aData[i]);
70 
71         if(a != 0)
72         {
73             if(a & 0x80) nRet++;
74             if(a & 0x40) nRet++;
75             if(a & 0x20) nRet++;
76             if(a & 0x10) nRet++;
77             if(a & 0x08) nRet++;
78             if(a & 0x04) nRet++;
79             if(a & 0x02) nRet++;
80             if(a & 0x01) nRet++;
81         }
82     }
83 
84     return nRet;
85 }
86 
87 sal_uInt8 SetOfByte::GetSetBit(sal_uInt16 nNum) const
88 {
89     nNum++;
90     sal_uInt16 i(0), j(0);
91     sal_uInt16 nRet(0);
92 
93     while(j < nNum && i < 256)
94     {
95         if(IsSet(sal_uInt8(i)))
96             j++;
97         i++;
98     }
99 
100     if(j == nNum)
101         nRet = i - 1;
102 
103     return sal_uInt8(nRet);
104 }
105 
106 sal_uInt16 SetOfByte::GetClearCount() const
107 {
108     return sal_uInt16(256 - GetSetCount());
109 }
110 
111 sal_uInt8 SetOfByte::GetClearBit(sal_uInt16 nNum) const
112 {
113     nNum++;
114     sal_uInt16 i(0), j(0);
115     sal_uInt16 nRet(0);
116 
117     while(j < nNum && i < 256)
118     {
119         if(!IsSet(sal_uInt8(i)))
120             j++;
121         i++;
122     }
123 
124     if(j == nNum)
125         nRet = i - 1;
126 
127     return sal_uInt8(nRet);
128 }
129 
130 void SetOfByte::operator&=(const SetOfByte& r2ndSet)
131 {
132     for(sal_uInt16 i(0); i < 32; i++)
133     {
134         aData[i] &= r2ndSet.aData[i];
135     }
136 }
137 
138 void SetOfByte::operator|=(const SetOfByte& r2ndSet)
139 {
140     for(sal_uInt16 i(0); i < 32; i++)
141     {
142         aData[i] |= r2ndSet.aData[i];
143     }
144 }
145 
146 /** initialize this set with a uno sequence of sal_Int8
147 */
148 void SetOfByte::PutValue( const com::sun::star::uno::Any & rAny )
149 {
150     com::sun::star::uno::Sequence< sal_Int8 > aSeq;
151     if( rAny >>= aSeq )
152     {
153         sal_Int16 nCount = (sal_Int16)aSeq.getLength();
154         if( nCount > 32 )
155             nCount = 32;
156 
157         sal_Int16 nIndex;
158         for( nIndex = 0; nIndex < nCount; nIndex++ )
159         {
160             aData[nIndex] = static_cast<sal_uInt8>(aSeq[nIndex]);
161         }
162 
163         for( ; nIndex < 32; nIndex++ )
164         {
165             aData[nIndex] = 0;
166         }
167     }
168 }
169 
170 /** returns a uno sequence of sal_Int8
171 */
172 void SetOfByte::QueryValue( com::sun::star::uno::Any & rAny ) const
173 {
174     sal_Int16 nNumBytesSet = 0;
175     sal_Int16 nIndex;
176     for( nIndex = 31; nIndex >= 00; nIndex-- )
177     {
178         if( 0 != aData[nIndex] )
179         {
180             nNumBytesSet = nIndex + 1;
181             break;
182         }
183     }
184 
185     com::sun::star::uno::Sequence< sal_Int8 > aSeq( nNumBytesSet );
186 
187     for( nIndex = 0; nIndex < nNumBytesSet; nIndex++ )
188     {
189         aSeq[nIndex] = static_cast<sal_Int8>(aData[nIndex]);
190     }
191 
192     rAny <<= aSeq;
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////////////////////////////
196 // SdrLayer
197 ////////////////////////////////////////////////////////////////////////////////////////////////////
198 
199 void SdrLayer::SetStandardLayer(FASTBOOL bStd)
200 {
201     nType=(sal_uInt16)bStd;
202     if (bStd) {
203         aName=ImpGetResStr(STR_StandardLayerName);
204     }
205     if (pModel!=NULL) {
206         SdrHint aHint(HINT_LAYERCHG);
207         pModel->Broadcast(aHint);
208         pModel->SetChanged();
209     }
210 }
211 
212 void SdrLayer::SetName(const XubString& rNewName)
213 {
214     if(!rNewName.Equals(aName))
215     {
216         aName = rNewName;
217         nType = 0; // Userdefined
218 
219         if(pModel)
220         {
221             SdrHint aHint(HINT_LAYERCHG);
222 
223             pModel->Broadcast(aHint);
224             pModel->SetChanged();
225         }
226     }
227 }
228 
229 bool SdrLayer::operator==(const SdrLayer& rCmpLayer) const
230 {
231     return (nID == rCmpLayer.nID
232         && nType == rCmpLayer.nType
233         && aName.Equals(rCmpLayer.aName));
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////////////////////////
237 // SdrLayerAdmin
238 ////////////////////////////////////////////////////////////////////////////////////////////////////
239 
240 SdrLayerAdmin::SdrLayerAdmin(SdrLayerAdmin* pNewParent):
241     aLayer(1024,16,16),
242     aLSets(1024,16,16),
243     pModel(NULL)
244 {
245     sal_Char aTextControls[] = "Controls";
246     aControlLayerName = String(aTextControls, sizeof(aTextControls-1));
247     pParent=pNewParent;
248 }
249 
250 SdrLayerAdmin::SdrLayerAdmin(const SdrLayerAdmin& rSrcLayerAdmin):
251     aLayer(1024,16,16),
252     aLSets(1024,16,16),
253     pParent(NULL),
254     pModel(NULL)
255 {
256     sal_Char aTextControls[] = "Controls";
257     aControlLayerName = String(aTextControls, sizeof(aTextControls-1));
258     *this = rSrcLayerAdmin;
259 }
260 
261 SdrLayerAdmin::~SdrLayerAdmin()
262 {
263     ClearLayer();
264 }
265 
266 void SdrLayerAdmin::ClearLayer()
267 {
268     SdrLayer* pL;
269     pL=(SdrLayer*)aLayer.First();
270     while (pL!=NULL) {
271         delete pL;
272         pL=(SdrLayer*)aLayer.Next();
273     }
274     aLayer.Clear();
275 }
276 
277 const SdrLayerAdmin& SdrLayerAdmin::operator=(const SdrLayerAdmin& rSrcLayerAdmin)
278 {
279     ClearLayer();
280     pParent=rSrcLayerAdmin.pParent;
281     sal_uInt16 i;
282     sal_uInt16 nAnz=rSrcLayerAdmin.GetLayerCount();
283     for (i=0; i<nAnz; i++) {
284         aLayer.Insert(new SdrLayer(*rSrcLayerAdmin.GetLayer(i)),CONTAINER_APPEND);
285     }
286     return *this;
287 }
288 
289 bool SdrLayerAdmin::operator==(const SdrLayerAdmin& rCmpLayerAdmin) const
290 {
291     if (pParent!=rCmpLayerAdmin.pParent ||
292         aLayer.Count()!=rCmpLayerAdmin.aLayer.Count() ||
293         aLSets.Count()!=rCmpLayerAdmin.aLSets.Count()) return sal_False;
294     FASTBOOL bOk=sal_True;
295     sal_uInt16 nAnz=GetLayerCount();
296     sal_uInt16 i=0;
297     while (bOk && i<nAnz) {
298         bOk=*GetLayer(i)==*rCmpLayerAdmin.GetLayer(i);
299         i++;
300     }
301     return bOk;
302 }
303 
304 void SdrLayerAdmin::SetModel(SdrModel* pNewModel)
305 {
306     if (pNewModel!=pModel) {
307         pModel=pNewModel;
308         sal_uInt16 nAnz=GetLayerCount();
309         sal_uInt16 i;
310         for (i=0; i<nAnz; i++) {
311             GetLayer(i)->SetModel(pNewModel);
312         }
313     }
314 }
315 
316 void SdrLayerAdmin::Broadcast() const
317 {
318     if (pModel!=NULL) {
319         SdrHint aHint(HINT_LAYERORDERCHG);
320         pModel->Broadcast(aHint);
321         pModel->SetChanged();
322     }
323 }
324 
325 SdrLayer* SdrLayerAdmin::RemoveLayer(sal_uInt16 nPos)
326 {
327     SdrLayer* pRetLayer=(SdrLayer*)(aLayer.Remove(nPos));
328     Broadcast();
329     return pRetLayer;
330 }
331 
332 SdrLayer* SdrLayerAdmin::NewLayer(const XubString& rName, sal_uInt16 nPos)
333 {
334     SdrLayerID nID=GetUniqueLayerID();
335     SdrLayer* pLay=new SdrLayer(nID,rName);
336     pLay->SetModel(pModel);
337     aLayer.Insert(pLay,nPos);
338     Broadcast();
339     return pLay;
340 }
341 
342 SdrLayer* SdrLayerAdmin::NewStandardLayer(sal_uInt16 nPos)
343 {
344     SdrLayerID nID=GetUniqueLayerID();
345     SdrLayer* pLay=new SdrLayer(nID,String());
346     pLay->SetStandardLayer();
347     pLay->SetModel(pModel);
348     aLayer.Insert(pLay,nPos);
349     Broadcast();
350     return pLay;
351 }
352 
353 SdrLayer* SdrLayerAdmin::MoveLayer(sal_uInt16 nPos, sal_uInt16 nNewPos)
354 {
355     SdrLayer* pLayer=(SdrLayer*)(aLayer.Remove(nPos));
356     if (pLayer!=NULL) {
357         aLayer.Insert(pLayer,nNewPos);
358     }
359 
360     Broadcast();
361     return pLayer;
362 }
363 
364 void SdrLayerAdmin::MoveLayer(SdrLayer* pLayer, sal_uInt16 nNewPos)
365 {
366     sal_uIntPtr nPos=aLayer.GetPos(pLayer);
367     if (nPos!=CONTAINER_ENTRY_NOTFOUND) {
368         aLayer.Remove(nPos);
369         aLayer.Insert(pLayer,nNewPos);
370         Broadcast();
371     }
372 }
373 
374 sal_uInt16 SdrLayerAdmin::GetLayerPos(SdrLayer* pLayer) const
375 {
376     sal_uIntPtr nRet=SDRLAYER_NOTFOUND;
377     if (pLayer!=NULL) {
378         nRet=aLayer.GetPos(pLayer);
379         if (nRet==CONTAINER_ENTRY_NOTFOUND) {
380             nRet=SDRLAYER_NOTFOUND;
381         }
382     }
383     return sal_uInt16(nRet);
384 }
385 
386 const SdrLayer* SdrLayerAdmin::GetLayer(const XubString& rName, FASTBOOL /*bInherited*/) const
387 {
388     sal_uInt16 i(0);
389     const SdrLayer* pLay = NULL;
390 
391     while(i < GetLayerCount() && !pLay)
392     {
393         if(rName.Equals(GetLayer(i)->GetName()))
394             pLay = GetLayer(i);
395         else
396             i++;
397     }
398 
399     if(!pLay && pParent)
400     {
401         pLay = pParent->GetLayer(rName, sal_True);
402     }
403 
404     return pLay;
405 }
406 
407 SdrLayerID SdrLayerAdmin::GetLayerID(const XubString& rName, FASTBOOL bInherited) const
408 {
409     SdrLayerID nRet=SDRLAYER_NOTFOUND;
410     const SdrLayer* pLay=GetLayer(rName,bInherited);
411     if (pLay!=NULL) nRet=pLay->GetID();
412     return nRet;
413 }
414 
415 const SdrLayer* SdrLayerAdmin::GetLayerPerID(sal_uInt16 nID) const
416 {
417     sal_uInt16 i=0;
418     const SdrLayer* pLay=NULL;
419     while (i<GetLayerCount() && pLay==NULL) {
420         if (nID==GetLayer(i)->GetID()) pLay=GetLayer(i);
421         else i++;
422     }
423     return pLay;
424 }
425 
426 // Globale LayerID's beginnen mit 0 aufsteigend.
427 // Lokale LayerID's beginnen mit 254 absteigend.
428 // 255 ist reserviert fuer SDRLAYER_NOTFOUND
429 
430 SdrLayerID SdrLayerAdmin::GetUniqueLayerID() const
431 {
432     SetOfByte aSet;
433     sal_Bool bDown = (pParent == NULL);
434     sal_uInt16 j;
435     for (j=0; j<GetLayerCount(); j++)
436     {
437         aSet.Set(GetLayer((sal_uInt16)j)->GetID());
438     }
439     SdrLayerID i;
440     if (!bDown)
441     {
442         i=254;
443         while (i && aSet.IsSet(sal_uInt8(i)))
444             --i;
445         if (i == 0)
446             i=254;
447     }
448     else
449     {
450         i=0;
451         while (i<=254 && aSet.IsSet(sal_uInt8(i)))
452             i++;
453         if (i>254)
454             i=0;
455     }
456     return i;
457 }
458 
459