xref: /trunk/main/basic/source/sbx/sbxbase.cxx (revision e1f63238)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basic.hxx"
26 
27 
28 #include <tools/shl.hxx>
29 #include <tools/stream.hxx>
30 
31 #include <basic/sbx.hxx>
32 #include <basic/sbxfac.hxx>
33 #include <basic/sbxbase.hxx>
34 
35 // AppData-Struktur fuer SBX:
36 
37 SV_IMPL_PTRARR(SbxParams,SbxParamInfo*);
38 SV_IMPL_PTRARR(SbxFacs,SbxFactory*);
39 
TYPEINIT0(SbxBase)40 TYPEINIT0(SbxBase)
41 
42 // SBX-Daten anfordern oder ggf. anlegen
43 // wir legen den Bereich einfach an und verzichten auf die Freigabe!
44 
45 SbxAppData* GetSbxData_Impl()
46 {
47 	SbxAppData** ppData = (SbxAppData**) ::GetAppData( SHL_SBX );
48 	SbxAppData* p = *ppData;
49 	if( !p )
50 		p = *ppData  = new SbxAppData;
51 	return p;
52 }
53 
~SbxAppData()54 SbxAppData::~SbxAppData()
55 {
56 	if( pBasicFormater )
57 		delete pBasicFormater;
58 }
59 
60 
61 //////////////////////////////// SbxBase /////////////////////////////////
62 
63 DBG_NAME(SbxBase);
64 
SbxBase()65 SbxBase::SbxBase()
66 {
67 	DBG_CTOR( SbxBase, 0 );
68 	nFlags  = SBX_READWRITE;
69 }
70 
SbxBase(const SbxBase & r)71 SbxBase::SbxBase( const SbxBase& r )
72     : SvRefBase( r )
73 {
74 	DBG_CTOR( SbxBase, 0 );
75 	nFlags  = r.nFlags;
76 }
77 
~SbxBase()78 SbxBase::~SbxBase()
79 {
80 	DBG_DTOR(SbxBase,0);
81 }
82 
operator =(const SbxBase & r)83 SbxBase& SbxBase::operator=( const SbxBase& r )
84 {
85 	DBG_CHKTHIS( SbxBase, 0 );
86 	nFlags = r.nFlags;
87 	return *this;
88 }
89 
GetType() const90 SbxDataType SbxBase::GetType() const
91 {
92 	DBG_CHKTHIS( SbxBase, 0 );
93 	return SbxEMPTY;
94 }
95 
GetClass() const96 SbxClassType SbxBase::GetClass() const
97 {
98 	DBG_CHKTHIS( SbxBase, 0 );
99 	return SbxCLASS_DONTCARE;
100 }
101 
Clear()102 void SbxBase::Clear()
103 {
104 	DBG_CHKTHIS( SbxBase, 0 );
105 }
106 
IsFixed() const107 sal_Bool SbxBase::IsFixed() const
108 {
109 	DBG_CHKTHIS( SbxBase, 0 );
110 	return IsSet( SBX_FIXED );
111 }
112 
SetModified(sal_Bool b)113 void SbxBase::SetModified( sal_Bool b )
114 {
115 	DBG_CHKTHIS( SbxBase, 0 );
116 	if( IsSet( SBX_NO_MODIFY ) )
117 		return;
118 	if( b )
119 		SetFlag( SBX_MODIFIED );
120 	else
121 		ResetFlag( SBX_MODIFIED );
122 }
123 
GetError()124 SbxError SbxBase::GetError()
125 {
126 	return GetSbxData_Impl()->eSbxError;
127 }
128 
SetError(SbxError e)129 void SbxBase::SetError( SbxError e )
130 {
131 	SbxAppData* p = GetSbxData_Impl();
132 	if( e && p->eSbxError == SbxERR_OK )
133 		p->eSbxError = e;
134 }
135 
IsError()136 sal_Bool SbxBase::IsError()
137 {
138 	return sal_Bool( GetSbxData_Impl()->eSbxError != SbxERR_OK );
139 }
140 
ResetError()141 void SbxBase::ResetError()
142 {
143 	GetSbxData_Impl()->eSbxError = SbxERR_OK;
144 }
145 
AddFactory(SbxFactory * pFac)146 void SbxBase::AddFactory( SbxFactory* pFac )
147 {
148 	SbxAppData* p = GetSbxData_Impl();
149 	const SbxFactory* pTemp = pFac;
150 
151 	// AB, 6.3.96: HandleLast-Flag beruecksichtigen
152 	sal_uInt16 nPos = p->aFacs.Count();		// Einfuege-Position
153 	if( !pFac->IsHandleLast() )			// Nur, wenn nicht selbst HandleLast
154 	{
155 		// Neue Factory vor Factories mit HandleLast einordnen
156 		while( nPos > 0 &&
157 				(static_cast<SbxFactory*>(p->aFacs.GetObject( nPos-1 )))->IsHandleLast() )
158 			nPos--;
159 	}
160 	p->aFacs.Insert( pTemp, nPos );
161 }
162 
RemoveFactory(SbxFactory * pFac)163 void SbxBase::RemoveFactory( SbxFactory* pFac )
164 {
165 	SbxAppData* p = GetSbxData_Impl();
166 	for( sal_uInt16 i = 0; i < p->aFacs.Count(); i++ )
167 	{
168 		if( p->aFacs.GetObject( i ) == pFac )
169 		{
170 			p->aFacs.Remove( i, 1 ); break;
171 		}
172 	}
173 }
174 
175 
Create(sal_uInt16 nSbxId,sal_uInt32 nCreator)176 SbxBase* SbxBase::Create( sal_uInt16 nSbxId, sal_uInt32 nCreator )
177 {
178     // #91626: Hack to skip old Basic dialogs
179     // Problem: There does not exist a factory any more,
180     // so we have to create a dummy SbxVariable instead
181 	if( nSbxId == 0x65 )    // Dialog Id
182         return new SbxVariable;
183 
184 	XubString aEmptyStr;
185 	if( nCreator == SBXCR_SBX )
186 	  switch( nSbxId )
187 	{
188 		case SBXID_VALUE:		return new SbxValue;
189 		case SBXID_VARIABLE:	return new SbxVariable;
190 		case SBXID_ARRAY:  		return new SbxArray;
191 		case SBXID_DIMARRAY:	return new SbxDimArray;
192 		case SBXID_OBJECT:		return new SbxObject( aEmptyStr );
193 		case SBXID_COLLECTION:	return new SbxCollection( aEmptyStr );
194 		case SBXID_FIXCOLLECTION:
195 								return new SbxStdCollection( aEmptyStr, aEmptyStr );
196 		case SBXID_METHOD:		return new SbxMethod( aEmptyStr, SbxEMPTY );
197 		case SBXID_PROPERTY:	return new SbxProperty( aEmptyStr, SbxEMPTY );
198 	}
199 	// Unbekanter Typ: �ber die Factories gehen!
200 	SbxAppData* p = GetSbxData_Impl();
201 	SbxBase* pNew = NULL;
202 	for( sal_uInt16 i = 0; i < p->aFacs.Count(); i++ )
203 	{
204 		SbxFactory* pFac = p->aFacs.GetObject( i );
205 		pNew = pFac->Create( nSbxId, nCreator );
206 		if( pNew )
207 			break;
208 	}
209 #ifdef DBG_UTIL
210 	if( !pNew )
211 	{
212 		ByteString aMsg( "SBX: Keine Factory fuer SBX-ID " );
213 		aMsg += ByteString::CreateFromInt32(nSbxId);
214 		DbgError( aMsg.GetBuffer() );
215 	}
216 #endif
217 	return pNew;
218 }
219 
CreateObject(const XubString & rClass)220 SbxObject* SbxBase::CreateObject( const XubString& rClass )
221 {
222 	SbxAppData* p = GetSbxData_Impl();
223 	SbxObject* pNew = NULL;
224 	for( sal_uInt16 i = 0; i < p->aFacs.Count(); i++ )
225 	{
226 		pNew = p->aFacs.GetObject( i )->CreateObject( rClass );
227 		if( pNew )
228 			break;
229 	}
230 #ifdef DBG_UTIL
231 	if( !pNew )
232 	{
233 		ByteString aMsg( "SBX: Keine Factory fuer Objektklasse " );
234 		ByteString aClassStr( (const UniString&)rClass, RTL_TEXTENCODING_ASCII_US );
235 		aMsg += aClassStr;
236 		DbgError( (const char*)aMsg.GetBuffer() );
237 	}
238 #endif
239 	return pNew;
240 }
241 
242 static sal_Bool bStaticEnableBroadcasting = sal_True;
243 
244 // Sbx-Loesung als Ersatz fuer SfxBroadcaster::Enable()
StaticEnableBroadcasting(sal_Bool bEnable)245 void SbxBase::StaticEnableBroadcasting( sal_Bool bEnable )
246 {
247 	bStaticEnableBroadcasting = bEnable;
248 }
249 
StaticIsEnabledBroadcasting(void)250 sal_Bool SbxBase::StaticIsEnabledBroadcasting( void )
251 {
252 	return bStaticEnableBroadcasting;
253 }
254 
255 
Load(SvStream & rStrm)256 SbxBase* SbxBase::Load( SvStream& rStrm )
257 {
258 	sal_uInt16 nSbxId, nFlags, nVer;
259 	sal_uInt32 nCreator, nSize;
260 	rStrm >> nCreator >> nSbxId >> nFlags >> nVer;
261 
262 	// Eine Dummheit meinerseits korrigieren:
263 	if( nFlags & SBX_RESERVED )
264 		nFlags = ( nFlags & ~SBX_RESERVED ) | SBX_GBLSEARCH;
265 
266 	sal_uIntPtr nOldPos = rStrm.Tell();
267 	rStrm >> nSize;
268 	SbxBase* p = Create( nSbxId, nCreator );
269 	if( p )
270 	{
271 		p->nFlags = nFlags;
272 		if( p->LoadData( rStrm, nVer ) )
273 		{
274 			sal_uIntPtr nNewPos = rStrm.Tell();
275 			nOldPos += nSize;
276 			DBG_ASSERT( nOldPos >= nNewPos, "SBX: Zu viele Daten eingelesen" );
277 			if( nOldPos != nNewPos )
278 				rStrm.Seek( nOldPos );
279 			if( !p->LoadCompleted() )
280 			{
281 				// Loeschen des Objekts
282 				SbxBaseRef aRef( p );
283 				p = NULL;
284 			}
285 		}
286 		else
287 		{
288 			rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR );
289 			// Loeschen des Objekts
290 			SbxBaseRef aRef( p );
291 			p = NULL;
292 		}
293 	}
294 	else
295 		rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR );
296 	return p;
297 }
298 
299 // Sbx-Objekt im Stream ueberspringen
Skip(SvStream & rStrm)300 void SbxBase::Skip( SvStream& rStrm )
301 {
302 	sal_uInt16 nSbxId, nFlags, nVer;
303 	sal_uInt32 nCreator, nSize;
304 	rStrm >> nCreator >> nSbxId >> nFlags >> nVer;
305 
306 	sal_uIntPtr nStartPos = rStrm.Tell();
307 	rStrm >> nSize;
308 
309 	rStrm.Seek( nStartPos + nSize );
310 }
311 
Store(SvStream & rStrm)312 sal_Bool SbxBase::Store( SvStream& rStrm )
313 {
314 	DBG_CHKTHIS( SbxBase, 0 );
315 	if( !( nFlags & SBX_DONTSTORE ) )
316 	{
317 		rStrm << (sal_uInt32) GetCreator()
318 			  << (sal_uInt16) GetSbxId()
319 			  << (sal_uInt16) GetFlags()
320 			  << (sal_uInt16) GetVersion();
321 		sal_uIntPtr nOldPos = rStrm.Tell();
322 		rStrm << (sal_uInt32) 0L;
323 		sal_Bool bRes = StoreData( rStrm );
324 		sal_uIntPtr nNewPos = rStrm.Tell();
325 		rStrm.Seek( nOldPos );
326 		rStrm << (sal_uInt32) ( nNewPos - nOldPos );
327 		rStrm.Seek( nNewPos );
328 		if( rStrm.GetError() != SVSTREAM_OK )
329 			bRes = sal_False;
330 		if( bRes )
331 			bRes = StoreCompleted();
332 		return bRes;
333 	}
334 	else
335 		return sal_True;
336 }
337 
LoadData(SvStream &,sal_uInt16)338 sal_Bool SbxBase::LoadData( SvStream&, sal_uInt16 )
339 {
340 	DBG_CHKTHIS( SbxBase, 0 );
341 	return sal_False;
342 }
343 
StoreData(SvStream &) const344 sal_Bool SbxBase::StoreData( SvStream& ) const
345 {
346 	DBG_CHKTHIS( SbxBase, 0 );
347 	return sal_False;
348 }
349 
LoadPrivateData(SvStream &,sal_uInt16)350 sal_Bool SbxBase::LoadPrivateData( SvStream&, sal_uInt16 )
351 {
352 	DBG_CHKTHIS( SbxBase, 0 );
353 	return sal_True;
354 }
355 
StorePrivateData(SvStream &) const356 sal_Bool SbxBase::StorePrivateData( SvStream& ) const
357 {
358 	DBG_CHKTHIS( SbxBase, 0 );
359 	return sal_True;
360 }
361 
LoadCompleted()362 sal_Bool SbxBase::LoadCompleted()
363 {
364 	DBG_CHKTHIS( SbxBase, 0 );
365 	return sal_True;
366 }
367 
StoreCompleted()368 sal_Bool SbxBase::StoreCompleted()
369 {
370 	DBG_CHKTHIS( SbxBase, 0 );
371 	return sal_True;
372 }
373 
374 //////////////////////////////// SbxFactory ////////////////////////////////
375 
Create(sal_uInt16,sal_uInt32)376 SbxBase* SbxFactory::Create( sal_uInt16, sal_uInt32 )
377 {
378 	return NULL;
379 }
380 
CreateObject(const XubString &)381 SbxObject* SbxFactory::CreateObject( const XubString& )
382 {
383 	return NULL;
384 }
385 
386 ///////////////////////////////// SbxInfo //////////////////////////////////
387 
~SbxInfo()388 SbxInfo::~SbxInfo()
389 {}
390 
AddParam(const XubString & rName,SbxDataType eType,sal_uInt16 nFlags)391 void SbxInfo::AddParam
392 		( const XubString& rName, SbxDataType eType, sal_uInt16 nFlags )
393 {
394 	const SbxParamInfo* p = new SbxParamInfo( rName, eType, nFlags );
395 	aParams.Insert( p, aParams.Count() );
396 }
397 
AddParam(const SbxParamInfo & r)398 void SbxInfo::AddParam( const SbxParamInfo& r )
399 {
400 	const SbxParamInfo* p = new SbxParamInfo
401 		( r.aName, r.eType, r.nFlags, r.aTypeRef );
402 	aParams.Insert( p, aParams.Count() );
403 }
404 
GetParam(sal_uInt16 n) const405 const SbxParamInfo* SbxInfo::GetParam( sal_uInt16 n ) const
406 {
407 	if( n < 1 || n > aParams.Count() )
408 		return NULL;
409 	else
410 		return aParams.GetObject( n-1 );
411 }
412 
LoadData(SvStream & rStrm,sal_uInt16 nVer)413 sal_Bool SbxInfo::LoadData( SvStream& rStrm, sal_uInt16 nVer )
414 {
415 	aParams.Remove( 0, aParams.Count() );
416 	sal_uInt16 nParam;
417 	rStrm.ReadByteString( aComment, RTL_TEXTENCODING_ASCII_US );
418 	rStrm.ReadByteString( aHelpFile, RTL_TEXTENCODING_ASCII_US );
419 	rStrm >> nHelpId >> nParam;
420 	while( nParam-- )
421 	{
422 		XubString aName;
423 		sal_uInt16 nType, nFlags;
424 		sal_uInt32 nUserData = 0;
425 		rStrm.ReadByteString( aName, RTL_TEXTENCODING_ASCII_US );
426 		rStrm >> nType >> nFlags;
427 		if( nVer > 1 )
428 			rStrm >> nUserData;
429 		AddParam( aName, (SbxDataType) nType, nFlags );
430 		SbxParamInfo* p = aParams.GetObject( aParams.Count() - 1 );
431 		p->nUserData = nUserData;
432 	}
433 	return sal_True;
434 }
435 
StoreData(SvStream & rStrm) const436 sal_Bool SbxInfo::StoreData( SvStream& rStrm ) const
437 {
438 	rStrm.WriteByteString( aComment, RTL_TEXTENCODING_ASCII_US );
439 	rStrm.WriteByteString( aHelpFile, RTL_TEXTENCODING_ASCII_US );
440 	rStrm << nHelpId << aParams.Count();
441 	for( sal_uInt16 i = 0; i < aParams.Count(); i++ )
442 	{
443 		SbxParamInfo* p = aParams.GetObject( i );
444 		rStrm.WriteByteString( p->aName, RTL_TEXTENCODING_ASCII_US );
445 		rStrm << (sal_uInt16) p->eType
446 			  << (sal_uInt16) p->nFlags
447 			  << (sal_uInt32) p->nUserData;
448 	}
449 	return sal_True;
450 }
451 
452