1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <o3tl/safeint.hxx>
21 #include <tools/stream.hxx>
23 #include <basic/sbx.hxx>
24 #include <basic/sberrors.hxx>
28 static OUString pCount
;
30 static OUString pItem
;
31 static OUString pRemove
;
32 static sal_uInt16 nCountHash
= 0, nAddHash
, nItemHash
, nRemoveHash
;
35 SbxCollection::SbxCollection()
40 pCount
= GetSbxRes( StringId::CountProp
);
41 pAdd
= GetSbxRes( StringId::AddMeth
);
42 pItem
= GetSbxRes( StringId::ItemMeth
);
43 pRemove
= GetSbxRes( StringId::RemoveMeth
);
44 nCountHash
= MakeHashCode( pCount
);
45 nAddHash
= MakeHashCode( pAdd
);
46 nItemHash
= MakeHashCode( pItem
);
47 nRemoveHash
= MakeHashCode( pRemove
);
50 // For Access on itself
51 StartListening(GetBroadcaster(), DuplicateHandling::Prevent
);
54 SbxCollection::SbxCollection( const SbxCollection
& rColl
)
55 : SvRefBase( rColl
), SbxObject( rColl
)
58 SbxCollection
& SbxCollection::operator=( const SbxCollection
& r
)
61 SbxObject::operator=( r
);
65 SbxCollection::~SbxCollection()
68 void SbxCollection::Clear()
74 void SbxCollection::Initialize()
77 SetFlag( SbxFlagBits::Fixed
);
78 ResetFlag( SbxFlagBits::Write
);
80 p
= Make( pCount
, SbxClassType::Property
, SbxINTEGER
);
81 p
->ResetFlag( SbxFlagBits::Write
);
82 p
->SetFlag( SbxFlagBits::DontStore
);
83 p
= Make( pAdd
, SbxClassType::Method
, SbxEMPTY
);
84 p
->SetFlag( SbxFlagBits::DontStore
);
85 p
= Make( pItem
, SbxClassType::Method
, SbxOBJECT
);
86 p
->SetFlag( SbxFlagBits::DontStore
);
87 p
= Make( pRemove
, SbxClassType::Method
, SbxEMPTY
);
88 p
->SetFlag( SbxFlagBits::DontStore
);
91 SbxVariable
* SbxCollection::Find( const OUString
& rName
, SbxClassType t
)
95 SbxObject
* pObj
= static_cast<SbxObject
*>(GetObject());
96 return pObj
? pObj
->Find( rName
, t
) : nullptr;
100 return SbxObject::Find( rName
, t
);
104 void SbxCollection::Notify( SfxBroadcaster
& rCst
, const SfxHint
& rHint
)
106 const SbxHint
* p
= dynamic_cast<const SbxHint
*>(&rHint
);
109 const SfxHintId nId
= p
->GetId();
110 bool bRead
= ( nId
== SfxHintId::BasicDataWanted
);
111 bool bWrite
= ( nId
== SfxHintId::BasicDataChanged
);
112 SbxVariable
* pVar
= p
->GetVar();
113 SbxArray
* pArg
= pVar
->GetParameters();
114 if( bRead
|| bWrite
)
116 OUString
aVarName( pVar
->GetName() );
121 else if( pVar
->GetHashCode() == nCountHash
122 && aVarName
.equalsIgnoreAsciiCase( pCount
) )
124 pVar
->PutLong(sal::static_int_cast
<sal_Int32
>(pObjs
->Count()));
126 else if( pVar
->GetHashCode() == nAddHash
127 && aVarName
.equalsIgnoreAsciiCase( pAdd
) )
131 else if( pVar
->GetHashCode() == nItemHash
132 && aVarName
.equalsIgnoreAsciiCase( pItem
) )
136 else if( pVar
->GetHashCode() == nRemoveHash
137 && aVarName
.equalsIgnoreAsciiCase( pRemove
) )
143 SbxObject::Notify( rCst
, rHint
);
148 SbxObject::Notify( rCst
, rHint
);
151 // Default: argument is object
153 void SbxCollection::CollAdd( SbxArray
* pPar_
)
155 if (pPar_
->Count() != 2)
157 SetError( ERRCODE_BASIC_WRONG_ARGS
);
161 SbxBase
* pObj
= pPar_
->Get(1)->GetObject();
162 if( !pObj
|| dynamic_cast<const SbxObject
*>(pObj
) == nullptr )
164 SetError( ERRCODE_BASIC_BAD_ARGUMENT
);
168 Insert( static_cast<SbxObject
*>(pObj
) );
173 // Default: index from 1 or object name
175 void SbxCollection::CollItem( SbxArray
* pPar_
)
177 if (pPar_
->Count() != 2)
179 SetError( ERRCODE_BASIC_WRONG_ARGS
);
183 SbxVariable
* pRes
= nullptr;
184 SbxVariable
* p
= pPar_
->Get(1);
185 if( p
->GetType() == SbxSTRING
)
187 pRes
= Find( p
->GetOUString(), SbxClassType::Object
);
191 short n
= p
->GetInteger();
192 if (n
>= 1 && o3tl::make_unsigned(n
) <= pObjs
->Count())
194 pRes
= pObjs
->Get(static_cast<sal_uInt32
>(n
) - 1);
199 SetError( ERRCODE_BASIC_BAD_INDEX
);
201 pPar_
->Get(0)->PutObject(pRes
);
205 // Default: index from 1
207 void SbxCollection::CollRemove( SbxArray
* pPar_
)
209 if (pPar_
->Count() != 2)
210 SetError( ERRCODE_BASIC_WRONG_ARGS
);
213 short n
= pPar_
->Get(1)->GetInteger();
214 if (n
< 1 || o3tl::make_unsigned(n
) > pObjs
->Count())
215 SetError( ERRCODE_BASIC_BAD_INDEX
);
217 Remove(pObjs
->Get(static_cast<sal_uInt32
>(n
) - 1));
221 bool SbxCollection::LoadData( SvStream
& rStrm
, sal_uInt16 nVer
)
223 bool bRes
= SbxObject::LoadData( rStrm
, nVer
);
229 SbxStdCollection::SbxStdCollection()
230 : bAddRemoveOk( true )
233 SbxStdCollection::SbxStdCollection( const SbxStdCollection
& r
)
234 : SvRefBase( r
), SbxCollection( r
),
235 aElemClass( r
.aElemClass
), bAddRemoveOk( r
.bAddRemoveOk
)
238 SbxStdCollection
& SbxStdCollection::operator=( const SbxStdCollection
& r
)
242 if( !r
.aElemClass
.equalsIgnoreAsciiCase( aElemClass
) )
244 SetError( ERRCODE_BASIC_CONVERSION
);
248 SbxCollection::operator=( r
);
254 SbxStdCollection::~SbxStdCollection()
257 // Default: Error, if wrong object
259 void SbxStdCollection::Insert( SbxVariable
* p
)
261 SbxObject
* pObj
= dynamic_cast<SbxObject
*>( p
);
262 if( pObj
&& !pObj
->IsClass( aElemClass
) )
263 SetError( ERRCODE_BASIC_BAD_ACTION
);
265 SbxCollection::Insert( p
);
268 void SbxStdCollection::CollAdd( SbxArray
* pPar_
)
271 SetError( ERRCODE_BASIC_BAD_ACTION
);
273 SbxCollection::CollAdd( pPar_
);
276 void SbxStdCollection::CollRemove( SbxArray
* pPar_
)
279 SetError( ERRCODE_BASIC_BAD_ACTION
);
281 SbxCollection::CollRemove( pPar_
);
284 bool SbxStdCollection::LoadData( SvStream
& rStrm
, sal_uInt16 nVer
)
286 bool bRes
= SbxCollection::LoadData( rStrm
, nVer
);
289 aElemClass
= read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm
,
290 RTL_TEXTENCODING_ASCII_US
);
291 rStrm
.ReadCharAsBool( bAddRemoveOk
);
296 bool SbxStdCollection::StoreData( SvStream
& rStrm
) const
298 bool bRes
= SbxCollection::StoreData( rStrm
);
301 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm
, aElemClass
,
302 RTL_TEXTENCODING_ASCII_US
);
303 rStrm
.WriteBool( bAddRemoveOk
);
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */