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