1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sbxcoll.cxx,v $
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>
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
)
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
) );
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
)
77 SbxObject::operator=( r
);
81 SbxCollection::~SbxCollection()
84 void SbxCollection::Clear()
90 void SbxCollection::Initialize()
94 ResetFlag( SBX_WRITE
);
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
;
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
;
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
);
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() );
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
) )
151 else if( pVar
->GetHashCode() == nItemHash
152 && aVarName
.EqualsIgnoreCaseAscii( pItem
) )
154 else if( pVar
->GetHashCode() == nRemoveHash
155 && aVarName
.EqualsIgnoreCaseAscii( pRemove
) )
158 SbxObject::SFX_NOTIFY( rCst
, rId1
, rHint
, rId2
);
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
);
173 SbxBase
* pObj
= pPar_
->Get( 1 )->GetObject();
174 if( !pObj
|| !( pObj
->ISA(SbxObject
) ) )
175 SetError( SbxERR_NOTIMP
);
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
);
189 SbxVariable
* pRes
= NULL
;
190 SbxVariable
* p
= pPar_
->Get( 1 );
191 if( p
->GetType() == SbxSTRING
)
192 pRes
= Find( p
->GetString(), SbxCLASS_OBJECT
);
195 short n
= p
->GetInteger();
196 if( n
>= 1 && n
<= (short) pObjs
->Count() )
197 pRes
= pObjs
->Get( (USHORT
) n
- 1 );
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
);
213 short n
= pPar_
->Get( 1 )->GetInteger();
214 if( n
< 1 || n
> (short) pObjs
->Count() )
215 SetError( SbxERR_BAD_INDEX
);
217 Remove( pObjs
->Get( (USHORT
) n
- 1 ) );
221 BOOL
SbxCollection::LoadData( SvStream
& rStrm
, USHORT nVer
)
223 BOOL bRes
= SbxObject::LoadData( rStrm
, nVer
);
228 /////////////////////////////////////////////////////////////////////////
230 SbxStdCollection::SbxStdCollection
231 ( const XubString
& rClass
, const XubString
& rElem
, BOOL b
)
232 : SbxCollection( rClass
), aElemClass( rElem
),
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
)
245 if( !r
.aElemClass
.EqualsIgnoreCaseAscii( aElemClass
) )
246 SetError( SbxERR_CONVERSION
);
248 SbxCollection::operator=( r
);
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
);
264 SbxCollection::Insert( p
);
267 void SbxStdCollection::CollAdd( SbxArray
* pPar_
)
270 SetError( SbxERR_BAD_ACTION
);
272 SbxCollection::CollAdd( pPar_
);
275 void SbxStdCollection::CollRemove( SbxArray
* pPar_
)
278 SetError( SbxERR_BAD_ACTION
);
280 SbxCollection::CollRemove( pPar_
);
283 BOOL
SbxStdCollection::LoadData( SvStream
& rStrm
, USHORT nVer
)
285 BOOL bRes
= SbxCollection::LoadData( rStrm
, nVer
);
288 rStrm
.ReadByteString( aElemClass
, RTL_TEXTENCODING_ASCII_US
);
289 rStrm
>> bAddRemoveOk
;
294 BOOL
SbxStdCollection::StoreData( SvStream
& rStrm
) const
296 BOOL bRes
= SbxCollection::StoreData( rStrm
);
299 rStrm
.WriteByteString( aElemClass
, RTL_TEXTENCODING_ASCII_US
);
300 rStrm
<< bAddRemoveOk
;