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>
25 TYPEINIT1(SbxCollection
,SbxObject
)
26 TYPEINIT1(SbxStdCollection
,SbxCollection
)
28 static OUString pCount
;
30 static OUString pItem
;
31 static OUString pRemove
;
32 static sal_uInt16 nCountHash
= 0, nAddHash
, nItemHash
, nRemoveHash
;
35 SbxCollection::SbxCollection( const OUString
& rClass
)
40 pCount
= rtl::OUString::createFromAscii(GetSbxRes( STRING_COUNTPROP
));
41 pAdd
= rtl::OUString::createFromAscii(GetSbxRes( STRING_ADDMETH
));
42 pItem
= rtl::OUString::createFromAscii(GetSbxRes( STRING_ITEMMETH
));
43 pRemove
= rtl::OUString::createFromAscii(GetSbxRes( STRING_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(), sal_True
);
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()
78 ResetFlag( SBX_WRITE
);
80 p
= Make( pCount
, SbxCLASS_PROPERTY
, SbxINTEGER
);
81 p
->ResetFlag( SBX_WRITE
);
82 p
->SetFlag( SBX_DONTSTORE
);
83 p
= Make( pAdd
, SbxCLASS_METHOD
, SbxEMPTY
);
84 p
->SetFlag( SBX_DONTSTORE
);
85 p
= Make( pItem
, SbxCLASS_METHOD
, SbxOBJECT
);
86 p
->SetFlag( SBX_DONTSTORE
);
87 p
= Make( pRemove
, SbxCLASS_METHOD
, SbxEMPTY
);
88 p
->SetFlag( SBX_DONTSTORE
);
91 SbxVariable
* SbxCollection::FindUserData( sal_uInt32 nData
)
95 SbxObject
* pObj
= (SbxObject
*) GetObject();
96 return pObj
? pObj
->FindUserData( nData
) : NULL
;
100 return SbxObject::FindUserData( nData
);
104 SbxVariable
* SbxCollection::Find( const rtl::OUString
& rName
, SbxClassType t
)
106 if( GetParameters() )
108 SbxObject
* pObj
= (SbxObject
*) GetObject();
109 return pObj
? pObj
->Find( rName
, t
) : NULL
;
113 return SbxObject::Find( rName
, t
);
117 void SbxCollection::SFX_NOTIFY( SfxBroadcaster
& rCst
, const TypeId
& rId1
,
118 const SfxHint
& rHint
, const TypeId
& rId2
)
120 const SbxHint
* p
= PTR_CAST(SbxHint
,&rHint
);
123 sal_uIntPtr nId
= p
->GetId();
124 bool bRead
= ( nId
== SBX_HINT_DATAWANTED
);
125 bool bWrite
= ( nId
== SBX_HINT_DATACHANGED
);
126 SbxVariable
* pVar
= p
->GetVar();
127 SbxArray
* pArg
= pVar
->GetParameters();
128 if( bRead
|| bWrite
)
130 OUString
aVarName( pVar
->GetName() );
135 else if( pVar
->GetHashCode() == nCountHash
136 && aVarName
.equalsIgnoreAsciiCase( pCount
) )
138 pVar
->PutLong( pObjs
->Count() );
140 else if( pVar
->GetHashCode() == nAddHash
141 && aVarName
.equalsIgnoreAsciiCase( pAdd
) )
145 else if( pVar
->GetHashCode() == nItemHash
146 && aVarName
.equalsIgnoreAsciiCase( pItem
) )
150 else if( pVar
->GetHashCode() == nRemoveHash
151 && aVarName
.equalsIgnoreAsciiCase( pRemove
) )
157 SbxObject::SFX_NOTIFY( rCst
, rId1
, rHint
, rId2
);
162 SbxObject::SFX_NOTIFY( rCst
, rId1
, rHint
, rId2
);
165 // Default: argument is object
167 void SbxCollection::CollAdd( SbxArray
* pPar_
)
169 if( pPar_
->Count() != 2 )
171 SetError( SbxERR_WRONG_ARGS
);
175 SbxBase
* pObj
= pPar_
->Get( 1 )->GetObject();
176 if( !pObj
|| !( pObj
->ISA(SbxObject
) ) )
178 SetError( SbxERR_NOTIMP
);
182 Insert( (SbxObject
*) pObj
);
187 // Default: index from 1 or object name
189 void SbxCollection::CollItem( SbxArray
* pPar_
)
191 if( pPar_
->Count() != 2 )
193 SetError( SbxERR_WRONG_ARGS
);
197 SbxVariable
* pRes
= NULL
;
198 SbxVariable
* p
= pPar_
->Get( 1 );
199 if( p
->GetType() == SbxSTRING
)
201 pRes
= Find( p
->GetOUString(), SbxCLASS_OBJECT
);
205 short n
= p
->GetInteger();
206 if( n
>= 1 && n
<= (short) pObjs
->Count() )
208 pRes
= pObjs
->Get( (sal_uInt16
) n
- 1 );
213 SetError( SbxERR_BAD_INDEX
);
215 pPar_
->Get( 0 )->PutObject( pRes
);
219 // Default: index from 1
221 void SbxCollection::CollRemove( SbxArray
* pPar_
)
223 if( pPar_
->Count() != 2 )
224 SetError( SbxERR_WRONG_ARGS
);
227 short n
= pPar_
->Get( 1 )->GetInteger();
228 if( n
< 1 || n
> (short) pObjs
->Count() )
229 SetError( SbxERR_BAD_INDEX
);
231 Remove( pObjs
->Get( (sal_uInt16
) n
- 1 ) );
235 sal_Bool
SbxCollection::LoadData( SvStream
& rStrm
, sal_uInt16 nVer
)
237 sal_Bool bRes
= SbxObject::LoadData( rStrm
, nVer
);
243 SbxStdCollection::SbxStdCollection
244 ( const OUString
& rClass
, const OUString
& rElem
, sal_Bool b
)
245 : SbxCollection( rClass
), aElemClass( rElem
),
249 SbxStdCollection::SbxStdCollection( const SbxStdCollection
& r
)
250 : SvRefBase( r
), SbxCollection( r
),
251 aElemClass( r
.aElemClass
), bAddRemoveOk( r
.bAddRemoveOk
)
254 SbxStdCollection
& SbxStdCollection::operator=( const SbxStdCollection
& r
)
258 if( !r
.aElemClass
.equalsIgnoreAsciiCase( aElemClass
) )
260 SetError( SbxERR_CONVERSION
);
264 SbxCollection::operator=( r
);
270 SbxStdCollection::~SbxStdCollection()
273 // Default: Error, if wrong object
275 void SbxStdCollection::Insert( SbxVariable
* p
)
277 SbxObject
* pObj
= PTR_CAST(SbxObject
,p
);
278 if( pObj
&& !pObj
->IsClass( aElemClass
) )
279 SetError( SbxERR_BAD_ACTION
);
281 SbxCollection::Insert( p
);
284 void SbxStdCollection::CollAdd( SbxArray
* pPar_
)
287 SetError( SbxERR_BAD_ACTION
);
289 SbxCollection::CollAdd( pPar_
);
292 void SbxStdCollection::CollRemove( SbxArray
* pPar_
)
295 SetError( SbxERR_BAD_ACTION
);
297 SbxCollection::CollRemove( pPar_
);
300 sal_Bool
SbxStdCollection::LoadData( SvStream
& rStrm
, sal_uInt16 nVer
)
302 sal_Bool bRes
= SbxCollection::LoadData( rStrm
, nVer
);
305 aElemClass
= read_lenPrefixed_uInt8s_ToOUString
<sal_uInt16
>(rStrm
,
306 RTL_TEXTENCODING_ASCII_US
);
307 rStrm
>> bAddRemoveOk
;
312 sal_Bool
SbxStdCollection::StoreData( SvStream
& rStrm
) const
314 sal_Bool bRes
= SbxCollection::StoreData( rStrm
);
317 write_lenPrefixed_uInt8s_FromOUString
<sal_uInt16
>(rStrm
, aElemClass
,
318 RTL_TEXTENCODING_ASCII_US
);
319 rStrm
<< bAddRemoveOk
;
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */