xref: /trunk/main/basic/source/sbx/sbxcoll.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basic.hxx"
30 
31 
32 #include <tools/stream.hxx>
33 
34 #include <basic/sbx.hxx>
35 #include "sbxres.hxx"
36 
37 TYPEINIT1(SbxCollection,SbxObject)
38 TYPEINIT1(SbxStdCollection,SbxCollection)
39 
40 static const char* pCount;
41 static const char* pAdd;
42 static const char* pItem;
43 static const char* pRemove;
44 static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
45 
46 /////////////////////////////////////////////////////////////////////////
47 
48 SbxCollection::SbxCollection( const XubString& rClass )
49 			 : SbxObject( rClass )
50 {
51 	if( !nCountHash )
52 	{
53 		pCount  = GetSbxRes( STRING_COUNTPROP );
54 		pAdd    = GetSbxRes( STRING_ADDMETH );
55 		pItem   = GetSbxRes( STRING_ITEMMETH );
56 		pRemove = GetSbxRes( STRING_REMOVEMETH );
57 		nCountHash  = MakeHashCode( String::CreateFromAscii( pCount ) );
58 		nAddHash    = MakeHashCode( String::CreateFromAscii( pAdd ) );
59 		nItemHash   = MakeHashCode( String::CreateFromAscii( pItem ) );
60 		nRemoveHash = MakeHashCode( String::CreateFromAscii( pRemove ) );
61 	}
62 	Initialize();
63 	// Fuer Zugriffe auf sich selbst
64 	StartListening( GetBroadcaster(), sal_True );
65 }
66 
67 SbxCollection::SbxCollection( const SbxCollection& rColl )
68     : SvRefBase( rColl ), SbxObject( rColl )
69 {}
70 
71 SbxCollection& SbxCollection::operator=( const SbxCollection& r )
72 {
73 	if( &r != this )
74 		SbxObject::operator=( r );
75 	return *this;
76 }
77 
78 SbxCollection::~SbxCollection()
79 {}
80 
81 void SbxCollection::Clear()
82 {
83 	SbxObject::Clear();
84 	Initialize();
85 }
86 
87 void SbxCollection::Initialize()
88 {
89 	SetType( SbxOBJECT );
90 	SetFlag( SBX_FIXED );
91 	ResetFlag( SBX_WRITE );
92 	SbxVariable* p;
93 	p = Make( String::CreateFromAscii( pCount ), SbxCLASS_PROPERTY, SbxINTEGER );
94 	p->ResetFlag( SBX_WRITE );
95 	p->SetFlag( SBX_DONTSTORE );
96 	p = Make( String::CreateFromAscii( pAdd ), SbxCLASS_METHOD, SbxEMPTY );
97 	p->SetFlag( SBX_DONTSTORE );
98 	p = Make( String::CreateFromAscii( pItem ), SbxCLASS_METHOD, SbxOBJECT );
99 	p->SetFlag( SBX_DONTSTORE );
100 	p = Make( String::CreateFromAscii( pRemove ), SbxCLASS_METHOD, SbxEMPTY );
101 	p->SetFlag( SBX_DONTSTORE );
102 }
103 
104 SbxVariable* SbxCollection::FindUserData( sal_uInt32 nData )
105 {
106 	if( GetParameters() )
107 	{
108 		SbxObject* pObj = (SbxObject*) GetObject();
109 		return pObj ? pObj->FindUserData( nData ) : NULL;
110 	}
111 	else
112 		return SbxObject::FindUserData( nData );
113 }
114 
115 SbxVariable* SbxCollection::Find( const XubString& rName, SbxClassType t )
116 {
117 	if( GetParameters() )
118 	{
119 		SbxObject* pObj = (SbxObject*) GetObject();
120 		return pObj ? pObj->Find( rName, t ) : NULL;
121 	}
122 	else
123 		return SbxObject::Find( rName, t );
124 }
125 
126 void SbxCollection::SFX_NOTIFY( SfxBroadcaster& rCst, const TypeId& rId1,
127 								const SfxHint& rHint, const TypeId& rId2 )
128 {
129 	const SbxHint* p = PTR_CAST(SbxHint,&rHint);
130 	if( p )
131 	{
132 		sal_uIntPtr nId = p->GetId();
133 		sal_Bool bRead  = sal_Bool( nId == SBX_HINT_DATAWANTED );
134 		sal_Bool bWrite = sal_Bool( nId == SBX_HINT_DATACHANGED );
135 		SbxVariable* pVar = p->GetVar();
136 		SbxArray* pArg = pVar->GetParameters();
137 		if( bRead || bWrite )
138 		{
139 			XubString aVarName( pVar->GetName() );
140 			if( pVar == this )
141 				CollItem( pArg );
142 			else if( pVar->GetHashCode() == nCountHash
143 				  && aVarName.EqualsIgnoreCaseAscii( pCount ) )
144 				pVar->PutLong( pObjs->Count() );
145 			else if( pVar->GetHashCode() == nAddHash
146 				  && aVarName.EqualsIgnoreCaseAscii( pAdd ) )
147 				CollAdd( pArg );
148 			else if( pVar->GetHashCode() == nItemHash
149 				  && aVarName.EqualsIgnoreCaseAscii( pItem ) )
150 				CollItem( pArg );
151 			else if( pVar->GetHashCode() == nRemoveHash
152 				  && aVarName.EqualsIgnoreCaseAscii( pRemove ) )
153 				CollRemove( pArg );
154 			else
155 				SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
156 			return;
157 		}
158 	}
159 	SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
160 }
161 
162 // Default: Argument ist Objekt
163 
164 void SbxCollection::CollAdd( SbxArray* pPar_ )
165 {
166 	if( pPar_->Count() != 2 )
167 		SetError( SbxERR_WRONG_ARGS );
168 	else
169 	{
170 		SbxBase* pObj = pPar_->Get( 1 )->GetObject();
171 		if( !pObj || !( pObj->ISA(SbxObject) ) )
172 			SetError( SbxERR_NOTIMP );
173 		else
174 			Insert( (SbxObject*) pObj );
175 	}
176 }
177 
178 // Default: Index ab 1 oder der Objektname
179 
180 void SbxCollection::CollItem( SbxArray* pPar_ )
181 {
182 	if( pPar_->Count() != 2 )
183 		SetError( SbxERR_WRONG_ARGS );
184 	else
185 	{
186 		SbxVariable* pRes = NULL;
187 		SbxVariable* p = pPar_->Get( 1 );
188 		if( p->GetType() == SbxSTRING )
189 			pRes = Find( p->GetString(), SbxCLASS_OBJECT );
190 		else
191 		{
192 			short n = p->GetInteger();
193 			if( n >= 1 && n <= (short) pObjs->Count() )
194 				pRes = pObjs->Get( (sal_uInt16) n - 1 );
195 		}
196 		if( !pRes )
197 			SetError( SbxERR_BAD_INDEX );
198 		pPar_->Get( 0 )->PutObject( pRes );
199 	}
200 }
201 
202 // Default: Index ab 1
203 
204 void SbxCollection::CollRemove( SbxArray* pPar_ )
205 {
206 	if( pPar_->Count() != 2 )
207 		SetError( SbxERR_WRONG_ARGS );
208 	else
209 	{
210 		short n = pPar_->Get( 1 )->GetInteger();
211 		if( n < 1 || n > (short) pObjs->Count() )
212 			SetError( SbxERR_BAD_INDEX );
213 		else
214 			Remove( pObjs->Get( (sal_uInt16) n - 1 ) );
215 	}
216 }
217 
218 sal_Bool SbxCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
219 {
220 	sal_Bool bRes = SbxObject::LoadData( rStrm, nVer );
221 	Initialize();
222 	return bRes;
223 }
224 
225 /////////////////////////////////////////////////////////////////////////
226 
227 SbxStdCollection::SbxStdCollection
228 					( const XubString& rClass, const XubString& rElem, sal_Bool b )
229 				  : SbxCollection( rClass ), aElemClass( rElem ),
230 					bAddRemoveOk( b )
231 {}
232 
233 SbxStdCollection::SbxStdCollection( const SbxStdCollection& r )
234 				  : SvRefBase( r ), SbxCollection( r ),
235                     aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
236 {}
237 
238 SbxStdCollection& SbxStdCollection::operator=( const SbxStdCollection& r )
239 {
240 	if( &r != this )
241 	{
242 		if( !r.aElemClass.EqualsIgnoreCaseAscii( aElemClass ) )
243 			SetError( SbxERR_CONVERSION );
244 		else
245 			SbxCollection::operator=( r );
246 	}
247 	return *this;
248 }
249 
250 SbxStdCollection::~SbxStdCollection()
251 {}
252 
253 // Default: Fehler, wenn falsches Objekt
254 
255 void SbxStdCollection::Insert( SbxVariable* p )
256 {
257 	SbxObject* pObj = PTR_CAST(SbxObject,p);
258 	if( pObj && !pObj->IsClass( aElemClass ) )
259 		SetError( SbxERR_BAD_ACTION );
260 	else
261 		SbxCollection::Insert( p );
262 }
263 
264 void SbxStdCollection::CollAdd( SbxArray* pPar_ )
265 {
266 	if( !bAddRemoveOk )
267 		SetError( SbxERR_BAD_ACTION );
268 	else
269 		SbxCollection::CollAdd( pPar_ );
270 }
271 
272 void SbxStdCollection::CollRemove( SbxArray* pPar_ )
273 {
274 	if( !bAddRemoveOk )
275 		SetError( SbxERR_BAD_ACTION );
276 	else
277 		SbxCollection::CollRemove( pPar_ );
278 }
279 
280 sal_Bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
281 {
282 	sal_Bool bRes = SbxCollection::LoadData( rStrm, nVer );
283 	if( bRes )
284 	{
285 		rStrm.ReadByteString( aElemClass, RTL_TEXTENCODING_ASCII_US );
286 		rStrm >> bAddRemoveOk;
287 	}
288 	return bRes;
289 }
290 
291 sal_Bool SbxStdCollection::StoreData( SvStream& rStrm ) const
292 {
293 	sal_Bool bRes = SbxCollection::StoreData( rStrm );
294 	if( bRes )
295 	{
296 		rStrm.WriteByteString( aElemClass, RTL_TEXTENCODING_ASCII_US );
297 		rStrm << bAddRemoveOk;
298 	}
299 	return bRes;
300 }
301 
302