update dev300-m58
[ooovba.git] / basic / source / sbx / sbxcoll.cxx
blob64af3ac8e007f3c3de1416a90def28463c9706f1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sbxcoll.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
35 #include <tools/stream.hxx>
37 #include <basic/sbx.hxx>
38 #include "sbxres.hxx"
40 TYPEINIT1(SbxCollection,SbxObject)
41 TYPEINIT1(SbxStdCollection,SbxCollection)
43 static const char* pCount;
44 static const char* pAdd;
45 static const char* pItem;
46 static const char* pRemove;
47 static USHORT nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
49 /////////////////////////////////////////////////////////////////////////
51 SbxCollection::SbxCollection( const XubString& rClass )
52 : SbxObject( rClass )
54 if( !nCountHash )
56 pCount = GetSbxRes( STRING_COUNTPROP );
57 pAdd = GetSbxRes( STRING_ADDMETH );
58 pItem = GetSbxRes( STRING_ITEMMETH );
59 pRemove = GetSbxRes( STRING_REMOVEMETH );
60 nCountHash = MakeHashCode( String::CreateFromAscii( pCount ) );
61 nAddHash = MakeHashCode( String::CreateFromAscii( pAdd ) );
62 nItemHash = MakeHashCode( String::CreateFromAscii( pItem ) );
63 nRemoveHash = MakeHashCode( String::CreateFromAscii( pRemove ) );
65 Initialize();
66 // Fuer Zugriffe auf sich selbst
67 StartListening( GetBroadcaster(), TRUE );
70 SbxCollection::SbxCollection( const SbxCollection& rColl )
71 : SvRefBase( rColl ), SbxObject( rColl )
74 SbxCollection& SbxCollection::operator=( const SbxCollection& r )
76 if( &r != this )
77 SbxObject::operator=( r );
78 return *this;
81 SbxCollection::~SbxCollection()
84 void SbxCollection::Clear()
86 SbxObject::Clear();
87 Initialize();
90 void SbxCollection::Initialize()
92 SetType( SbxOBJECT );
93 SetFlag( SBX_FIXED );
94 ResetFlag( SBX_WRITE );
95 SbxVariable* p;
96 p = Make( String::CreateFromAscii( pCount ), SbxCLASS_PROPERTY, SbxINTEGER );
97 p->ResetFlag( SBX_WRITE );
98 p->SetFlag( SBX_DONTSTORE );
99 p = Make( String::CreateFromAscii( pAdd ), SbxCLASS_METHOD, SbxEMPTY );
100 p->SetFlag( SBX_DONTSTORE );
101 p = Make( String::CreateFromAscii( pItem ), SbxCLASS_METHOD, SbxOBJECT );
102 p->SetFlag( SBX_DONTSTORE );
103 p = Make( String::CreateFromAscii( pRemove ), SbxCLASS_METHOD, SbxEMPTY );
104 p->SetFlag( SBX_DONTSTORE );
107 SbxVariable* SbxCollection::FindUserData( UINT32 nData )
109 if( GetParameters() )
111 SbxObject* pObj = (SbxObject*) GetObject();
112 return pObj ? pObj->FindUserData( nData ) : NULL;
114 else
115 return SbxObject::FindUserData( nData );
118 SbxVariable* SbxCollection::Find( const XubString& rName, SbxClassType t )
120 if( GetParameters() )
122 SbxObject* pObj = (SbxObject*) GetObject();
123 return pObj ? pObj->Find( rName, t ) : NULL;
125 else
126 return SbxObject::Find( rName, t );
129 void SbxCollection::SFX_NOTIFY( SfxBroadcaster& rCst, const TypeId& rId1,
130 const SfxHint& rHint, const TypeId& rId2 )
132 const SbxHint* p = PTR_CAST(SbxHint,&rHint);
133 if( p )
135 ULONG nId = p->GetId();
136 BOOL bRead = BOOL( nId == SBX_HINT_DATAWANTED );
137 BOOL bWrite = BOOL( nId == SBX_HINT_DATACHANGED );
138 SbxVariable* pVar = p->GetVar();
139 SbxArray* pArg = pVar->GetParameters();
140 if( bRead || bWrite )
142 XubString aVarName( pVar->GetName() );
143 if( pVar == this )
144 CollItem( pArg );
145 else if( pVar->GetHashCode() == nCountHash
146 && aVarName.EqualsIgnoreCaseAscii( pCount ) )
147 pVar->PutLong( pObjs->Count() );
148 else if( pVar->GetHashCode() == nAddHash
149 && aVarName.EqualsIgnoreCaseAscii( pAdd ) )
150 CollAdd( pArg );
151 else if( pVar->GetHashCode() == nItemHash
152 && aVarName.EqualsIgnoreCaseAscii( pItem ) )
153 CollItem( pArg );
154 else if( pVar->GetHashCode() == nRemoveHash
155 && aVarName.EqualsIgnoreCaseAscii( pRemove ) )
156 CollRemove( pArg );
157 else
158 SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
159 return;
162 SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
165 // Default: Argument ist Objekt
167 void SbxCollection::CollAdd( SbxArray* pPar_ )
169 if( pPar_->Count() != 2 )
170 SetError( SbxERR_WRONG_ARGS );
171 else
173 SbxBase* pObj = pPar_->Get( 1 )->GetObject();
174 if( !pObj || !( pObj->ISA(SbxObject) ) )
175 SetError( SbxERR_NOTIMP );
176 else
177 Insert( (SbxObject*) pObj );
181 // Default: Index ab 1 oder der Objektname
183 void SbxCollection::CollItem( SbxArray* pPar_ )
185 if( pPar_->Count() != 2 )
186 SetError( SbxERR_WRONG_ARGS );
187 else
189 SbxVariable* pRes = NULL;
190 SbxVariable* p = pPar_->Get( 1 );
191 if( p->GetType() == SbxSTRING )
192 pRes = Find( p->GetString(), SbxCLASS_OBJECT );
193 else
195 short n = p->GetInteger();
196 if( n >= 1 && n <= (short) pObjs->Count() )
197 pRes = pObjs->Get( (USHORT) n - 1 );
199 if( !pRes )
200 SetError( SbxERR_BAD_INDEX );
201 pPar_->Get( 0 )->PutObject( pRes );
205 // Default: Index ab 1
207 void SbxCollection::CollRemove( SbxArray* pPar_ )
209 if( pPar_->Count() != 2 )
210 SetError( SbxERR_WRONG_ARGS );
211 else
213 short n = pPar_->Get( 1 )->GetInteger();
214 if( n < 1 || n > (short) pObjs->Count() )
215 SetError( SbxERR_BAD_INDEX );
216 else
217 Remove( pObjs->Get( (USHORT) n - 1 ) );
221 BOOL SbxCollection::LoadData( SvStream& rStrm, USHORT nVer )
223 BOOL bRes = SbxObject::LoadData( rStrm, nVer );
224 Initialize();
225 return bRes;
228 /////////////////////////////////////////////////////////////////////////
230 SbxStdCollection::SbxStdCollection
231 ( const XubString& rClass, const XubString& rElem, BOOL b )
232 : SbxCollection( rClass ), aElemClass( rElem ),
233 bAddRemoveOk( b )
236 SbxStdCollection::SbxStdCollection( const SbxStdCollection& r )
237 : SvRefBase( r ), SbxCollection( r ),
238 aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
241 SbxStdCollection& SbxStdCollection::operator=( const SbxStdCollection& r )
243 if( &r != this )
245 if( !r.aElemClass.EqualsIgnoreCaseAscii( aElemClass ) )
246 SetError( SbxERR_CONVERSION );
247 else
248 SbxCollection::operator=( r );
250 return *this;
253 SbxStdCollection::~SbxStdCollection()
256 // Default: Fehler, wenn falsches Objekt
258 void SbxStdCollection::Insert( SbxVariable* p )
260 SbxObject* pObj = PTR_CAST(SbxObject,p);
261 if( pObj && !pObj->IsClass( aElemClass ) )
262 SetError( SbxERR_BAD_ACTION );
263 else
264 SbxCollection::Insert( p );
267 void SbxStdCollection::CollAdd( SbxArray* pPar_ )
269 if( !bAddRemoveOk )
270 SetError( SbxERR_BAD_ACTION );
271 else
272 SbxCollection::CollAdd( pPar_ );
275 void SbxStdCollection::CollRemove( SbxArray* pPar_ )
277 if( !bAddRemoveOk )
278 SetError( SbxERR_BAD_ACTION );
279 else
280 SbxCollection::CollRemove( pPar_ );
283 BOOL SbxStdCollection::LoadData( SvStream& rStrm, USHORT nVer )
285 BOOL bRes = SbxCollection::LoadData( rStrm, nVer );
286 if( bRes )
288 rStrm.ReadByteString( aElemClass, RTL_TEXTENCODING_ASCII_US );
289 rStrm >> bAddRemoveOk;
291 return bRes;
294 BOOL SbxStdCollection::StoreData( SvStream& rStrm ) const
296 BOOL bRes = SbxCollection::StoreData( rStrm );
297 if( bRes )
299 rStrm.WriteByteString( aElemClass, RTL_TEXTENCODING_ASCII_US );
300 rStrm << bAddRemoveOk;
302 return bRes;