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 <tools/stream.hxx>
22 #include <basic/sbx.hxx>
23 #include <basic/sberrors.hxx>
27 static OUString pCount
;
29 static OUString pItem
;
30 static OUString pRemove
;
31 static sal_uInt16 nCountHash
= 0, nAddHash
, nItemHash
, nRemoveHash
;
34 SbxCollection::SbxCollection()
39 pCount
= GetSbxRes( StringId::CountProp
);
40 pAdd
= GetSbxRes( StringId::AddMeth
);
41 pItem
= GetSbxRes( StringId::ItemMeth
);
42 pRemove
= GetSbxRes( StringId::RemoveMeth
);
43 nCountHash
= MakeHashCode( pCount
);
44 nAddHash
= MakeHashCode( pAdd
);
45 nItemHash
= MakeHashCode( pItem
);
46 nRemoveHash
= MakeHashCode( pRemove
);
49 // For Access on itself
50 StartListening(GetBroadcaster(), DuplicateHandling::Prevent
);
53 SbxCollection::SbxCollection( const SbxCollection
& rColl
)
54 : SvRefBase( rColl
), SbxObject( rColl
)
57 SbxCollection
& SbxCollection::operator=( const SbxCollection
& r
)
60 SbxObject::operator=( r
);
64 SbxCollection::~SbxCollection()
67 void SbxCollection::Clear()
73 void SbxCollection::Initialize()
76 SetFlag( SbxFlagBits::Fixed
);
77 ResetFlag( SbxFlagBits::Write
);
79 p
= Make( pCount
, SbxClassType::Property
, SbxINTEGER
);
80 p
->ResetFlag( SbxFlagBits::Write
);
81 p
->SetFlag( SbxFlagBits::DontStore
);
82 p
= Make( pAdd
, SbxClassType::Method
, SbxEMPTY
);
83 p
->SetFlag( SbxFlagBits::DontStore
);
84 p
= Make( pItem
, SbxClassType::Method
, SbxOBJECT
);
85 p
->SetFlag( SbxFlagBits::DontStore
);
86 p
= Make( pRemove
, SbxClassType::Method
, SbxEMPTY
);
87 p
->SetFlag( SbxFlagBits::DontStore
);
90 SbxVariable
* SbxCollection::Find( const OUString
& rName
, SbxClassType t
)
94 SbxObject
* pObj
= static_cast<SbxObject
*>(GetObject());
95 return pObj
? pObj
->Find( rName
, t
) : nullptr;
99 return SbxObject::Find( rName
, t
);
103 void SbxCollection::Notify( SfxBroadcaster
& rCst
, const SfxHint
& rHint
)
105 const SbxHint
* p
= dynamic_cast<const SbxHint
*>(&rHint
);
108 const SfxHintId nId
= p
->GetId();
109 bool bRead
= ( nId
== SfxHintId::BasicDataWanted
);
110 bool bWrite
= ( nId
== SfxHintId::BasicDataChanged
);
111 SbxVariable
* pVar
= p
->GetVar();
112 SbxArray
* pArg
= pVar
->GetParameters();
113 if( bRead
|| bWrite
)
115 OUString
aVarName( pVar
->GetName() );
120 else if( pVar
->GetHashCode() == nCountHash
121 && aVarName
.equalsIgnoreAsciiCase( pCount
) )
123 pVar
->PutLong(sal::static_int_cast
<sal_Int32
>(pObjs
->Count()));
125 else if( pVar
->GetHashCode() == nAddHash
126 && aVarName
.equalsIgnoreAsciiCase( pAdd
) )
130 else if( pVar
->GetHashCode() == nItemHash
131 && aVarName
.equalsIgnoreAsciiCase( pItem
) )
135 else if( pVar
->GetHashCode() == nRemoveHash
136 && aVarName
.equalsIgnoreAsciiCase( pRemove
) )
142 SbxObject::Notify( rCst
, rHint
);
147 SbxObject::Notify( rCst
, rHint
);
150 // Default: argument is object
152 void SbxCollection::CollAdd( SbxArray
* pPar_
)
154 if (pPar_
->Count() != 2)
156 SetError( ERRCODE_BASIC_WRONG_ARGS
);
160 SbxBase
* pObj
= pPar_
->Get(1)->GetObject();
161 if( !pObj
|| dynamic_cast<const SbxObject
*>(pObj
) == nullptr )
163 SetError( ERRCODE_BASIC_BAD_ARGUMENT
);
167 Insert( static_cast<SbxObject
*>(pObj
) );
172 // Default: index from 1 or object name
174 void SbxCollection::CollItem( SbxArray
* pPar_
)
176 if (pPar_
->Count() != 2)
178 SetError( ERRCODE_BASIC_WRONG_ARGS
);
182 SbxVariable
* pRes
= nullptr;
183 SbxVariable
* p
= pPar_
->Get(1);
184 if( p
->GetType() == SbxSTRING
)
186 pRes
= Find( p
->GetOUString(), SbxClassType::Object
);
190 short n
= p
->GetInteger();
191 if (n
>= 1 && n
<= static_cast<sal_Int32
>(pObjs
->Count()))
193 pRes
= pObjs
->Get(static_cast<sal_uInt32
>(n
) - 1);
198 SetError( ERRCODE_BASIC_BAD_INDEX
);
200 pPar_
->Get(0)->PutObject(pRes
);
204 // Default: index from 1
206 void SbxCollection::CollRemove( SbxArray
* pPar_
)
208 if (pPar_
->Count() != 2)
209 SetError( ERRCODE_BASIC_WRONG_ARGS
);
212 short n
= pPar_
->Get(1)->GetInteger();
213 if (n
< 1 || n
> static_cast<sal_Int32
>(pObjs
->Count()))
214 SetError( ERRCODE_BASIC_BAD_INDEX
);
216 Remove(pObjs
->Get(static_cast<sal_uInt32
>(n
) - 1));
220 bool SbxCollection::LoadData( SvStream
& rStrm
, sal_uInt16 nVer
)
222 bool bRes
= SbxObject::LoadData( rStrm
, nVer
);
228 SbxStdCollection::SbxStdCollection()
229 : bAddRemoveOk( true )
232 SbxStdCollection::SbxStdCollection( const SbxStdCollection
& r
)
233 : SvRefBase( r
), SbxCollection( r
),
234 aElemClass( r
.aElemClass
), bAddRemoveOk( r
.bAddRemoveOk
)
237 SbxStdCollection
& SbxStdCollection::operator=( const SbxStdCollection
& r
)
241 if( !r
.aElemClass
.equalsIgnoreAsciiCase( aElemClass
) )
243 SetError( ERRCODE_BASIC_CONVERSION
);
247 SbxCollection::operator=( r
);
253 SbxStdCollection::~SbxStdCollection()
256 // Default: Error, if wrong object
258 void SbxStdCollection::Insert( SbxVariable
* p
)
260 SbxObject
* pObj
= dynamic_cast<SbxObject
*>( p
);
261 if( pObj
&& !pObj
->IsClass( aElemClass
) )
262 SetError( ERRCODE_BASIC_BAD_ACTION
);
264 SbxCollection::Insert( p
);
267 void SbxStdCollection::CollAdd( SbxArray
* pPar_
)
270 SetError( ERRCODE_BASIC_BAD_ACTION
);
272 SbxCollection::CollAdd( pPar_
);
275 void SbxStdCollection::CollRemove( SbxArray
* pPar_
)
278 SetError( ERRCODE_BASIC_BAD_ACTION
);
280 SbxCollection::CollRemove( pPar_
);
283 bool SbxStdCollection::LoadData( SvStream
& rStrm
, sal_uInt16 nVer
)
285 bool bRes
= SbxCollection::LoadData( rStrm
, nVer
);
288 aElemClass
= read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm
,
289 RTL_TEXTENCODING_ASCII_US
);
290 rStrm
.ReadCharAsBool( bAddRemoveOk
);
295 bool SbxStdCollection::StoreData( SvStream
& rStrm
) const
297 bool bRes
= SbxCollection::StoreData( rStrm
);
300 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm
, aElemClass
,
301 RTL_TEXTENCODING_ASCII_US
);
302 rStrm
.WriteBool( bAddRemoveOk
);
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */