Colibre: Fix wrong color, the ones which doesn not follow Monoline
[LibreOffice.git] / basic / source / sbx / sbxcoll.cxx
blobea4eb4cdea145fc229568330ebdad1a9eed5c073
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/stream.hxx>
22 #include <basic/sbx.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxres.hxx"
27 static OUString pCount;
28 static OUString pAdd;
29 static OUString pItem;
30 static OUString pRemove;
31 static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
34 SbxCollection::SbxCollection()
35 : SbxObject( "" )
37 if( !nCountHash )
39 pCount = GetSbxRes( StringId::CountProp );
40 pAdd = GetSbxRes( StringId::AddMeth );
41 pItem = GetSbxRes( StringId::ItemMeth );
42 pRemove = GetSbxRes( StringId::RemoveMeth );
43 nCountHash = MakeHashCode( pCount );
44 nAddHash = MakeHashCode( pAdd );
45 nItemHash = MakeHashCode( pItem );
46 nRemoveHash = MakeHashCode( pRemove );
48 Initialize();
49 // For Access on itself
50 StartListening(GetBroadcaster(), DuplicateHandling::Prevent);
53 SbxCollection::SbxCollection( const SbxCollection& rColl )
54 : SvRefBase( rColl ), SbxObject( rColl )
57 SbxCollection& SbxCollection::operator=( const SbxCollection& r )
59 if( &r != this )
60 SbxObject::operator=( r );
61 return *this;
64 SbxCollection::~SbxCollection()
67 void SbxCollection::Clear()
69 SbxObject::Clear();
70 Initialize();
73 void SbxCollection::Initialize()
75 SetType( SbxOBJECT );
76 SetFlag( SbxFlagBits::Fixed );
77 ResetFlag( SbxFlagBits::Write );
78 SbxVariable* p;
79 p = Make( pCount , SbxClassType::Property, SbxINTEGER );
80 p->ResetFlag( SbxFlagBits::Write );
81 p->SetFlag( SbxFlagBits::DontStore );
82 p = Make( pAdd, SbxClassType::Method, SbxEMPTY );
83 p->SetFlag( SbxFlagBits::DontStore );
84 p = Make( pItem , SbxClassType::Method, SbxOBJECT );
85 p->SetFlag( SbxFlagBits::DontStore );
86 p = Make( pRemove, SbxClassType::Method, SbxEMPTY );
87 p->SetFlag( SbxFlagBits::DontStore );
90 SbxVariable* SbxCollection::Find( const OUString& rName, SbxClassType t )
92 if( GetParameters() )
94 SbxObject* pObj = static_cast<SbxObject*>(GetObject());
95 return pObj ? pObj->Find( rName, t ) : nullptr;
97 else
99 return SbxObject::Find( rName, t );
103 void SbxCollection::Notify( SfxBroadcaster& rCst, const SfxHint& rHint )
105 const SbxHint* p = dynamic_cast<const SbxHint*>(&rHint);
106 if( p )
108 const SfxHintId nId = p->GetId();
109 bool bRead = ( nId == SfxHintId::BasicDataWanted );
110 bool bWrite = ( nId == SfxHintId::BasicDataChanged );
111 SbxVariable* pVar = p->GetVar();
112 SbxArray* pArg = pVar->GetParameters();
113 if( bRead || bWrite )
115 OUString aVarName( pVar->GetName() );
116 if( pVar == this )
118 CollItem( pArg );
120 else if( pVar->GetHashCode() == nCountHash
121 && aVarName.equalsIgnoreAsciiCase( pCount ) )
123 pVar->PutLong(sal::static_int_cast<sal_Int32>(pObjs->Count()));
125 else if( pVar->GetHashCode() == nAddHash
126 && aVarName.equalsIgnoreAsciiCase( pAdd ) )
128 CollAdd( pArg );
130 else if( pVar->GetHashCode() == nItemHash
131 && aVarName.equalsIgnoreAsciiCase( pItem ) )
133 CollItem( pArg );
135 else if( pVar->GetHashCode() == nRemoveHash
136 && aVarName.equalsIgnoreAsciiCase( pRemove ) )
138 CollRemove( pArg );
140 else
142 SbxObject::Notify( rCst, rHint );
144 return;
147 SbxObject::Notify( rCst, rHint );
150 // Default: argument is object
152 void SbxCollection::CollAdd( SbxArray* pPar_ )
154 if (pPar_->Count() != 2)
156 SetError( ERRCODE_BASIC_WRONG_ARGS );
158 else
160 SbxBase* pObj = pPar_->Get(1)->GetObject();
161 if( !pObj || dynamic_cast<const SbxObject*>(pObj) == nullptr )
163 SetError( ERRCODE_BASIC_BAD_ARGUMENT );
165 else
167 Insert( static_cast<SbxObject*>(pObj) );
172 // Default: index from 1 or object name
174 void SbxCollection::CollItem( SbxArray* pPar_ )
176 if (pPar_->Count() != 2)
178 SetError( ERRCODE_BASIC_WRONG_ARGS );
180 else
182 SbxVariable* pRes = nullptr;
183 SbxVariable* p = pPar_->Get(1);
184 if( p->GetType() == SbxSTRING )
186 pRes = Find( p->GetOUString(), SbxClassType::Object );
188 else
190 short n = p->GetInteger();
191 if (n >= 1 && n <= static_cast<sal_Int32>(pObjs->Count()))
193 pRes = pObjs->Get(static_cast<sal_uInt32>(n) - 1);
196 if( !pRes )
198 SetError( ERRCODE_BASIC_BAD_INDEX );
200 pPar_->Get(0)->PutObject(pRes);
204 // Default: index from 1
206 void SbxCollection::CollRemove( SbxArray* pPar_ )
208 if (pPar_->Count() != 2)
209 SetError( ERRCODE_BASIC_WRONG_ARGS );
210 else
212 short n = pPar_->Get(1)->GetInteger();
213 if (n < 1 || n > static_cast<sal_Int32>(pObjs->Count()))
214 SetError( ERRCODE_BASIC_BAD_INDEX );
215 else
216 Remove(pObjs->Get(static_cast<sal_uInt32>(n) - 1));
220 bool SbxCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
222 bool bRes = SbxObject::LoadData( rStrm, nVer );
223 Initialize();
224 return bRes;
228 SbxStdCollection::SbxStdCollection()
229 : bAddRemoveOk( true )
232 SbxStdCollection::SbxStdCollection( const SbxStdCollection& r )
233 : SvRefBase( r ), SbxCollection( r ),
234 aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
237 SbxStdCollection& SbxStdCollection::operator=( const SbxStdCollection& r )
239 if( &r != this )
241 if( !r.aElemClass.equalsIgnoreAsciiCase( aElemClass ) )
243 SetError( ERRCODE_BASIC_CONVERSION );
245 else
247 SbxCollection::operator=( r );
250 return *this;
253 SbxStdCollection::~SbxStdCollection()
256 // Default: Error, if wrong object
258 void SbxStdCollection::Insert( SbxVariable* p )
260 SbxObject* pObj = dynamic_cast<SbxObject*>( p );
261 if( pObj && !pObj->IsClass( aElemClass ) )
262 SetError( ERRCODE_BASIC_BAD_ACTION );
263 else
264 SbxCollection::Insert( p );
267 void SbxStdCollection::CollAdd( SbxArray* pPar_ )
269 if( !bAddRemoveOk )
270 SetError( ERRCODE_BASIC_BAD_ACTION );
271 else
272 SbxCollection::CollAdd( pPar_ );
275 void SbxStdCollection::CollRemove( SbxArray* pPar_ )
277 if( !bAddRemoveOk )
278 SetError( ERRCODE_BASIC_BAD_ACTION );
279 else
280 SbxCollection::CollRemove( pPar_ );
283 bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
285 bool bRes = SbxCollection::LoadData( rStrm, nVer );
286 if( bRes )
288 aElemClass = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
289 RTL_TEXTENCODING_ASCII_US);
290 rStrm.ReadCharAsBool( bAddRemoveOk );
292 return bRes;
295 bool SbxStdCollection::StoreData( SvStream& rStrm ) const
297 bool bRes = SbxCollection::StoreData( rStrm );
298 if( bRes )
300 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aElemClass,
301 RTL_TEXTENCODING_ASCII_US);
302 rStrm.WriteBool( bAddRemoveOk );
304 return bRes;
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */