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