Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / basic / source / sbx / sbxcoll.cxx
blob40b4a9fb2fd737510942eaf787eef5f185dc0a92
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "sbxres.hxx"
25 TYPEINIT1(SbxCollection,SbxObject)
26 TYPEINIT1(SbxStdCollection,SbxCollection)
28 static OUString pCount;
29 static OUString pAdd;
30 static OUString pItem;
31 static OUString pRemove;
32 static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
35 SbxCollection::SbxCollection( const OUString& rClass )
36 : SbxObject( rClass )
38 if( !nCountHash )
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 );
49 Initialize();
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 )
60 if( &r != this )
61 SbxObject::operator=( r );
62 return *this;
65 SbxCollection::~SbxCollection()
68 void SbxCollection::Clear()
70 SbxObject::Clear();
71 Initialize();
74 void SbxCollection::Initialize()
76 SetType( SbxOBJECT );
77 SetFlag( SBX_FIXED );
78 ResetFlag( SBX_WRITE );
79 SbxVariable* p;
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 )
93 if( GetParameters() )
95 SbxObject* pObj = (SbxObject*) GetObject();
96 return pObj ? pObj->FindUserData( nData ) : NULL;
98 else
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;
111 else
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);
121 if( p )
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() );
131 if( pVar == this )
133 CollItem( pArg );
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 ) )
143 CollAdd( pArg );
145 else if( pVar->GetHashCode() == nItemHash
146 && aVarName.equalsIgnoreAsciiCase( pItem ) )
148 CollItem( pArg );
150 else if( pVar->GetHashCode() == nRemoveHash
151 && aVarName.equalsIgnoreAsciiCase( pRemove ) )
153 CollRemove( pArg );
155 else
157 SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
159 return;
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 );
173 else
175 SbxBase* pObj = pPar_->Get( 1 )->GetObject();
176 if( !pObj || !( pObj->ISA(SbxObject) ) )
178 SetError( SbxERR_NOTIMP );
180 else
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 );
195 else
197 SbxVariable* pRes = NULL;
198 SbxVariable* p = pPar_->Get( 1 );
199 if( p->GetType() == SbxSTRING )
201 pRes = Find( p->GetOUString(), SbxCLASS_OBJECT );
203 else
205 short n = p->GetInteger();
206 if( n >= 1 && n <= (short) pObjs->Count() )
208 pRes = pObjs->Get( (sal_uInt16) n - 1 );
211 if( !pRes )
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 );
225 else
227 short n = pPar_->Get( 1 )->GetInteger();
228 if( n < 1 || n > (short) pObjs->Count() )
229 SetError( SbxERR_BAD_INDEX );
230 else
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 );
238 Initialize();
239 return bRes;
243 SbxStdCollection::SbxStdCollection
244 ( const OUString& rClass, const OUString& rElem, sal_Bool b )
245 : SbxCollection( rClass ), aElemClass( rElem ),
246 bAddRemoveOk( b )
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 )
256 if( &r != this )
258 if( !r.aElemClass.equalsIgnoreAsciiCase( aElemClass ) )
260 SetError( SbxERR_CONVERSION );
262 else
264 SbxCollection::operator=( r );
267 return *this;
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 );
280 else
281 SbxCollection::Insert( p );
284 void SbxStdCollection::CollAdd( SbxArray* pPar_ )
286 if( !bAddRemoveOk )
287 SetError( SbxERR_BAD_ACTION );
288 else
289 SbxCollection::CollAdd( pPar_ );
292 void SbxStdCollection::CollRemove( SbxArray* pPar_ )
294 if( !bAddRemoveOk )
295 SetError( SbxERR_BAD_ACTION );
296 else
297 SbxCollection::CollRemove( pPar_ );
300 sal_Bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
302 sal_Bool bRes = SbxCollection::LoadData( rStrm, nVer );
303 if( bRes )
305 aElemClass = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(rStrm,
306 RTL_TEXTENCODING_ASCII_US);
307 rStrm >> bAddRemoveOk;
309 return bRes;
312 sal_Bool SbxStdCollection::StoreData( SvStream& rStrm ) const
314 sal_Bool bRes = SbxCollection::StoreData( rStrm );
315 if( bRes )
317 write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(rStrm, aElemClass,
318 RTL_TEXTENCODING_ASCII_US);
319 rStrm << bAddRemoveOk;
321 return bRes;
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */