Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include <tools/stream.hxx>
21 : :
22 : : #include <basic/sbx.hxx>
23 : : #include "sbxres.hxx"
24 : :
25 [ # # ][ # # ]: 3357 : TYPEINIT1(SbxCollection,SbxObject)
26 [ # # ][ # # ]: 0 : TYPEINIT1(SbxStdCollection,SbxCollection)
27 : :
28 : : static const char* pCount;
29 : : static const char* pAdd;
30 : : static const char* pItem;
31 : : static const char* pRemove;
32 : : static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
33 : :
34 : :
35 : 0 : SbxCollection::SbxCollection( const XubString& rClass )
36 [ # # ][ # # ]: 0 : : SbxObject( rClass )
37 : : {
38 [ # # ][ # # ]: 0 : if( !nCountHash )
39 : : {
40 [ # # ][ # # ]: 0 : pCount = GetSbxRes( STRING_COUNTPROP );
41 [ # # ][ # # ]: 0 : pAdd = GetSbxRes( STRING_ADDMETH );
42 [ # # ][ # # ]: 0 : pItem = GetSbxRes( STRING_ITEMMETH );
43 [ # # ][ # # ]: 0 : pRemove = GetSbxRes( STRING_REMOVEMETH );
44 [ # # ][ # # ]: 0 : nCountHash = MakeHashCode( rtl::OUString::createFromAscii( pCount ) );
[ # # ][ # # ]
[ # # ][ # # ]
45 [ # # ][ # # ]: 0 : nAddHash = MakeHashCode( rtl::OUString::createFromAscii( pAdd ) );
[ # # ][ # # ]
[ # # ][ # # ]
46 [ # # ][ # # ]: 0 : nItemHash = MakeHashCode( rtl::OUString::createFromAscii( pItem ) );
[ # # ][ # # ]
[ # # ][ # # ]
47 [ # # ][ # # ]: 0 : nRemoveHash = MakeHashCode( rtl::OUString::createFromAscii( pRemove ) );
[ # # ][ # # ]
[ # # ][ # # ]
48 : : }
49 [ # # ][ # # ]: 0 : Initialize();
50 : : // For Access on itself
51 [ # # ][ # # ]: 0 : StartListening( GetBroadcaster(), sal_True );
[ # # ][ # # ]
52 : 0 : }
53 : :
54 : 0 : SbxCollection::SbxCollection( const SbxCollection& rColl )
55 [ # # ][ # # ]: 0 : : SvRefBase( rColl ), SbxObject( rColl )
56 : 0 : {}
57 : :
58 : 0 : SbxCollection& SbxCollection::operator=( const SbxCollection& r )
59 : : {
60 [ # # ]: 0 : if( &r != this )
61 : 0 : SbxObject::operator=( r );
62 : 0 : return *this;
63 : : }
64 : :
65 [ # # ][ # # ]: 0 : SbxCollection::~SbxCollection()
66 [ # # ][ # # ]: 0 : {}
[ # # ][ # # ]
[ # # ][ # # ]
67 : :
68 : 0 : void SbxCollection::Clear()
69 : : {
70 : 0 : SbxObject::Clear();
71 : 0 : Initialize();
72 : 0 : }
73 : :
74 : 0 : void SbxCollection::Initialize()
75 : : {
76 : 0 : SetType( SbxOBJECT );
77 : 0 : SetFlag( SBX_FIXED );
78 : 0 : ResetFlag( SBX_WRITE );
79 : : SbxVariable* p;
80 [ # # ][ # # ]: 0 : p = Make( rtl::OUString::createFromAscii( pCount ), SbxCLASS_PROPERTY, SbxINTEGER );
[ # # ]
81 : 0 : p->ResetFlag( SBX_WRITE );
82 : 0 : p->SetFlag( SBX_DONTSTORE );
83 [ # # ][ # # ]: 0 : p = Make( rtl::OUString::createFromAscii( pAdd ), SbxCLASS_METHOD, SbxEMPTY );
[ # # ]
84 : 0 : p->SetFlag( SBX_DONTSTORE );
85 [ # # ][ # # ]: 0 : p = Make( rtl::OUString::createFromAscii( pItem ), SbxCLASS_METHOD, SbxOBJECT );
[ # # ]
86 : 0 : p->SetFlag( SBX_DONTSTORE );
87 [ # # ][ # # ]: 0 : p = Make( rtl::OUString::createFromAscii( pRemove ), SbxCLASS_METHOD, SbxEMPTY );
[ # # ]
88 : 0 : p->SetFlag( SBX_DONTSTORE );
89 : 0 : }
90 : :
91 : 0 : SbxVariable* SbxCollection::FindUserData( sal_uInt32 nData )
92 : : {
93 [ # # ]: 0 : if( GetParameters() )
94 : : {
95 : 0 : SbxObject* pObj = (SbxObject*) GetObject();
96 [ # # ]: 0 : return pObj ? pObj->FindUserData( nData ) : NULL;
97 : : }
98 : : else
99 : 0 : return SbxObject::FindUserData( nData );
100 : : }
101 : :
102 : 0 : SbxVariable* SbxCollection::Find( const rtl::OUString& rName, SbxClassType t )
103 : : {
104 [ # # ]: 0 : if( GetParameters() )
105 : : {
106 : 0 : SbxObject* pObj = (SbxObject*) GetObject();
107 [ # # ]: 0 : return pObj ? pObj->Find( rName, t ) : NULL;
108 : : }
109 : : else
110 : 0 : return SbxObject::Find( rName, t );
111 : : }
112 : :
113 : 0 : void SbxCollection::SFX_NOTIFY( SfxBroadcaster& rCst, const TypeId& rId1,
114 : : const SfxHint& rHint, const TypeId& rId2 )
115 : : {
116 [ # # ][ # # ]: 0 : const SbxHint* p = PTR_CAST(SbxHint,&rHint);
117 [ # # ]: 0 : if( p )
118 : : {
119 : 0 : sal_uIntPtr nId = p->GetId();
120 : 0 : bool bRead = ( nId == SBX_HINT_DATAWANTED );
121 : 0 : bool bWrite = ( nId == SBX_HINT_DATACHANGED );
122 : 0 : SbxVariable* pVar = p->GetVar();
123 : 0 : SbxArray* pArg = pVar->GetParameters();
124 [ # # ][ # # ]: 0 : if( bRead || bWrite )
125 : : {
126 [ # # ][ # # ]: 0 : XubString aVarName( pVar->GetName() );
127 [ # # ]: 0 : if( pVar == this )
128 [ # # ]: 0 : CollItem( pArg );
129 [ # # ][ # # ]: 0 : else if( pVar->GetHashCode() == nCountHash
[ # # ]
130 [ # # ]: 0 : && aVarName.EqualsIgnoreCaseAscii( pCount ) )
131 [ # # ][ # # ]: 0 : pVar->PutLong( pObjs->Count() );
132 [ # # ][ # # ]: 0 : else if( pVar->GetHashCode() == nAddHash
[ # # ]
133 [ # # ]: 0 : && aVarName.EqualsIgnoreCaseAscii( pAdd ) )
134 [ # # ]: 0 : CollAdd( pArg );
135 [ # # ][ # # ]: 0 : else if( pVar->GetHashCode() == nItemHash
[ # # ]
136 [ # # ]: 0 : && aVarName.EqualsIgnoreCaseAscii( pItem ) )
137 [ # # ]: 0 : CollItem( pArg );
138 [ # # ][ # # ]: 0 : else if( pVar->GetHashCode() == nRemoveHash
[ # # ]
139 [ # # ]: 0 : && aVarName.EqualsIgnoreCaseAscii( pRemove ) )
140 [ # # ]: 0 : CollRemove( pArg );
141 : : else
142 [ # # ]: 0 : SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
143 [ # # ]: 0 : return;
144 : : }
145 : : }
146 : 0 : SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
147 : : }
148 : :
149 : : // Default: argument is object
150 : :
151 : 0 : void SbxCollection::CollAdd( SbxArray* pPar_ )
152 : : {
153 [ # # ]: 0 : if( pPar_->Count() != 2 )
154 : 0 : SetError( SbxERR_WRONG_ARGS );
155 : : else
156 : : {
157 : 0 : SbxBase* pObj = pPar_->Get( 1 )->GetObject();
158 [ # # ][ # # ]: 0 : if( !pObj || !( pObj->ISA(SbxObject) ) )
[ # # ]
159 : 0 : SetError( SbxERR_NOTIMP );
160 : : else
161 : 0 : Insert( (SbxObject*) pObj );
162 : : }
163 : 0 : }
164 : :
165 : : // Default: index from 1 or object name
166 : :
167 : 0 : void SbxCollection::CollItem( SbxArray* pPar_ )
168 : : {
169 [ # # ]: 0 : if( pPar_->Count() != 2 )
170 : 0 : SetError( SbxERR_WRONG_ARGS );
171 : : else
172 : : {
173 : 0 : SbxVariable* pRes = NULL;
174 : 0 : SbxVariable* p = pPar_->Get( 1 );
175 [ # # ]: 0 : if( p->GetType() == SbxSTRING )
176 [ # # ]: 0 : pRes = Find( p->GetString(), SbxCLASS_OBJECT );
177 : : else
178 : : {
179 : 0 : short n = p->GetInteger();
180 [ # # ][ # # ]: 0 : if( n >= 1 && n <= (short) pObjs->Count() )
[ # # ]
181 : 0 : pRes = pObjs->Get( (sal_uInt16) n - 1 );
182 : : }
183 [ # # ]: 0 : if( !pRes )
184 : 0 : SetError( SbxERR_BAD_INDEX );
185 : 0 : pPar_->Get( 0 )->PutObject( pRes );
186 : : }
187 : 0 : }
188 : :
189 : : // Default: index from 1
190 : :
191 : 0 : void SbxCollection::CollRemove( SbxArray* pPar_ )
192 : : {
193 [ # # ]: 0 : if( pPar_->Count() != 2 )
194 : 0 : SetError( SbxERR_WRONG_ARGS );
195 : : else
196 : : {
197 : 0 : short n = pPar_->Get( 1 )->GetInteger();
198 [ # # ][ # # ]: 0 : if( n < 1 || n > (short) pObjs->Count() )
[ # # ]
199 : 0 : SetError( SbxERR_BAD_INDEX );
200 : : else
201 : 0 : Remove( pObjs->Get( (sal_uInt16) n - 1 ) );
202 : : }
203 : 0 : }
204 : :
205 : 0 : sal_Bool SbxCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
206 : : {
207 : 0 : sal_Bool bRes = SbxObject::LoadData( rStrm, nVer );
208 : 0 : Initialize();
209 : 0 : return bRes;
210 : : }
211 : :
212 : :
213 : 0 : SbxStdCollection::SbxStdCollection
214 : : ( const XubString& rClass, const XubString& rElem, sal_Bool b )
215 : : : SbxCollection( rClass ), aElemClass( rElem ),
216 [ # # ][ # # ]: 0 : bAddRemoveOk( b )
[ # # ][ # # ]
217 : 0 : {}
218 : :
219 : 0 : SbxStdCollection::SbxStdCollection( const SbxStdCollection& r )
220 : : : SvRefBase( r ), SbxCollection( r ),
221 [ # # ][ # # ]: 0 : aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
[ # # ][ # # ]
222 : 0 : {}
223 : :
224 : 0 : SbxStdCollection& SbxStdCollection::operator=( const SbxStdCollection& r )
225 : : {
226 [ # # ]: 0 : if( &r != this )
227 : : {
228 [ # # ]: 0 : if( !r.aElemClass.EqualsIgnoreCaseAscii( aElemClass ) )
229 : 0 : SetError( SbxERR_CONVERSION );
230 : : else
231 : 0 : SbxCollection::operator=( r );
232 : : }
233 : 0 : return *this;
234 : : }
235 : :
236 [ # # ][ # # ]: 0 : SbxStdCollection::~SbxStdCollection()
[ # # ][ # # ]
237 [ # # ][ # # ]: 0 : {}
[ # # ][ # # ]
[ # # ][ # # ]
238 : :
239 : : // Default: Error, if wrong object
240 : :
241 : 0 : void SbxStdCollection::Insert( SbxVariable* p )
242 : : {
243 [ # # ][ # # ]: 0 : SbxObject* pObj = PTR_CAST(SbxObject,p);
244 [ # # ][ # # ]: 0 : if( pObj && !pObj->IsClass( aElemClass ) )
[ # # ][ # # ]
[ # # ]
[ # # # # ]
245 : 0 : SetError( SbxERR_BAD_ACTION );
246 : : else
247 : 0 : SbxCollection::Insert( p );
248 : 0 : }
249 : :
250 : 0 : void SbxStdCollection::CollAdd( SbxArray* pPar_ )
251 : : {
252 [ # # ]: 0 : if( !bAddRemoveOk )
253 : 0 : SetError( SbxERR_BAD_ACTION );
254 : : else
255 : 0 : SbxCollection::CollAdd( pPar_ );
256 : 0 : }
257 : :
258 : 0 : void SbxStdCollection::CollRemove( SbxArray* pPar_ )
259 : : {
260 [ # # ]: 0 : if( !bAddRemoveOk )
261 : 0 : SetError( SbxERR_BAD_ACTION );
262 : : else
263 : 0 : SbxCollection::CollRemove( pPar_ );
264 : 0 : }
265 : :
266 : 0 : sal_Bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
267 : : {
268 : 0 : sal_Bool bRes = SbxCollection::LoadData( rStrm, nVer );
269 [ # # ]: 0 : if( bRes )
270 : : {
271 : : aElemClass = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(rStrm,
272 [ # # ]: 0 : RTL_TEXTENCODING_ASCII_US);
273 : 0 : rStrm >> bAddRemoveOk;
274 : : }
275 : 0 : return bRes;
276 : : }
277 : :
278 : 0 : sal_Bool SbxStdCollection::StoreData( SvStream& rStrm ) const
279 : : {
280 : 0 : sal_Bool bRes = SbxCollection::StoreData( rStrm );
281 [ # # ]: 0 : if( bRes )
282 : : {
283 : : write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(rStrm, aElemClass,
284 [ # # ]: 0 : RTL_TEXTENCODING_ASCII_US);
285 : 0 : rStrm << bAddRemoveOk;
286 : : }
287 : 0 : return bRes;
288 : : }
289 : :
290 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|