| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <sal/config.h> |
| 21 |
#include <sal/log.hxx> |
| 22 |
|
| 23 |
#include <iomanip> |
| 24 |
|
| 25 |
#include <tools/debug.hxx> |
| 26 |
#include <tools/stream.hxx> |
| 27 |
#include <basic/sbx.hxx> |
| 28 |
#include <basic/sberrors.hxx> |
| 29 |
#include <basic/sbxmeth.hxx> |
| 30 |
#include <sbxprop.hxx> |
| 31 |
#include <svl/SfxBroadcaster.hxx> |
| 32 |
#include "sbxdec.hxx" |
| 33 |
#include "sbxres.hxx" |
| 34 |
|
| 35 |
|
| 36 |
static OUString pNameProp; // Name-Property |
| 37 |
static OUString pParentProp; // Parent-Property |
| 38 |
|
| 39 |
static sal_uInt16 nNameHash = 0, nParentHash = 0; |
| 40 |
|
| 41 |
|
| 42 |
SbxObject::SbxObject( const OUString& rClass ) |
| 43 |
: SbxVariable( SbxOBJECT ), aClassName( rClass ) |
| 44 |
{ |
| 45 |
aData.pObj = this; |
| 46 |
if( !nNameHash ) |
| 47 |
{ |
| 48 |
pNameProp = GetSbxRes( StringId::NameProp ); |
| 49 |
pParentProp = GetSbxRes( StringId::ParentProp ); |
| 50 |
nNameHash = MakeHashCode( pNameProp ); |
| 51 |
nParentHash = MakeHashCode( pParentProp ); |
| 52 |
} |
| 53 |
SbxObject::Clear(); |
| 54 |
SbxObject::SetName( rClass ); |
| 55 |
} |
| 56 |
|
| 57 |
SbxObject::SbxObject( const SbxObject& rObj ) |
| 58 |
: SvRefBase( rObj ), SbxVariable( rObj.GetType() ), |
| 59 |
SfxListener( rObj ) |
| 60 |
{ |
| 61 |
*this = rObj; |
| 62 |
} |
| 63 |
|
| 64 |
SbxObject& SbxObject::operator=( const SbxObject& r ) |
| 65 |
{ |
| 66 |
if( &r != this ) |
| 67 |
{ |
| 68 |
SbxVariable::operator=( r ); |
| 69 |
aClassName = r.aClassName; |
| 70 |
pMethods = new SbxArray; |
| 71 |
pProps = new SbxArray; |
| 72 |
pObjs = new SbxArray( SbxOBJECT ); |
| 73 |
// The arrays were copied, the content taken over |
| 74 |
*pMethods = *r.pMethods; |
| 75 |
*pProps = *r.pProps; |
| 76 |
*pObjs = *r.pObjs; |
| 77 |
// Because the variables were taken over, this is OK |
| 78 |
pDfltProp = r.pDfltProp; |
| 79 |
SetName( r.GetName() ); |
| 80 |
SetFlags( r.GetFlags() ); |
| 81 |
SetModified( true ); |
| 82 |
} |
| 83 |
return *this; |
| 84 |
} |
| 85 |
|
| 86 |
static void CheckParentsOnDelete( SbxObject* pObj, SbxArray* p ) |
| 87 |
{ |
| 88 |
for (sal_uInt32 i = 0; i < p->Count(); i++) |
| 89 |
{ |
| 90 |
SbxVariableRef& rRef = p->GetRef(i); |
| 91 |
if( rRef->IsBroadcaster() ) |
| 92 |
{ |
| 93 |
pObj->EndListening( rRef->GetBroadcaster(), true ); |
| 94 |
} |
| 95 |
// does the element have more than one reference and still a Listener? |
| 96 |
if( rRef->GetRefCount() > 1 ) |
| 97 |
{ |
| 98 |
rRef->SetParent( nullptr ); |
| 99 |
SAL_INFO_IF(rRef->IsBroadcaster() && rRef->GetBroadcaster().GetListenerCount(), "basic.sbx", "Object element with dangling parent"); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
SbxObject::~SbxObject() |
| 105 |
{ |
| 106 |
CheckParentsOnDelete( this, pProps.get() ); |
| 107 |
CheckParentsOnDelete( this, pMethods.get() ); |
| 108 |
CheckParentsOnDelete( this, pObjs.get() ); |
| 109 |
|
| 110 |
// avoid handling in ~SbxVariable as SbxFlagBits::DimAsNew == SbxFlagBits::GlobalSearch |
| 111 |
ResetFlag( SbxFlagBits::DimAsNew ); |
| 112 |
} |
| 113 |
|
| 114 |
SbxDataType SbxObject::GetType() const |
| 115 |
{ |
| 116 |
return SbxOBJECT; |
| 117 |
} |
| 118 |
|
| 119 |
SbxClassType SbxObject::GetClass() const |
| 120 |
{ |
| 121 |
return SbxClassType::Object; |
| 122 |
} |
| 123 |
|
| 124 |
void SbxObject::Clear() |
| 125 |
{ |
| 126 |
pMethods = new SbxArray; |
| 127 |
pProps = new SbxArray; |
| 128 |
pObjs = new SbxArray( SbxOBJECT ); |
| 129 |
SbxVariable* p; |
| 130 |
p = Make( pNameProp, SbxClassType::Property, SbxSTRING ); |
| 131 |
p->SetFlag( SbxFlagBits::DontStore ); |
| 132 |
p = Make( pParentProp, SbxClassType::Property, SbxOBJECT ); |
| 133 |
p->ResetFlag( SbxFlagBits::Write ); |
| 134 |
p->SetFlag( SbxFlagBits::DontStore ); |
| 135 |
pDfltProp = nullptr; |
| 136 |
SetModified( false ); |
| 137 |
} |
| 138 |
|
| 139 |
void SbxObject::Notify( SfxBroadcaster&, const SfxHint& rHint ) |
| 140 |
{ |
| 141 |
const SfxHintId nId = rHint.GetId(); |
| 142 |
bool bRead = ( nId == SfxHintId::BasicDataWanted ); |
| 143 |
bool bWrite = ( nId == SfxHintId::BasicDataChanged ); |
| 144 |
if( !(bRead || bWrite) ) |
| 145 |
return; |
| 146 |
const SbxHint* p = static_cast<const SbxHint*>(&rHint); |
| 147 |
|
| 148 |
SbxVariable* pVar = p->GetVar(); |
| 149 |
|
| 150 |
OUString aVarName( pVar->GetName() ); |
| 151 |
sal_uInt16 nHash_ = MakeHashCode( aVarName ); |
| 152 |
if( nHash_ == nNameHash && aVarName.equalsIgnoreAsciiCase( pNameProp ) ) |
| 153 |
{ |
| 154 |
if( bRead ) |
| 155 |
{ |
| 156 |
pVar->PutString( GetName() ); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
SetName( pVar->GetOUString() ); |
| 161 |
} |
| 162 |
} |
| 163 |
else if( nHash_ == nParentHash && aVarName.equalsIgnoreAsciiCase( pParentProp ) ) |
| 164 |
{ |
| 165 |
SbxObject* p_ = GetParent(); |
| 166 |
if( !p_ ) |
| 167 |
{ |
| 168 |
p_ = this; |
| 169 |
} |
| 170 |
pVar->PutObject( p_ ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
bool SbxObject::IsClass( const OUString& rName ) const |
| 175 |
{ |
| 176 |
return aClassName.equalsIgnoreAsciiCase( rName ); |
| 177 |
} |
| 178 |
|
| 179 |
SbxVariable* SbxObject::Find( const OUString& rName, SbxClassType t ) |
| 180 |
{ |
| 181 |
#ifdef DBG_UTIL |
| 182 |
static int nLvl = 1; |
| 183 |
static const char* const pCls[] = { "DontCare","Array","Value","Variable","Method","Property","Object" }; |
| 184 |
SAL_INFO( |
| 185 |
"basic.sbx", |
| 186 |
"search" << std::setw(nLvl) << " " |
| 187 |
<< (t >= SbxClassType::DontCare && t <= SbxClassType::Object |
| 188 |
? pCls[static_cast<int>(t) - 1] : "Unknown class") |
| 189 |
<< " " << rName << " in " << SbxVariable::GetName()); |
| 190 |
++nLvl; |
| 191 |
#endif |
| 192 |
|
| 193 |
SbxVariable* pRes = nullptr; |
| 194 |
pObjs->SetFlag( SbxFlagBits::ExtSearch ); |
| 195 |
if( t == SbxClassType::DontCare ) |
| 196 |
{ |
| 197 |
pRes = pMethods->Find( rName, SbxClassType::Method ); |
| 198 |
if( !pRes ) |
| 199 |
{ |
| 200 |
pRes = pProps->Find( rName, SbxClassType::Property ); |
| 201 |
} |
| 202 |
if( !pRes ) |
| 203 |
{ |
| 204 |
pRes = pObjs->Find( rName, t ); |
| 205 |
} |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
SbxArray* pArray = nullptr; |
| 210 |
switch( t ) |
| 211 |
{ |
| 212 |
case SbxClassType::Variable: |
| 213 |
case SbxClassType::Property: pArray = pProps.get(); break; |
| 214 |
case SbxClassType::Method: pArray = pMethods.get(); break; |
| 215 |
case SbxClassType::Object: pArray = pObjs.get(); break; |
| 216 |
default: SAL_WARN( "basic.sbx", "Invalid SBX-Class" ); break; |
| 217 |
} |
| 218 |
if( pArray ) |
| 219 |
{ |
| 220 |
pRes = pArray->Find( rName, t ); |
| 221 |
} |
| 222 |
} |
| 223 |
// Extended Search in the Object-Array? |
| 224 |
// For objects and DontCare the array of objects has already been searched |
| 225 |
if( !pRes && ( t == SbxClassType::Method || t == SbxClassType::Property ) ) |
| 226 |
pRes = pObjs->Find( rName, t ); |
| 227 |
// Search in the parents? |
| 228 |
if( !pRes && IsSet( SbxFlagBits::GlobalSearch ) ) |
| 229 |
{ |
| 230 |
SbxObject* pCur = this; |
| 231 |
while( !pRes && pCur->pParent ) |
| 232 |
{ |
| 233 |
// I myself was already searched! |
| 234 |
SbxFlagBits nOwn = pCur->GetFlags(); |
| 235 |
pCur->ResetFlag( SbxFlagBits::ExtSearch ); |
| 236 |
// I search already global! |
| 237 |
SbxFlagBits nPar = pCur->pParent->GetFlags(); |
| 238 |
pCur->pParent->ResetFlag( SbxFlagBits::GlobalSearch ); |
| 239 |
pRes = pCur->pParent->Find( rName, t ); |
| 240 |
pCur->SetFlags( nOwn ); |
| 241 |
pCur->pParent->SetFlags( nPar ); |
| 242 |
pCur = pCur->pParent; |
| 243 |
} |
| 244 |
} |
| 245 |
#ifdef DBG_UTIL |
| 246 |
--nLvl; |
| 247 |
SAL_INFO_IF( |
| 248 |
pRes, "basic.sbx", |
| 249 |
"found" << std::setw(nLvl) << " " << rName << " in " |
| 250 |
<< SbxVariable::GetName()); |
| 251 |
#endif |
| 252 |
return pRes; |
| 253 |
} |
| 254 |
|
| 255 |
// Abbreviated version: The parent-string will be searched |
| 256 |
// The whole thing recursive, because Call() might be overridden |
| 257 |
// Qualified names are allowed |
| 258 |
|
| 259 |
bool SbxObject::Call( const OUString& rName, SbxArray* pParam ) |
| 260 |
{ |
| 261 |
SbxVariable* pMeth = FindQualified( rName, SbxClassType::DontCare); |
| 262 |
if( dynamic_cast<const SbxMethod*>( pMeth) ) |
| 263 |
{ |
| 264 |
// tdf#149622 - clear return value of the method before calling it |
| 265 |
pMeth->Clear(); |
| 266 |
|
| 267 |
// FindQualified() might have struck already! |
| 268 |
if( pParam ) |
| 269 |
{ |
| 270 |
pMeth->SetParameters( pParam ); |
| 271 |
} |
| 272 |
pMeth->Broadcast( SfxHintId::BasicDataWanted ); |
| 273 |
pMeth->SetParameters( nullptr ); |
| 274 |
return true; |
| 275 |
} |
| 276 |
SetError( ERRCODE_BASIC_NO_METHOD, rName ); |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
SbxProperty* SbxObject::GetDfltProperty() |
| 281 |
{ |
| 282 |
if ( !pDfltProp && !aDfltPropName.isEmpty() ) |
| 283 |
{ |
| 284 |
pDfltProp = static_cast<SbxProperty*>( Find( aDfltPropName, SbxClassType::Property ) ); |
| 285 |
if( !pDfltProp ) |
| 286 |
{ |
| 287 |
pDfltProp = static_cast<SbxProperty*>( Make( aDfltPropName, SbxClassType::Property, SbxVARIANT ) ); |
| 288 |
} |
| 289 |
} |
| 290 |
return pDfltProp; |
| 291 |
} |
| 292 |
void SbxObject::SetDfltProperty( const OUString& rName ) |
| 293 |
{ |
| 294 |
if ( rName != aDfltPropName ) |
| 295 |
{ |
| 296 |
pDfltProp = nullptr; |
| 297 |
} |
| 298 |
aDfltPropName = rName; |
| 299 |
SetModified( true ); |
| 300 |
} |
| 301 |
|
| 302 |
// Search of an already available variable. If it was located, |
| 303 |
// the index will be set, otherwise the Count of the Array will be returned. |
| 304 |
// In any case the correct Array will be returned. |
| 305 |
|
| 306 |
SbxArray* SbxObject::FindVar( SbxVariable const * pVar, sal_uInt32& nArrayIdx ) |
| 307 |
{ |
| 308 |
SbxArray* pArray = nullptr; |
| 309 |
if( pVar ) |
| 310 |
{ |
| 311 |
switch( pVar->GetClass() ) |
| 312 |
{ |
| 313 |
case SbxClassType::Variable: |
| 314 |
case SbxClassType::Property: pArray = pProps.get(); break; |
| 315 |
case SbxClassType::Method: pArray = pMethods.get(); break; |
| 316 |
case SbxClassType::Object: pArray = pObjs.get(); break; |
| 317 |
default: SAL_WARN( "basic.sbx", "Invalid SBX-Class" ); break; |
| 318 |
} |
| 319 |
} |
| 320 |
if( pArray ) |
| 321 |
{ |
| 322 |
nArrayIdx = pArray->Count(); |
| 323 |
// Is the variable per name available? |
| 324 |
pArray->ResetFlag( SbxFlagBits::ExtSearch ); |
| 325 |
SbxVariable* pOld = pArray->Find( pVar->GetName(), pVar->GetClass() ); |
| 326 |
if( pOld ) |
| 327 |
{ |
| 328 |
for (sal_uInt32 i = 0; i < pArray->Count(); i++) |
| 329 |
{ |
| 330 |
SbxVariableRef& rRef = pArray->GetRef(i); |
| 331 |
if( rRef.get() == pOld ) |
| 332 |
{ |
| 333 |
nArrayIdx = i; break; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
return pArray; |
| 339 |
} |
| 340 |
|
| 341 |
// If a new object will be established, this object will be indexed, |
| 342 |
// if an object of this name exists already. |
| 343 |
|
| 344 |
SbxVariable* SbxObject::Make( const OUString& rName, SbxClassType ct, SbxDataType dt, bool bIsRuntimeFunction ) |
| 345 |
{ |
| 346 |
// Is the object already available? |
| 347 |
SbxArray* pArray = nullptr; |
| 348 |
switch( ct ) |
| 349 |
{ |
| 350 |
case SbxClassType::Variable: |
| 351 |
case SbxClassType::Property: pArray = pProps.get(); break; |
| 352 |
case SbxClassType::Method: pArray = pMethods.get(); break; |
| 353 |
case SbxClassType::Object: pArray = pObjs.get(); break; |
| 354 |
default: SAL_WARN( "basic.sbx", "Invalid SBX-Class" ); break; |
| 355 |
} |
| 356 |
if( !pArray ) |
| 357 |
{ |
| 358 |
return nullptr; |
| 359 |
} |
| 360 |
// Collections may contain objects of the same name |
| 361 |
if( ct != SbxClassType::Object || dynamic_cast<const SbxCollection*>( this ) == nullptr ) |
| 362 |
{ |
| 363 |
SbxVariable* pRes = pArray->Find( rName, ct ); |
| 364 |
if( pRes ) |
| 365 |
{ |
| 366 |
return pRes; |
| 367 |
} |
| 368 |
} |
| 369 |
SbxVariableRef pVar; |
| 370 |
switch( ct ) |
| 371 |
{ |
| 372 |
case SbxClassType::Variable: |
| 373 |
case SbxClassType::Property: |
| 374 |
pVar = new SbxProperty( rName, dt ); |
| 375 |
break; |
| 376 |
case SbxClassType::Method: |
| 377 |
pVar = new SbxMethod( rName, dt, bIsRuntimeFunction ); |
| 378 |
break; |
| 379 |
case SbxClassType::Object: |
| 380 |
pVar = CreateObject( rName ).get(); |
| 381 |
break; |
| 382 |
default: |
| 383 |
break; |
| 384 |
} |
| 385 |
pVar->SetParent( this ); |
| 386 |
pArray->Put(pVar.get(), pArray->Count()); |
| 387 |
SetModified( true ); |
| 388 |
// The object listen always |
| 389 |
StartListening(pVar->GetBroadcaster(), DuplicateHandling::Prevent); |
| 390 |
return pVar.get(); |
| 391 |
} |
| 392 |
|
| 393 |
void SbxObject::Insert( SbxVariable* pVar ) |
| 394 |
{ |
| 395 |
sal_uInt32 nIdx; |
| 396 |
SbxArray* pArray = FindVar( pVar, nIdx ); |
| 397 |
if( !pArray ) |
| 398 |
return; |
| 399 |
|
| 400 |
// Into with it. But you should pay attention at the Pointer! |
| 401 |
if (nIdx < pArray->Count()) |
| 402 |
{ |
| 403 |
// Then this element exists already |
| 404 |
// There are objects of the same name allowed at collections |
| 405 |
if( pArray == pObjs.get() && dynamic_cast<const SbxCollection*>( this ) != nullptr ) |
| 406 |
{ |
| 407 |
nIdx = pArray->Count(); |
| 408 |
} |
| 409 |
else |
| 410 |
{ |
| 411 |
SbxVariable* pOld = pArray->Get(nIdx); |
| 412 |
// already inside: overwrite |
| 413 |
if( pOld == pVar ) |
| 414 |
{ |
| 415 |
return; |
| 416 |
} |
| 417 |
EndListening( pOld->GetBroadcaster(), true ); |
| 418 |
if( pVar->GetClass() == SbxClassType::Property ) |
| 419 |
{ |
| 420 |
if( pOld == pDfltProp ) |
| 421 |
{ |
| 422 |
pDfltProp = static_cast<SbxProperty*>(pVar); |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
StartListening(pVar->GetBroadcaster(), DuplicateHandling::Prevent); |
| 428 |
pArray->Put(pVar, nIdx); |
| 429 |
if( pVar->GetParent() != this ) |
| 430 |
{ |
| 431 |
pVar->SetParent( this ); |
| 432 |
} |
| 433 |
SetModified( true ); |
| 434 |
#ifdef DBG_UTIL |
| 435 |
static const char* const pCls[] = |
| 436 |
{ "DontCare","Array","Value","Variable","Method","Property","Object" }; |
| 437 |
OUString aVarName( pVar->GetName() ); |
| 438 |
if (const SbxObject *pSbxObj = aVarName.isEmpty() ? dynamic_cast<const SbxObject*>(pVar) : nullptr) |
| 439 |
{ |
| 440 |
aVarName = pSbxObj->GetClassName(); |
| 441 |
} |
| 442 |
SAL_INFO( |
| 443 |
"basic.sbx", |
| 444 |
"insert " |
| 445 |
<< ((pVar->GetClass() >= SbxClassType::DontCare |
| 446 |
&& pVar->GetClass() <= SbxClassType::Object) |
| 447 |
? pCls[static_cast<int>(pVar->GetClass()) - 1] : "Unknown class") |
| 448 |
<< " " << aVarName << " in " << SbxVariable::GetName()); |
| 449 |
#endif |
| 450 |
} |
| 451 |
|
| 452 |
// Optimisation, Insertion without checking about |
| 453 |
// double entry and without broadcasts, will only be used in SO2/auto.cxx |
| 454 |
void SbxObject::QuickInsert( SbxVariable* pVar ) |
| 455 |
{ |
| 456 |
SbxArray* pArray = nullptr; |
| 457 |
if( pVar ) |
| 458 |
{ |
| 459 |
switch( pVar->GetClass() ) |
| 460 |
{ |
| 461 |
case SbxClassType::Variable: |
| 462 |
case SbxClassType::Property: pArray = pProps.get(); break; |
| 463 |
case SbxClassType::Method: pArray = pMethods.get(); break; |
| 464 |
case SbxClassType::Object: pArray = pObjs.get(); break; |
| 465 |
default: SAL_WARN( "basic.sbx", "Invalid SBX-Class" ); break; |
| 466 |
} |
| 467 |
} |
| 468 |
if( !pArray ) |
| 469 |
return; |
| 470 |
|
| 471 |
StartListening(pVar->GetBroadcaster(), DuplicateHandling::Prevent); |
| 472 |
pArray->Put(pVar, pArray->Count()); |
| 473 |
if( pVar->GetParent() != this ) |
| 474 |
{ |
| 475 |
pVar->SetParent( this ); |
| 476 |
} |
| 477 |
SetModified( true ); |
| 478 |
#ifdef DBG_UTIL |
| 479 |
static const char* const pCls[] = |
| 480 |
{ "DontCare","Array","Value","Variable","Method","Property","Object" }; |
| 481 |
OUString aVarName( pVar->GetName() ); |
| 482 |
if (const SbxObject *pSbxObj = aVarName.isEmpty() ? dynamic_cast<const SbxObject*>(pVar) : nullptr) |
| 483 |
{ |
| 484 |
aVarName = pSbxObj->GetClassName(); |
| 485 |
} |
| 486 |
SAL_INFO( |
| 487 |
"basic.sbx", |
| 488 |
"insert " |
| 489 |
<< ((pVar->GetClass() >= SbxClassType::DontCare |
| 490 |
&& pVar->GetClass() <= SbxClassType::Object) |
| 491 |
? pCls[static_cast<int>(pVar->GetClass()) - 1] : "Unknown class") |
| 492 |
<< " " << aVarName << " in " << SbxVariable::GetName()); |
| 493 |
#endif |
| 494 |
} |
| 495 |
|
| 496 |
void SbxObject::Remove( const OUString& rName, SbxClassType t ) |
| 497 |
{ |
| 498 |
Remove( SbxObject::Find( rName, t ) ); |
| 499 |
} |
| 500 |
|
| 501 |
void SbxObject::Remove( SbxVariable* pVar ) |
| 502 |
{ |
| 503 |
sal_uInt32 nIdx; |
| 504 |
SbxArray* pArray = FindVar( pVar, nIdx ); |
| 505 |
if (!(pArray && nIdx < pArray->Count())) |
| 506 |
return; |
| 507 |
|
| 508 |
#ifdef DBG_UTIL |
| 509 |
OUString aVarName( pVar->GetName() ); |
| 510 |
if (const SbxObject *pSbxObj = aVarName.isEmpty() ? dynamic_cast<const SbxObject*>(pVar) : nullptr) |
| 511 |
{ |
| 512 |
aVarName = pSbxObj->GetClassName(); |
| 513 |
} |
| 514 |
SAL_INFO( |
| 515 |
"basic.sbx", |
| 516 |
"remove " << aVarName << " in " << SbxVariable::GetName()); |
| 517 |
#endif |
| 518 |
SbxVariableRef pVar_ = pArray->Get(nIdx); |
| 519 |
if( pVar_->IsBroadcaster() ) |
| 520 |
{ |
| 521 |
EndListening( pVar_->GetBroadcaster(), true ); |
| 522 |
} |
| 523 |
if( pVar_.get() == pDfltProp ) |
| 524 |
{ |
| 525 |
pDfltProp = nullptr; |
| 526 |
} |
| 527 |
pArray->Remove( nIdx ); |
| 528 |
if( pVar_->GetParent() == this ) |
| 529 |
{ |
| 530 |
pVar_->SetParent( nullptr ); |
| 531 |
} |
| 532 |
SetModified( true ); |
| 533 |
} |
| 534 |
|
| 535 |
static bool LoadArray( SvStream& rStrm, SbxObject* pThis, SbxArray* pArray ) |
| 536 |
{ |
| 537 |
SbxArrayRef p = static_cast<SbxArray*>( SbxBase::Load( rStrm ).get() ); |
| 538 |
if( !p.is() ) |
| 539 |
{ |
| 540 |
return false; |
| 541 |
} |
| 542 |
for (sal_uInt32 i = 0; i < p->Count(); i++) |
| 543 |
{ |
| 544 |
SbxVariableRef& r = p->GetRef(i); |
| 545 |
SbxVariable* pVar = r.get(); |
| 546 |
if( pVar ) |
| 547 |
{ |
| 548 |
pVar->SetParent( pThis ); |
| 549 |
pThis->StartListening(pVar->GetBroadcaster(), DuplicateHandling::Prevent); |
| 550 |
} |
| 551 |
} |
| 552 |
pArray->Merge( p.get() ); |
| 553 |
return true; |
| 554 |
} |
| 555 |
|
| 556 |
// The load of an object is additive! |
| 557 |
|
| 558 |
bool SbxObject::LoadData( SvStream& rStrm, sal_uInt16 nVer ) |
| 559 |
{ |
| 560 |
// Help for the read in of old objects: just return TRUE, |
| 561 |
// LoadPrivateData() has to set the default status up |
| 562 |
if( !nVer ) |
| 563 |
{ |
| 564 |
return true; |
| 565 |
} |
| 566 |
pDfltProp = nullptr; |
| 567 |
if( !SbxVariable::LoadData( rStrm, nVer ) ) |
| 568 |
{ |
| 569 |
return false; |
| 570 |
} |
| 571 |
// If it contains no alien object, insert ourselves |
| 572 |
if( aData.eType == SbxOBJECT && !aData.pObj ) |
| 573 |
{ |
| 574 |
aData.pObj = this; |
| 575 |
} |
| 576 |
sal_uInt32 nSize; |
| 577 |
OUString aDfltProp; |
| 578 |
aClassName = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm, RTL_TEXTENCODING_ASCII_US); |
| 579 |
aDfltProp = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm, RTL_TEXTENCODING_ASCII_US); |
| 580 |
sal_uInt64 nPos = rStrm.Tell(); |
| 581 |
rStrm.ReadUInt32( nSize ); |
| 582 |
sal_uInt64 const nNewPos = rStrm.Tell(); |
| 583 |
nPos += nSize; |
| 584 |
DBG_ASSERT( nPos >= nNewPos, "SBX: Loaded too much data" ); |
| 585 |
if( nPos != nNewPos ) |
| 586 |
{ |
| 587 |
rStrm.Seek( nPos ); |
| 588 |
} |
| 589 |
if( !LoadArray( rStrm, this, pMethods.get() ) || |
| 590 |
!LoadArray( rStrm, this, pProps.get() ) || |
| 591 |
!LoadArray( rStrm, this, pObjs.get() ) ) |
| 592 |
{ |
| 593 |
return false; |
| 594 |
} |
| 595 |
// Set properties |
| 596 |
if( !aDfltProp.isEmpty() ) |
| 597 |
{ |
| 598 |
pDfltProp = static_cast<SbxProperty*>( pProps->Find( aDfltProp, SbxClassType::Property ) ); |
| 599 |
} |
| 600 |
SetModified( false ); |
| 601 |
return true; |
| 602 |
} |
| 603 |
|
| 604 |
std::pair<bool, sal_uInt32> SbxObject::StoreData( SvStream& rStrm ) const |
| 605 |
{ |
| 606 |
if( !SbxVariable::StoreData(rStrm).first ) |
| 607 |
{ |
| 608 |
return { false, 0 }; |
| 609 |
} |
| 610 |
OUString aDfltProp; |
| 611 |
if( pDfltProp ) |
| 612 |
{ |
| 613 |
aDfltProp = pDfltProp->GetName(); |
| 614 |
} |
| 615 |
write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aClassName, RTL_TEXTENCODING_ASCII_US); |
| 616 |
write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aDfltProp, RTL_TEXTENCODING_ASCII_US); |
| 617 |
sal_uInt64 const nPos = rStrm.Tell(); |
| 618 |
rStrm.WriteUInt32( 0 ); |
| 619 |
sal_uInt64 const nNew = rStrm.Tell(); |
| 620 |
rStrm.Seek( nPos ); |
| 621 |
rStrm.WriteUInt32( nNew - nPos ); |
| 622 |
rStrm.Seek( nNew ); |
| 623 |
const auto [bSuccess, nVersion] = pMethods->Store( rStrm ); |
| 624 |
if( !bSuccess ) |
| 625 |
{ |
| 626 |
return { false, 0 }; |
| 627 |
} |
| 628 |
if( !pProps->Store( rStrm ).first ) |
| 629 |
{ |
| 630 |
return { false, 0 }; |
| 631 |
} |
| 632 |
if( !pObjs->Store( rStrm ).first ) |
| 633 |
{ |
| 634 |
return { false, 0 }; |
| 635 |
} |
| 636 |
const_cast<SbxObject*>(this)->SetModified( false ); |
| 637 |
return { true, nVersion }; |
| 638 |
} |
| 639 |
|
| 640 |
static bool CollectAttrs( const SbxBase* p, OUString& rRes ) |
| 641 |
{ |
| 642 |
OUString aAttrs; |
| 643 |
if( p->IsHidden() ) |
| 644 |
{ |
| 645 |
aAttrs = "Hidden"; |
| 646 |
} |
| 647 |
if( p->IsSet( SbxFlagBits::ExtSearch ) ) |
| 648 |
{ |
| 649 |
if( !aAttrs.isEmpty() ) |
| 650 |
{ |
| 651 |
aAttrs += ","; |
| 652 |
} |
| 653 |
aAttrs += "ExtSearch"; |
| 654 |
} |
| 655 |
if( !p->IsVisible() ) |
| 656 |
{ |
| 657 |
if( !aAttrs.isEmpty() ) |
| 658 |
{ |
| 659 |
aAttrs += ","; |
| 660 |
} |
| 661 |
aAttrs += "Invisible"; |
| 662 |
} |
| 663 |
if( p->IsSet( SbxFlagBits::DontStore ) ) |
| 664 |
{ |
| 665 |
if( !aAttrs.isEmpty() ) |
| 666 |
{ |
| 667 |
aAttrs += ","; |
| 668 |
} |
| 669 |
aAttrs += "DontStore"; |
| 670 |
} |
| 671 |
if( !aAttrs.isEmpty() ) |
| 672 |
{ |
| 673 |
rRes = " (" + aAttrs + ")"; |
| 674 |
return true; |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
rRes.clear(); |
| 679 |
return false; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
void SbxObject::Dump( SvStream& rStrm, bool bFill ) |
| 684 |
{ |
| 685 |
// Shifting |
| 686 |
static sal_uInt16 nLevel = 0; |
| 687 |
if ( nLevel > 10 ) |
| 688 |
{ |
| 689 |
rStrm.WriteOString( "<too deep>" ) << endl; |
| 690 |
return; |
| 691 |
} |
| 692 |
++nLevel; |
| 693 |
OUString aIndent(u""_ustr); |
| 694 |
for ( sal_uInt16 n = 1; n < nLevel; ++n ) |
| 695 |
{ |
| 696 |
aIndent += " "; |
| 697 |
} |
| 698 |
// Output the data of the object itself |
| 699 |
OString aNameStr(OUStringToOString(GetName(), RTL_TEXTENCODING_ASCII_US)); |
| 700 |
OString aClassNameStr(OUStringToOString(aClassName, RTL_TEXTENCODING_ASCII_US)); |
| 701 |
rStrm.WriteOString( "Object( " ) |
| 702 |
.WriteOString( OString::number(reinterpret_cast<sal_IntPtr>(this)) ).WriteOString( "=='" ) |
| 703 |
.WriteOString( aNameStr.isEmpty() ? "<unnamed>"_ostr : aNameStr ).WriteOString( "', " ) |
| 704 |
.WriteOString( "of class '" ).WriteOString( aClassNameStr ).WriteOString( "', " ) |
| 705 |
.WriteOString( "counts " ) |
| 706 |
.WriteOString( OString::number(GetRefCount()) ) |
| 707 |
.WriteOString( " refs, " ); |
| 708 |
if ( GetParent() ) |
| 709 |
{ |
| 710 |
OString aParentNameStr(OUStringToOString(GetName(), RTL_TEXTENCODING_ASCII_US)); |
| 711 |
rStrm.WriteOString( "in parent " ) |
| 712 |
.WriteOString( OString::number(reinterpret_cast<sal_IntPtr>(GetParent())) ) |
| 713 |
.WriteOString( "=='" ).WriteOString( aParentNameStr.isEmpty() ? "<unnamed>"_ostr : aParentNameStr ).WriteOString( "'" ); |
| 714 |
} |
| 715 |
else |
| 716 |
{ |
| 717 |
rStrm.WriteOString( "no parent " ); |
| 718 |
} |
| 719 |
rStrm.WriteOString( " )" ) << endl; |
| 720 |
OString aIndentNameStr(OUStringToOString(aIndent, RTL_TEXTENCODING_ASCII_US)); |
| 721 |
rStrm.WriteOString( aIndentNameStr ).WriteOString( "{" ) << endl; |
| 722 |
|
| 723 |
// Flags |
| 724 |
OUString aAttrs; |
| 725 |
if( CollectAttrs( this, aAttrs ) ) |
| 726 |
{ |
| 727 |
OString aAttrStr(OUStringToOString(aAttrs, RTL_TEXTENCODING_ASCII_US)); |
| 728 |
rStrm.WriteOString( aIndentNameStr ).WriteOString( "- Flags: " ).WriteOString( aAttrStr ) << endl; |
| 729 |
} |
| 730 |
|
| 731 |
// Methods |
| 732 |
rStrm.WriteOString( aIndentNameStr ).WriteOString( "- Methods:" ) << endl; |
| 733 |
for (sal_uInt32 i = 0; i < pMethods->Count(); i++) |
| 734 |
{ |
| 735 |
SbxVariableRef& r = pMethods->GetRef(i); |
| 736 |
SbxVariable* pVar = r.get(); |
| 737 |
if( pVar ) |
| 738 |
{ |
| 739 |
OUString aLine = aIndent + " - " + pVar->GetName( SbxNameType::ShortTypes ); |
| 740 |
OUString aAttrs2; |
| 741 |
if( CollectAttrs( pVar, aAttrs2 ) ) |
| 742 |
{ |
| 743 |
aLine += aAttrs2; |
| 744 |
} |
| 745 |
if( dynamic_cast<const SbxMethod *>(pVar) == nullptr ) |
| 746 |
{ |
| 747 |
aLine += " !! Not a Method !!"; |
| 748 |
} |
| 749 |
write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aLine, RTL_TEXTENCODING_ASCII_US); |
| 750 |
|
| 751 |
// Output also the object at object-methods |
| 752 |
if ( pVar->GetValues_Impl().eType == SbxOBJECT && |
| 753 |
pVar->GetValues_Impl().pObj && |
| 754 |
pVar->GetValues_Impl().pObj != this && |
| 755 |
pVar->GetValues_Impl().pObj != GetParent() ) |
| 756 |
{ |
| 757 |
rStrm.WriteOString( " contains " ); |
| 758 |
static_cast<SbxObject*>(pVar->GetValues_Impl().pObj)->Dump( rStrm, bFill ); |
| 759 |
} |
| 760 |
else |
| 761 |
{ |
| 762 |
rStrm << endl; |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
// Properties |
| 768 |
rStrm.WriteOString( aIndentNameStr ).WriteOString( "- Properties:" ) << endl; |
| 769 |
{ |
| 770 |
for (sal_uInt32 i = 0; i < pProps->Count(); i++) |
| 771 |
{ |
| 772 |
SbxVariableRef& r = pProps->GetRef(i); |
| 773 |
SbxVariable* pVar = r.get(); |
| 774 |
if( pVar ) |
| 775 |
{ |
| 776 |
OUString aLine = aIndent + " - " + pVar->GetName( SbxNameType::ShortTypes ); |
| 777 |
OUString aAttrs3; |
| 778 |
if( CollectAttrs( pVar, aAttrs3 ) ) |
| 779 |
{ |
| 780 |
aLine += aAttrs3; |
| 781 |
} |
| 782 |
if( dynamic_cast<const SbxProperty *>(pVar) == nullptr ) |
| 783 |
{ |
| 784 |
aLine += " !! Not a Property !!"; |
| 785 |
} |
| 786 |
write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aLine, RTL_TEXTENCODING_ASCII_US); |
| 787 |
|
| 788 |
// output also the object at object properties |
| 789 |
if ( pVar->GetValues_Impl().eType == SbxOBJECT && |
| 790 |
pVar->GetValues_Impl().pObj && |
| 791 |
pVar->GetValues_Impl().pObj != this && |
| 792 |
pVar->GetValues_Impl().pObj != GetParent() ) |
| 793 |
{ |
| 794 |
rStrm.WriteOString( " contains " ); |
| 795 |
static_cast<SbxObject*>(pVar->GetValues_Impl().pObj)->Dump( rStrm, bFill ); |
| 796 |
} |
| 797 |
else |
| 798 |
{ |
| 799 |
rStrm << endl; |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
// Objects |
| 806 |
rStrm.WriteOString( aIndentNameStr ).WriteOString( "- Objects:" ) << endl; |
| 807 |
{ |
| 808 |
for (sal_uInt32 i = 0; i < pObjs->Count(); i++) |
| 809 |
{ |
| 810 |
SbxVariableRef& r = pObjs->GetRef(i); |
| 811 |
SbxVariable* pVar = r.get(); |
| 812 |
if ( pVar ) |
| 813 |
{ |
| 814 |
rStrm.WriteOString( aIndentNameStr ).WriteOString( " - Sub" ); |
| 815 |
if (SbxObject *pSbxObj = dynamic_cast<SbxObject*>(pVar)) |
| 816 |
{ |
| 817 |
pSbxObj->Dump(rStrm, bFill); |
| 818 |
} |
| 819 |
else |
| 820 |
{ |
| 821 |
pVar->Dump(rStrm, bFill); |
| 822 |
} |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
rStrm.WriteOString( aIndentNameStr ).WriteOString( "}" ) << endl << endl; |
| 828 |
--nLevel; |
| 829 |
} |
| 830 |
|
| 831 |
SbxMethod::SbxMethod( const OUString& r, SbxDataType t, bool bIsRuntimeFunction ) |
| 832 |
: SbxVariable(t) |
| 833 |
, mbIsRuntimeFunction(bIsRuntimeFunction) |
| 834 |
, mbRuntimeFunctionReturnType(t) |
| 835 |
{ |
| 836 |
SetName(r); |
| 837 |
} |
| 838 |
|
| 839 |
SbxMethod::SbxMethod( const SbxMethod& r ) |
| 840 |
: SvRefBase(r) |
| 841 |
, SbxVariable(r) |
| 842 |
, mbIsRuntimeFunction(r.IsRuntimeFunction()) |
| 843 |
, mbRuntimeFunctionReturnType(r.GetRuntimeFunctionReturnType()) |
| 844 |
{ |
| 845 |
} |
| 846 |
|
| 847 |
SbxMethod::~SbxMethod() |
| 848 |
{ |
| 849 |
} |
| 850 |
|
| 851 |
SbxClassType SbxMethod::GetClass() const |
| 852 |
{ |
| 853 |
return SbxClassType::Method; |
| 854 |
} |
| 855 |
|
| 856 |
void SbxMethod::Clear() |
| 857 |
{ |
| 858 |
// Release referenced data, and reset data type to the function return type |
| 859 |
// Implementation similar to SbxValue::SetType |
| 860 |
// tdf#143582: Don't take "read-only" flag into account, allow clearing method return value |
| 861 |
switch (aData.eType) |
| 862 |
{ |
| 863 |
case SbxSTRING: |
| 864 |
delete aData.pOUString; |
| 865 |
break; |
| 866 |
case SbxOBJECT: |
| 867 |
if (aData.pObj) |
| 868 |
{ |
| 869 |
if (aData.pObj != this) |
| 870 |
{ |
| 871 |
bool bParentProp = (GetUserData() & 0xFFFF) == 5345; // See sbxvalue.cxx |
| 872 |
if (!bParentProp) |
| 873 |
aData.pObj->ReleaseRef(); |
| 874 |
} |
| 875 |
} |
| 876 |
break; |
| 877 |
case SbxDECIMAL: |
| 878 |
releaseDecimalPtr(aData.pDecimal); |
| 879 |
break; |
| 880 |
default: |
| 881 |
break; |
| 882 |
} |
| 883 |
aData.clear(IsFixed() ? aData.eType : SbxEMPTY); |
| 884 |
} |
| 885 |
|
| 886 |
SbxProperty::SbxProperty( const OUString& r, SbxDataType t ) |
| 887 |
: SbxVariable( t ) |
| 888 |
{ |
| 889 |
SetName( r ); |
| 890 |
} |
| 891 |
|
| 892 |
SbxProperty::~SbxProperty() |
| 893 |
{ |
| 894 |
} |
| 895 |
|
| 896 |
SbxClassType SbxProperty::GetClass() const |
| 897 |
{ |
| 898 |
return SbxClassType::Property; |
| 899 |
} |
| 900 |
|
| 901 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |