xref: /trunk/main/sc/source/core/data/documen2.cxx (revision b3f79822)
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_sc.hxx"
26 
27 // INCLUDE ---------------------------------------------------------------
28 
29 #define _ZFORLIST_DECLARE_TABLE
30 #include "scitems.hxx"
31 #include <editeng/eeitem.hxx>
32 
33 #include <editeng/editeng.hxx>
34 #include <editeng/forbiddencharacterstable.hxx>
35 #include <sfx2/linkmgr.hxx>
36 #include <svx/svdpool.hxx>
37 #include <svx/svdobj.hxx>
38 #include <sfx2/bindings.hxx>
39 #include <sfx2/objsh.hxx>
40 #include <sfx2/printer.hxx>
41 #include <svl/zforlist.hxx>
42 #include <svl/zformat.hxx>
43 #include <vcl/virdev.hxx>
44 #include <comphelper/processfactory.hxx>
45 #include <svl/PasswordHelper.hxx>
46 #include <tools/tenccvt.hxx>
47 #include <tools/list.hxx>
48 #include <rtl/crc.h>
49 #include <basic/basmgr.hxx>
50 
51 #include "document.hxx"
52 #include "table.hxx"
53 #include "attrib.hxx"
54 #include "patattr.hxx"
55 #include "rangenam.hxx"
56 #include "dbcolect.hxx"
57 #include "pivot.hxx"
58 #include "docpool.hxx"
59 #include "stlpool.hxx"
60 #include "stlsheet.hxx"
61 #include "globstr.hrc"
62 #include "chartarr.hxx"
63 #include "chartlock.hxx"
64 #include "rechead.hxx"
65 #include "global.hxx"
66 #include "brdcst.hxx"
67 #include "bcaslot.hxx"
68 #include "adiasync.hxx"
69 #include "addinlis.hxx"
70 #include "chartlis.hxx"
71 #include "markdata.hxx"
72 #include "conditio.hxx"
73 #include "validat.hxx"
74 #include "progress.hxx"
75 #include "detdata.hxx"
76 #include "sc.hrc"				// FID_DATACHANGED
77 #include "ddelink.hxx"
78 #include "chgtrack.hxx"
79 #include "chgviset.hxx"
80 #include "editutil.hxx"
81 #include "hints.hxx"
82 #include "dpobject.hxx"
83 #include "scrdata.hxx"
84 #include "poolhelp.hxx"
85 #include "unoreflist.hxx"
86 #include "listenercalls.hxx"
87 #include "recursionhelper.hxx"
88 #include "lookupcache.hxx"
89 #include "externalrefmgr.hxx"
90 #include "tabprotection.hxx"
91 #include "formulaparserpool.hxx"
92 #include "clipparam.hxx"
93 
94 using namespace com::sun::star;
95 
96 // pImpl because including lookupcache.hxx in document.hxx isn't wanted, and
97 // dtor plus helpers are convenient.
98 struct ScLookupCacheMapImpl
99 {
100     ScLookupCacheMap aCacheMap;
101     ~ScLookupCacheMapImpl()
102     {
103         freeCaches();
104     }
105     void clear()
106     {
107         freeCaches();
108         // Zap map.
109         ScLookupCacheMap aTmp;
110         aCacheMap.swap( aTmp);
111     }
112 private:
113     void freeCaches()
114     {
115         for (ScLookupCacheMap::iterator it( aCacheMap.begin()); it != aCacheMap.end(); ++it)
116             delete (*it).second;
117     }
118 };
119 
120 // STATIC DATA -----------------------------------------------------------
121 
122 ScDocument::ScDocument( ScDocumentMode	eMode,
123 						SfxObjectShell* pDocShell ) :
124 		xServiceManager( ::comphelper::getProcessServiceFactory() ),
125 		mpUndoManager( NULL ),
126 		pEditEngine( NULL ),
127 		pNoteEngine( NULL ),
128 		pNoteItemPool( NULL ),
129 		pShell( pDocShell ),
130 		pPrinter( NULL ),
131 		pVirtualDevice_100th_mm( NULL ),
132 		pDrawLayer( NULL ),
133 		pColorTable( NULL ),
134 		pCondFormList( NULL ),
135 		pValidationList( NULL ),
136 		pFormatExchangeList( NULL ),
137 		pDPCollection( NULL ),
138 		pLinkManager( NULL ),
139 		pFormulaTree( NULL ),
140 		pEOFormulaTree( NULL ),
141 		pFormulaTrack( NULL ),
142 		pEOFormulaTrack( NULL ),
143 		pOtherObjects( NULL ),
144 		pClipData( NULL ),
145 		pDetOpList(NULL),
146 		pChangeTrack( NULL ),
147 		pUnoBroadcaster( NULL ),
148 		pUnoListenerCalls( NULL ),
149         pUnoRefUndoList( NULL ),
150 		pChangeViewSettings( NULL ),
151 		pScriptTypeData( NULL ),
152         pCacheFieldEditEngine( NULL ),
153         pDocProtection( NULL ),
154         mpClipParam( NULL),
155         pExternalRefMgr( NULL ),
156 		pViewOptions( NULL ),
157 		pDocOptions( NULL ),
158 		pExtDocOptions( NULL ),
159 		pConsolidateDlgData( NULL ),
160         pRecursionHelper( NULL ),
161         pAutoNameCache( NULL ),
162         pLookupCacheMapImpl( NULL ),
163         nUnoObjectId( 0 ),
164         nRangeOverflowType( 0 ),
165 		aCurTextWidthCalcPos(MAXCOL,0,0),
166 		nFormulaCodeInTree(0),
167         nXMLImportedFormulaCount( 0 ),
168 		nInterpretLevel(0),
169 		nMacroInterpretLevel(0),
170 		nInterpreterTableOpLevel(0),
171 		nMaxTableNumber( 0 ),
172 	    nSrcVer( SC_CURRENT_VERSION ),
173 	    nSrcMaxRow( MAXROW ),
174 		nFormulaTrackCount(0),
175 		nHardRecalcState(0),
176 		nVisibleTab( 0 ),
177 		eLinkMode(LM_UNKNOWN),
178 		bAutoCalc( eMode == SCDOCMODE_DOCUMENT ),
179 		bAutoCalcShellDisabled( sal_False ),
180 		bForcedFormulaPending( sal_False ),
181 		bCalculatingFormulaTree( sal_False ),
182 		bIsClip( eMode == SCDOCMODE_CLIP ),
183 		bIsUndo( eMode == SCDOCMODE_UNDO ),
184 		bIsVisible( sal_False ),
185 		bIsEmbedded( sal_False ),
186 //		bNoSetDirty( sal_True ),
187 		bNoSetDirty( sal_False ),
188 		bInsertingFromOtherDoc( sal_False ),
189         bLoadingMedium( false ),
190 		bImportingXML( false ),
191         bXMLFromWrapper( sal_False ),
192 		bCalcingAfterLoad( sal_False ),
193 		bNoListening( sal_False ),
194 		bIdleDisabled( sal_False ),
195 		bInLinkUpdate( sal_False ),
196 		bChartListenerCollectionNeedsUpdate( sal_False ),
197 		bHasForcedFormulas( sal_False ),
198 		bInDtorClear( sal_False ),
199 		bExpandRefs( sal_False ),
200 		bDetectiveDirty( sal_False ),
201 		nMacroCallMode( SC_MACROCALL_ALLOWED ),
202 		bHasMacroFunc( sal_False ),
203 		nVisSpellState( 0 ),
204         nAsianCompression(SC_ASIANCOMPRESSION_INVALID),
205         nAsianKerning(SC_ASIANKERNING_INVALID),
206         bSetDrawDefaults( sal_False ),
207         bPastingDrawFromOtherDoc( sal_False ),
208         nInDdeLinkUpdate( 0 ),
209         bInUnoBroadcast( sal_False ),
210         bInUnoListenerCall( sal_False ),
211         eGrammar( formula::FormulaGrammar::GRAM_NATIVE ),
212         bStyleSheetUsageInvalid( sal_True ),
213         mbUndoEnabled( true ),
214         mbAdjustHeightEnabled( true ),
215         mbExecuteLinkEnabled( true ),
216         mbChangeReadOnlyEnabled( false ),
217         mbStreamValidLocked( false ),
218         mnNamedRangesLockCount( 0 )
219 {
220     SetStorageGrammar( formula::FormulaGrammar::GRAM_STORAGE_DEFAULT);
221 
222 	eSrcSet = gsl_getSystemTextEncoding();
223 
224 	if ( eMode == SCDOCMODE_DOCUMENT )
225 	{
226 		if ( pDocShell )
227 			pLinkManager = new sfx2::LinkManager( pDocShell );
228 
229 		xPoolHelper = new ScPoolHelper( this );
230 
231 		pTab[0]  = NULL;
232 		pBASM = new ScBroadcastAreaSlotMachine( this );
233 		pChartListenerCollection = new ScChartListenerCollection( this );
234 		pRefreshTimerControl = new ScRefreshTimerControl;
235 	}
236 	else
237 	{
238 		pTab[0]		= NULL;
239 		pBASM		= NULL;
240 		pChartListenerCollection = NULL;
241 		pRefreshTimerControl = NULL;
242 	}
243 
244 	for (SCTAB i=1; i<=MAXTAB; i++)
245 		pTab[i] = NULL;
246 
247 	pRangeName = new ScRangeName( 4, 4, sal_False, this );
248 	pDBCollection = new ScDBCollection( 4, 4, sal_False, this );
249 	pSelectionAttr = NULL;
250 	pChartCollection = new ScChartCollection;
251     apTemporaryChartLock = std::auto_ptr< ScTemporaryChartLock >( new ScTemporaryChartLock(this) );
252 	xColNameRanges = new ScRangePairList;
253 	xRowNameRanges = new ScRangePairList;
254 	ImplCreateOptions();
255 	// languages for a visible document are set by docshell later (from options)
256 	SetLanguage( ScGlobal::eLnge, ScGlobal::eLnge, ScGlobal::eLnge );
257 
258 	aTrackTimer.SetTimeoutHdl( LINK( this, ScDocument, TrackTimeHdl ) );
259 	aTrackTimer.SetTimeout( 100 );
260 }
261 
262 sfx2::LinkManager*	ScDocument::GetLinkManager()  const
263 {
264     if ( bAutoCalc && !pLinkManager && pShell)
265 	{
266 		pLinkManager = new sfx2::LinkManager( pShell );
267     }
268     return pLinkManager;
269 }
270 
271 
272 void ScDocument::SetStorageGrammar( formula::FormulaGrammar::Grammar eGram )
273 {
274     DBG_ASSERT(
275         eGram == formula::FormulaGrammar::GRAM_ODFF ||
276             eGram == formula::FormulaGrammar::GRAM_PODF,
277             "ScDocument::SetStorageGrammar: wrong storage grammar");
278 
279     eStorageGrammar = eGram;
280 
281     // FIXME: the XML import shouldn't strip brackets, the compiler should
282     // digest them instead, which could also speedup reference recognition
283     // during import.
284 
285     eXmlImportGrammar = formula::FormulaGrammar::mergeToGrammar( eGram,
286             formula::FormulaGrammar::CONV_OOO);
287 }
288 
289 
290 void ScDocument::SetDocVisible( sal_Bool bSet )
291 {
292 	//	called from view ctor - only for a visible document,
293 	//	each new sheet's RTL flag is initialized from the locale
294 	bIsVisible = bSet;
295 }
296 
297 
298 sal_uInt32 ScDocument::GetDocumentID() const
299 {
300     const ScDocument* pThis = this;
301     sal_uInt32 nCrc = rtl_crc32( 0, &pThis, sizeof(ScDocument*) );
302     // the this pointer only might not be sufficient
303     nCrc = rtl_crc32( nCrc, &pShell, sizeof(SfxObjectShell*) );
304     return nCrc;
305 }
306 
307 
308 void ScDocument::StartChangeTracking()
309 {
310 	if (!pChangeTrack)
311 		pChangeTrack = new ScChangeTrack( this );
312 }
313 
314 void ScDocument::EndChangeTracking()
315 {
316 	delete pChangeTrack;
317 	pChangeTrack = NULL;
318 }
319 
320 void ScDocument::SetChangeTrack( ScChangeTrack* pTrack )
321 {
322 	DBG_ASSERT( pTrack->GetDocument() == this, "SetChangeTrack: different documents" );
323 	if ( !pTrack || pTrack == pChangeTrack || pTrack->GetDocument() != this )
324 		return ;
325 	EndChangeTracking();
326 	pChangeTrack = pTrack;
327 }
328 
329 
330 IMPL_LINK( ScDocument, TrackTimeHdl, Timer*, EMPTYARG )
331 {
332 	if ( ScDdeLink::IsInUpdate() )		// nicht verschachteln
333 	{
334 		aTrackTimer.Start();			// spaeter nochmal versuchen
335 	}
336 	else if (pShell)					// ausfuehren
337 	{
338 		TrackFormulas();
339 		pShell->Broadcast( SfxSimpleHint( FID_DATACHANGED ) );
340 		ResetChanged( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB) );
341 
342 			//	modified...
343 
344 		if (!pShell->IsModified())
345 		{
346 			pShell->SetModified( sal_True );
347 			SfxBindings* pBindings = GetViewBindings();
348 			if (pBindings)
349 			{
350 				pBindings->Invalidate( SID_SAVEDOC );
351 				pBindings->Invalidate( SID_DOC_MODIFIED );
352 			}
353 		}
354 	}
355 
356 	return 0;
357 }
358 
359 void ScDocument::StartTrackTimer()
360 {
361 	if (!aTrackTimer.IsActive())		// nicht ewig aufschieben
362 		aTrackTimer.Start();
363 }
364 
365 ScDocument::~ScDocument()
366 {
367 	DBG_ASSERT( !bInLinkUpdate, "bInLinkUpdate in dtor" );
368 
369 	bInDtorClear = sal_True;
370 
371 	// first of all disable all refresh timers by deleting the control
372 	if ( pRefreshTimerControl )
373 	{	// To be sure there isn't anything running do it with a protector,
374 		// this ensures also that nothing needs the control anymore.
375 		ScRefreshTimerProtector aProt( GetRefreshTimerControlAddress() );
376 		delete pRefreshTimerControl, pRefreshTimerControl = NULL;
377 	}
378 
379 	// Links aufrauemen
380 
381 	if ( GetLinkManager() )
382 	{
383 		// BaseLinks freigeben
384 		for ( sal_uInt16 n = pLinkManager->GetServers().Count(); n; )
385 			pLinkManager->GetServers()[ --n ]->Closed();
386 
387 		if ( pLinkManager->GetLinks().Count() )
388 			pLinkManager->Remove( 0, pLinkManager->GetLinks().Count() );
389 	}
390 
391     mxFormulaParserPool.reset();
392     // Destroy the external ref mgr instance here because it has a timer
393     // which needs to be stopped before the app closes.
394     pExternalRefMgr.reset();
395 
396 	ScAddInAsync::RemoveDocument( this );
397 	ScAddInListener::RemoveDocument( this );
398     DELETEZ( pChartListenerCollection);   // vor pBASM wg. evtl. Listener!
399     DELETEZ( pLookupCacheMapImpl);  // before pBASM because of listeners
400 	// BroadcastAreas vor allen Zellen zerstoeren um unnoetige
401 	// Einzel-EndListenings der Formelzellen zu vermeiden
402 	delete pBASM;		// BroadcastAreaSlotMachine
403 	pBASM = NULL;
404 
405 	if (pUnoBroadcaster)
406 	{
407 		delete pUnoBroadcaster;		// broadcasted nochmal SFX_HINT_DYING
408 		pUnoBroadcaster = NULL;
409 	}
410 
411     delete pUnoRefUndoList;
412 	delete pUnoListenerCalls;
413 
414     Clear( sal_True );              // sal_True = from destructor (needed for SdrModel::ClearModel)
415 
416 	if (pCondFormList)
417 	{
418 		pCondFormList->DeleteAndDestroy( 0, pCondFormList->Count() );
419 		DELETEZ(pCondFormList);
420 	}
421 	if (pValidationList)
422 	{
423 		pValidationList->DeleteAndDestroy( 0, pValidationList->Count() );
424 		DELETEZ(pValidationList);
425 	}
426 	delete pRangeName;
427 	delete pDBCollection;
428 	delete pSelectionAttr;
429     apTemporaryChartLock.reset();
430 	delete pChartCollection;
431 	DeleteDrawLayer();
432 	delete pFormatExchangeList;
433 	delete pPrinter;
434 	ImplDeleteOptions();
435 	delete pConsolidateDlgData;
436 	delete pLinkManager;
437 	delete pClipData;
438 	delete pDetOpList;					// loescht auch die Eintraege
439 	delete pChangeTrack;
440 	delete pEditEngine;
441 	delete pNoteEngine;
442 	SfxItemPool::Free(pNoteItemPool);
443 	delete pChangeViewSettings;			// und weg damit
444 	delete pVirtualDevice_100th_mm;
445 
446     if (pDPCollection)
447     {
448         pDPCollection->FreeAll();
449         RemoveUnusedDPObjectCaches();
450         delete pDPCollection;
451     }
452 
453 	// delete the EditEngine before destroying the xPoolHelper
454 	delete pCacheFieldEditEngine;
455 
456 	if ( xPoolHelper.isValid() && !bIsClip )
457 		xPoolHelper->SourceDocumentGone();
458 	xPoolHelper.unbind();
459 
460 	DeleteColorTable();
461 	delete pScriptTypeData;
462 	delete pOtherObjects;
463     delete pRecursionHelper;
464 
465     DBG_ASSERT( !pAutoNameCache, "AutoNameCache still set in dtor" );
466 }
467 
468 void ScDocument::InitClipPtrs( ScDocument* pSourceDoc )
469 {
470 	DBG_ASSERT(bIsClip, "InitClipPtrs und nicht bIsClip");
471 
472 	if (pCondFormList)
473 	{
474 		pCondFormList->DeleteAndDestroy( 0, pCondFormList->Count() );
475 		DELETEZ(pCondFormList);
476 	}
477 	if (pValidationList)
478 	{
479 		pValidationList->DeleteAndDestroy( 0, pValidationList->Count() );
480 		DELETEZ(pValidationList);
481 	}
482 
483 	Clear();
484 
485 	xPoolHelper = pSourceDoc->xPoolHelper;
486 
487 	//	bedingte Formate / Gueltigkeiten
488 	//!	Vorlagen kopieren?
489 	const ScConditionalFormatList* pSourceCond = pSourceDoc->pCondFormList;
490 	if ( pSourceCond )
491 		pCondFormList = new ScConditionalFormatList(this, *pSourceCond);
492 	const ScValidationDataList* pSourceValid = pSourceDoc->pValidationList;
493 	if ( pSourceValid )
494 		pValidationList = new ScValidationDataList(this, *pSourceValid);
495 
496 						// Links in Stream speichern
497 	delete pClipData;
498 	if (pSourceDoc->HasDdeLinks())
499 	{
500 		pClipData = new SvMemoryStream;
501 		pSourceDoc->SaveDdeLinks(*pClipData);
502 	}
503 	else
504 		pClipData = NULL;
505 
506     // Options pointers exist (ImplCreateOptions) for any document.
507     // Must be copied for correct results in OLE objects (#i42666#).
508     SetDocOptions( pSourceDoc->GetDocOptions() );
509     SetViewOptions( pSourceDoc->GetViewOptions() );
510 }
511 
512 SvNumberFormatter* ScDocument::GetFormatTable() const
513 {
514 	return xPoolHelper->GetFormTable();
515 }
516 
517 SfxItemPool* ScDocument::GetEditPool() const
518 {
519 	return xPoolHelper->GetEditPool();
520 }
521 
522 SfxItemPool* ScDocument::GetEnginePool() const
523 {
524 	return xPoolHelper->GetEnginePool();
525 }
526 
527 ScFieldEditEngine& ScDocument::GetEditEngine()
528 {
529 	if ( !pEditEngine )
530 	{
531 		pEditEngine = new ScFieldEditEngine( GetEnginePool(), GetEditPool() );
532 		pEditEngine->SetUpdateMode( sal_False );
533 		pEditEngine->EnableUndo( sal_False );
534 		pEditEngine->SetRefMapMode( MAP_100TH_MM );
535         ApplyAsianEditSettings( *pEditEngine );
536 	}
537 	return *pEditEngine;
538 }
539 
540 ScNoteEditEngine& ScDocument::GetNoteEngine()
541 {
542 	if ( !pNoteEngine )
543 	{
544 		pNoteEngine = new ScNoteEditEngine( GetEnginePool(), GetEditPool() );
545 		pNoteEngine->SetUpdateMode( sal_False );
546 		pNoteEngine->EnableUndo( sal_False );
547 		pNoteEngine->SetRefMapMode( MAP_100TH_MM );
548         ApplyAsianEditSettings( *pNoteEngine );
549         const SfxItemSet& rItemSet = GetDefPattern()->GetItemSet();
550         SfxItemSet* pEEItemSet = new SfxItemSet( pNoteEngine->GetEmptyItemSet() );
551         ScPatternAttr::FillToEditItemSet( *pEEItemSet, rItemSet );
552         pNoteEngine->SetDefaults( pEEItemSet );      // edit engine takes ownership
553 	}
554 	return *pNoteEngine;
555 }
556 
557 void ScDocument::ResetClip( ScDocument* pSourceDoc, const ScMarkData* pMarks )
558 {
559 	if (bIsClip)
560 	{
561 		InitClipPtrs(pSourceDoc);
562 
563 		for (SCTAB i = 0; i <= MAXTAB; i++)
564 			if (pSourceDoc->pTab[i])
565 				if (!pMarks || pMarks->GetTableSelect(i))
566 				{
567 					String aString;
568 					pSourceDoc->pTab[i]->GetName(aString);
569 					pTab[i] = new ScTable(this, i, aString);
570 					pTab[i]->SetLayoutRTL( pSourceDoc->pTab[i]->IsLayoutRTL() );
571 					nMaxTableNumber = i+1;
572 				}
573 	}
574 	else
575 	{
576 		DBG_ERROR("ResetClip");
577 	}
578 }
579 
580 void ScDocument::ResetClip( ScDocument* pSourceDoc, SCTAB nTab )
581 {
582 	if (bIsClip)
583 	{
584 		InitClipPtrs(pSourceDoc);
585 
586 		pTab[nTab] = new ScTable(this, nTab,
587 							String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM("baeh")));
588 		if (pSourceDoc->pTab[nTab])
589 			pTab[nTab]->SetLayoutRTL( pSourceDoc->pTab[nTab]->IsLayoutRTL() );
590 		nMaxTableNumber = nTab+1;
591 	}
592 	else
593 	{
594 		DBG_ERROR("ResetClip");
595 	}
596 }
597 
598 void ScDocument::DeleteNumberFormat( const sal_uInt32* /* pDelKeys */, sal_uInt32 /* nCount */ )
599 {
600 /*
601 	for (sal_uLong i = 0; i < nCount; i++)
602 		xPoolHelper->GetFormTable()->DeleteEntry(pDelKeys[i]);
603 */
604 }
605 
606 void ScDocument::PutCell( SCCOL nCol, SCROW nRow, SCTAB nTab,
607 						  ScBaseCell* pCell, sal_uLong nFormatIndex, sal_Bool bForceTab )
608 {
609 	if (VALIDTAB(nTab))
610 	{
611 		if ( bForceTab && !pTab[nTab] )
612 		{
613 			sal_Bool bExtras = !bIsUndo;		// Spaltenbreiten, Zeilenhoehen, Flags
614 
615 			pTab[nTab] = new ScTable(this, nTab,
616 								String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM("temp")),
617 								bExtras, bExtras);
618 		}
619 
620 		if (pTab[nTab])
621 			pTab[nTab]->PutCell( nCol, nRow, nFormatIndex, pCell );
622 	}
623 }
624 
625 //UNUSED2009-05 void ScDocument::PutCell( const ScAddress& rPos, ScBaseCell* pCell,
626 //UNUSED2009-05                             sal_uLong nFormatIndex, sal_Bool bForceTab )
627 //UNUSED2009-05 {
628 //UNUSED2009-05     SCTAB nTab = rPos.Tab();
629 //UNUSED2009-05     if ( bForceTab && !pTab[nTab] )
630 //UNUSED2009-05     {
631 //UNUSED2009-05         sal_Bool bExtras = !bIsUndo;        // Spaltenbreiten, Zeilenhoehen, Flags
632 //UNUSED2009-05
633 //UNUSED2009-05         pTab[nTab] = new ScTable(this, nTab,
634 //UNUSED2009-05                             String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM("temp")),
635 //UNUSED2009-05                             bExtras, bExtras);
636 //UNUSED2009-05     }
637 //UNUSED2009-05
638 //UNUSED2009-05     if (pTab[nTab])
639 //UNUSED2009-05         pTab[nTab]->PutCell( rPos, nFormatIndex, pCell );
640 //UNUSED2009-05 }
641 
642 sal_Bool ScDocument::GetPrintArea( SCTAB nTab, SCCOL& rEndCol, SCROW& rEndRow,
643 								sal_Bool bNotes ) const
644 {
645 	if (ValidTab(nTab) && pTab[nTab])
646 	{
647 		sal_Bool bAny = pTab[nTab]->GetPrintArea( rEndCol, rEndRow, bNotes );
648 		if (pDrawLayer)
649 		{
650 			ScRange aDrawRange(0,0,nTab, MAXCOL,MAXROW,nTab);
651 			if (DrawGetPrintArea( aDrawRange, sal_True, sal_True ))
652 			{
653 				if (aDrawRange.aEnd.Col()>rEndCol) rEndCol=aDrawRange.aEnd.Col();
654 				if (aDrawRange.aEnd.Row()>rEndRow) rEndRow=aDrawRange.aEnd.Row();
655 				bAny = sal_True;
656 			}
657 		}
658 		return bAny;
659 	}
660 
661 	rEndCol = 0;
662 	rEndRow = 0;
663 	return sal_False;
664 }
665 
666 sal_Bool ScDocument::GetPrintAreaHor( SCTAB nTab, SCROW nStartRow, SCROW nEndRow,
667 										SCCOL& rEndCol, sal_Bool bNotes ) const
668 {
669 	if (ValidTab(nTab) && pTab[nTab])
670 	{
671 		sal_Bool bAny = pTab[nTab]->GetPrintAreaHor( nStartRow, nEndRow, rEndCol, bNotes );
672 		if (pDrawLayer)
673 		{
674 			ScRange aDrawRange(0,nStartRow,nTab, MAXCOL,nEndRow,nTab);
675 			if (DrawGetPrintArea( aDrawRange, sal_True, sal_False ))
676 			{
677 				if (aDrawRange.aEnd.Col()>rEndCol) rEndCol=aDrawRange.aEnd.Col();
678 				bAny = sal_True;
679 			}
680 		}
681 		return bAny;
682 	}
683 
684 	rEndCol = 0;
685 	return sal_False;
686 }
687 
688 sal_Bool ScDocument::GetPrintAreaVer( SCTAB nTab, SCCOL nStartCol, SCCOL nEndCol,
689 										SCROW& rEndRow, sal_Bool bNotes ) const
690 {
691 	if (ValidTab(nTab) && pTab[nTab])
692 	{
693 		sal_Bool bAny = pTab[nTab]->GetPrintAreaVer( nStartCol, nEndCol, rEndRow, bNotes );
694 		if (pDrawLayer)
695 		{
696 			ScRange aDrawRange(nStartCol,0,nTab, nEndCol,MAXROW,nTab);
697 			if (DrawGetPrintArea( aDrawRange, sal_False, sal_True ))
698 			{
699 				if (aDrawRange.aEnd.Row()>rEndRow) rEndRow=aDrawRange.aEnd.Row();
700 				bAny = sal_True;
701 			}
702 		}
703 		return bAny;
704 	}
705 
706 	rEndRow = 0;
707 	return sal_False;
708 }
709 
710 sal_Bool ScDocument::GetDataStart( SCTAB nTab, SCCOL& rStartCol, SCROW& rStartRow ) const
711 {
712 	if (ValidTab(nTab) && pTab[nTab])
713 	{
714 		sal_Bool bAny = pTab[nTab]->GetDataStart( rStartCol, rStartRow );
715 		if (pDrawLayer)
716 		{
717 			ScRange aDrawRange(0,0,nTab, MAXCOL,MAXROW,nTab);
718 			if (DrawGetPrintArea( aDrawRange, sal_True, sal_True ))
719 			{
720 				if (aDrawRange.aStart.Col()<rStartCol) rStartCol=aDrawRange.aStart.Col();
721 				if (aDrawRange.aStart.Row()<rStartRow) rStartRow=aDrawRange.aStart.Row();
722 				bAny = sal_True;
723 			}
724 		}
725 		return bAny;
726 	}
727 
728 	rStartCol = 0;
729 	rStartRow = 0;
730 	return sal_False;
731 }
732 
733 sal_Bool ScDocument::MoveTab( SCTAB nOldPos, SCTAB nNewPos )
734 {
735 	if (nOldPos == nNewPos) return sal_False;
736 	sal_Bool bValid = sal_False;
737 	if (VALIDTAB(nOldPos))
738 	{
739 		if (pTab[nOldPos])
740 		{
741 			SCTAB nTabCount = GetTableCount();
742 			if (nTabCount > 1)
743 			{
744 				sal_Bool bOldAutoCalc = GetAutoCalc();
745 				SetAutoCalc( sal_False );	// Mehrfachberechnungen vermeiden
746 				SetNoListening( sal_True );
747 				ScProgress* pProgress = new ScProgress( GetDocumentShell(),
748 					ScGlobal::GetRscString(STR_UNDO_MOVE_TAB), GetCodeCount() );
749 				if (nNewPos == SC_TAB_APPEND)
750 					nNewPos = nTabCount-1;
751 
752 				//	Referenz-Updaterei
753 				//!	mit UpdateReference zusammenfassen!
754 
755 				SCsTAB nDz = ((SCsTAB)nNewPos) - (SCsTAB)nOldPos;
756 				ScRange aSourceRange( 0,0,nOldPos, MAXCOL,MAXROW,nOldPos );
757 				pRangeName->UpdateTabRef(nOldPos, 3, nNewPos);
758 				pDBCollection->UpdateMoveTab( nOldPos, nNewPos );
759 				xColNameRanges->UpdateReference( URM_REORDER, this, aSourceRange, 0,0,nDz );
760 				xRowNameRanges->UpdateReference( URM_REORDER, this, aSourceRange, 0,0,nDz );
761 				if (pDPCollection)
762 					pDPCollection->UpdateReference( URM_REORDER, aSourceRange, 0,0,nDz );
763 				if (pDetOpList)
764 					pDetOpList->UpdateReference( this, URM_REORDER, aSourceRange, 0,0,nDz );
765 				UpdateChartRef( URM_REORDER,
766 									0,0,nOldPos, MAXCOL,MAXROW,nOldPos, 0,0,nDz );
767 				UpdateRefAreaLinks( URM_REORDER, aSourceRange, 0,0,nDz );
768 				if ( pCondFormList )
769 					pCondFormList->UpdateMoveTab( nOldPos, nNewPos );
770 				if ( pValidationList )
771 					pValidationList->UpdateMoveTab( nOldPos, nNewPos );
772 				if ( pUnoBroadcaster )
773 					pUnoBroadcaster->Broadcast( ScUpdateRefHint( URM_REORDER,
774 									aSourceRange, 0,0,nDz ) );
775 
776 				ScTable* pSaveTab = pTab[nOldPos];
777 				SCTAB i;
778 				for (i = nOldPos + 1; i < nTabCount; i++)
779 					pTab[i - 1] = pTab[i];
780 				pTab[i-1] = NULL;
781 				for (i = nTabCount - 1; i > nNewPos; i--)
782 					pTab[i] = pTab[i - 1];
783 				pTab[nNewPos] = pSaveTab;
784 				for (i = 0; i <= MAXTAB; i++)
785 					if (pTab[i])
786 						pTab[i]->UpdateMoveTab( nOldPos, nNewPos, i, *pProgress );
787 				delete pProgress;	// freimachen fuer evtl. andere
788 				for (i = 0; i <= MAXTAB; i++)
789 					if (pTab[i])
790 						pTab[i]->UpdateCompile();
791 				SetNoListening( sal_False );
792 				for (i = 0; i <= MAXTAB; i++)
793 					if (pTab[i])
794 						pTab[i]->StartAllListeners();
795 				// #81844# sheet names of references may not be valid until sheet is moved
796 				pChartListenerCollection->UpdateScheduledSeriesRanges();
797 				SetDirty();
798 				SetAutoCalc( bOldAutoCalc );
799 
800 				if (pDrawLayer)
801 					DrawMovePage( static_cast<sal_uInt16>(nOldPos), static_cast<sal_uInt16>(nNewPos) );
802 
803 				bValid = sal_True;
804 			}
805 		}
806 	}
807 	return bValid;
808 }
809 
810 sal_Bool ScDocument::CopyTab( SCTAB nOldPos, SCTAB nNewPos, const ScMarkData* pOnlyMarked )
811 {
812 	if (SC_TAB_APPEND == nNewPos ) nNewPos = nMaxTableNumber;
813 	String aName;
814 	GetName(nOldPos, aName);
815 
816 	//	vorneweg testen, ob der Prefix als gueltig erkannt wird
817 	//	wenn nicht, nur doppelte vermeiden
818 	sal_Bool bPrefix = ValidTabName( aName );
819 	DBG_ASSERT(bPrefix, "ungueltiger Tabellenname");
820 	SCTAB nDummy;
821 
822 	CreateValidTabName(aName);
823 
824 	sal_Bool bValid;
825 	if (bPrefix)
826 		bValid = ( ValidNewTabName(aName) && (nMaxTableNumber <= MAXTAB) );
827 	else
828 		bValid = ( !GetTable( aName, nDummy ) && (nMaxTableNumber <= MAXTAB) );
829 
830 	sal_Bool bOldAutoCalc = GetAutoCalc();
831 	SetAutoCalc( sal_False );	// Mehrfachberechnungen vermeiden
832 	if (bValid)
833 	{
834 		if (nNewPos == nMaxTableNumber)
835 		{
836 			pTab[nMaxTableNumber] = new ScTable(this, nMaxTableNumber, aName);
837 			++nMaxTableNumber;
838 		}
839 		else
840 		{
841 			if (VALIDTAB(nNewPos) && (nNewPos < nMaxTableNumber))
842 			{
843 				SetNoListening( sal_True );
844 
845 				ScRange aRange( 0,0,nNewPos, MAXCOL,MAXROW,MAXTAB );
846 				xColNameRanges->UpdateReference( URM_INSDEL, this, aRange, 0,0,1 );
847 				xRowNameRanges->UpdateReference( URM_INSDEL, this, aRange, 0,0,1 );
848 				pRangeName->UpdateTabRef(nNewPos, 1);
849 				pDBCollection->UpdateReference(
850 									URM_INSDEL, 0,0,nNewPos, MAXCOL,MAXROW,MAXTAB, 0,0,1 );
851 				if (pDPCollection)
852 					pDPCollection->UpdateReference( URM_INSDEL, aRange, 0,0,1 );
853 				if (pDetOpList)
854 					pDetOpList->UpdateReference( this, URM_INSDEL, aRange, 0,0,1 );
855 				UpdateChartRef( URM_INSDEL, 0,0,nNewPos, MAXCOL,MAXROW,MAXTAB, 0,0,1 );
856 				UpdateRefAreaLinks( URM_INSDEL, aRange, 0,0,1 );
857 				if ( pUnoBroadcaster )
858 					pUnoBroadcaster->Broadcast( ScUpdateRefHint( URM_INSDEL, aRange, 0,0,1 ) );
859 
860 				SCTAB i;
861 				for (i = 0; i <= MAXTAB; i++)
862 					if (pTab[i] && i != nOldPos)
863 						pTab[i]->UpdateInsertTab(nNewPos);
864 				for (i = nMaxTableNumber; i > nNewPos; i--)
865 					pTab[i] = pTab[i - 1];
866 				if (nNewPos <= nOldPos)
867 					nOldPos++;
868 				pTab[nNewPos] = new ScTable(this, nNewPos, aName);
869 				++nMaxTableNumber;
870 				bValid = sal_True;
871 				for (i = 0; i <= MAXTAB; i++)
872 					if (pTab[i] && i != nOldPos && i != nNewPos)
873 						pTab[i]->UpdateCompile();
874 				SetNoListening( sal_False );
875 				for (i = 0; i <= MAXTAB; i++)
876 					if (pTab[i] && i != nOldPos && i != nNewPos)
877 						pTab[i]->StartAllListeners();
878 
879 				//	update conditional formats after table is inserted
880 				if ( pCondFormList )
881 					pCondFormList->UpdateReference( URM_INSDEL, aRange, 0,0,1 );
882 				if ( pValidationList )
883 					pValidationList->UpdateReference( URM_INSDEL, aRange, 0,0,1 );
884 				// #81844# sheet names of references may not be valid until sheet is copied
885 				pChartListenerCollection->UpdateScheduledSeriesRanges();
886 			}
887 			else
888 				bValid = sal_False;
889 		}
890 	}
891 	if (bValid)
892 	{
893 		SetNoListening( sal_True );		// noch nicht bei CopyToTable/Insert
894 		pTab[nOldPos]->CopyToTable(0, 0, MAXCOL, MAXROW, IDF_ALL, (pOnlyMarked != NULL),
895 										pTab[nNewPos], pOnlyMarked );
896         pTab[nNewPos]->SetTabBgColor(pTab[nOldPos]->GetTabBgColor());
897 
898 		SCsTAB nDz;
899 /*		if (nNewPos < nOldPos)
900 			nDz = ((short)nNewPos) - (short)nOldPos + 1;
901 		else
902 */			nDz = ((short)nNewPos) - (short)nOldPos;
903 		pTab[nNewPos]->UpdateReference(URM_COPY, 0, 0, nNewPos , MAXCOL, MAXROW,
904 										nNewPos, 0, 0, nDz, NULL);
905 
906 		pTab[nNewPos]->UpdateInsertTabAbs(nNewPos); // alle abs. um eins hoch!!
907 		pTab[nOldPos]->UpdateInsertTab(nNewPos);
908 
909 		pTab[nOldPos]->UpdateCompile();
910 		pTab[nNewPos]->UpdateCompile( sal_True );	// #67996# maybe already compiled in Clone, but used names need recompilation
911 		SetNoListening( sal_False );
912 		pTab[nOldPos]->StartAllListeners();
913 		pTab[nNewPos]->StartAllListeners();
914 		SetDirty();
915 		SetAutoCalc( bOldAutoCalc );
916 
917 		if (pDrawLayer)
918 			DrawCopyPage( static_cast<sal_uInt16>(nOldPos), static_cast<sal_uInt16>(nNewPos) );
919 
920 		pTab[nNewPos]->SetPageStyle( pTab[nOldPos]->GetPageStyle() );
921         pTab[nNewPos]->SetPendingRowHeights( pTab[nOldPos]->IsPendingRowHeights() );
922 	}
923 	else
924 		SetAutoCalc( bOldAutoCalc );
925 	return bValid;
926 }
927 
928 void VBA_InsertModule( ScDocument& rDoc, SCTAB nTab, String& sModuleName, String& sModuleSource );
929 
930 sal_uLong ScDocument::TransferTab( ScDocument* pSrcDoc, SCTAB nSrcPos,
931 								SCTAB nDestPos, sal_Bool bInsertNew,
932 								sal_Bool bResultsOnly )
933 {
934 	sal_uLong nRetVal = 1;						// 0 => Fehler 1 = ok
935 											// 2 => RefBox, 3 => NameBox
936 											// 4 => beides
937 	sal_Bool bValid = sal_True;
938 	if (bInsertNew)				// neu einfuegen
939 	{
940 		String aName;
941 		pSrcDoc->GetName(nSrcPos, aName);
942 		CreateValidTabName(aName);
943 		bValid = InsertTab(nDestPos, aName);
944 	}
945 	else						// bestehende Tabelle ersetzen
946 	{
947 		if (VALIDTAB(nDestPos) && pTab[nDestPos])
948 		{
949 			pTab[nDestPos]->DeleteArea( 0,0, MAXCOL,MAXROW, IDF_ALL );
950 		}
951 		else
952 			bValid = sal_False;
953 	}
954 
955 	if (bValid)
956 	{
957         sal_Bool bOldAutoCalcSrc = sal_False;
958 		sal_Bool bOldAutoCalc = GetAutoCalc();
959 		SetAutoCalc( sal_False );	// Mehrfachberechnungen vermeiden
960 		SetNoListening( sal_True );
961 		if ( bResultsOnly )
962 		{
963 			bOldAutoCalcSrc = pSrcDoc->GetAutoCalc();
964 			pSrcDoc->SetAutoCalc( sal_True );	// falls was berechnet werden muss
965 		}
966 
967 		{
968             NumFmtMergeHandler aNumFmtMergeHdl(this, pSrcDoc);
969 
970             nDestPos = Min(nDestPos, (SCTAB)(GetTableCount() - 1));
971             {   // scope for bulk broadcast
972                 ScBulkBroadcast aBulkBroadcast( pBASM);
973                 pSrcDoc->pTab[nSrcPos]->CopyToTable(0, 0, MAXCOL, MAXROW,
974                         ( bResultsOnly ? IDF_ALL & ~IDF_FORMULA : IDF_ALL),
975                         sal_False, pTab[nDestPos] );
976             }
977         }
978 		pTab[nDestPos]->SetTabNo(nDestPos);
979 
980 		if ( !bResultsOnly )
981 		{
982             sal_Bool bNamesLost = sal_False;
983 			sal_uInt16 nSrcRangeNames = pSrcDoc->pRangeName->GetCount();
984 			// array containing range names which might need update of indices
985 			ScRangeData** pSrcRangeNames = nSrcRangeNames ? new ScRangeData* [nSrcRangeNames] : NULL;
986 			// the index mapping thereof
987             ScRangeData::IndexMap aSrcRangeMap;
988 			sal_Bool bRangeNameReplace = sal_False;
989 
990             // find named ranges that are used in the source sheet
991             std::set<sal_uInt16> aUsedNames;
992             pSrcDoc->pTab[nSrcPos]->FindRangeNamesInUse( 0, 0, MAXCOL, MAXROW, aUsedNames );
993 
994 			for (sal_uInt16 i = 0; i < nSrcRangeNames; i++)		//! DB-Bereiche Pivot-Bereiche auch !!!
995 			{
996 				ScRangeData* pSrcData = (*pSrcDoc->pRangeName)[i];
997 				sal_uInt16 nOldIndex = pSrcData->GetIndex();
998                 bool bInUse = ( aUsedNames.find(nOldIndex) != aUsedNames.end() );
999 				if (bInUse)
1000 				{
1001 				    sal_uInt16 nExisting = 0;
1002 				    if ( pRangeName->SearchName( pSrcData->GetName(), nExisting ) )
1003 				    {
1004                         // the name exists already in the destination document
1005                         // -> use the existing name, but show a warning
1006                         // (when refreshing links, the existing name is used and the warning not shown)
1007 
1008 				        ScRangeData* pExistingData = (*pRangeName)[nExisting];
1009 				        sal_uInt16 nExistingIndex = pExistingData->GetIndex();
1010 
1011                         pSrcRangeNames[i] = NULL;       // don't modify the named range
1012                         aSrcRangeMap.insert(
1013                             ScRangeData::IndexMap::value_type(nOldIndex, nExistingIndex));
1014                         bRangeNameReplace = sal_True;
1015                         bNamesLost = sal_True;
1016 				    }
1017 				    else
1018 				    {
1019     					ScRangeData* pData = new ScRangeData( *pSrcData );
1020     					pData->SetDocument(this);
1021     					if ( pRangeName->FindIndex( pData->GetIndex() ) )
1022     						pData->SetIndex(0);		// need new index, done in Insert
1023     					if (!pRangeName->Insert(pData))
1024     					{
1025     					    DBG_ERROR("can't insert name");     // shouldn't happen
1026     						delete pData;
1027     					}
1028     					else
1029     					{
1030     						pData->TransferTabRef( nSrcPos, nDestPos );
1031     						pSrcRangeNames[i] = pData;
1032     						sal_uInt16 nNewIndex = pData->GetIndex();
1033                             aSrcRangeMap.insert(
1034                                 ScRangeData::IndexMap::value_type(nOldIndex, nNewIndex));
1035     						if ( !bRangeNameReplace )
1036     							bRangeNameReplace = ( nOldIndex != nNewIndex );
1037     					}
1038 				    }
1039 				}
1040 				else
1041 				{
1042 					pSrcRangeNames[i] = NULL;
1043 					//aSrcRangeMap.SetPair( i, 0, 0 );		// not needed, defaulted
1044 				}
1045 			}
1046 			if ( bRangeNameReplace )
1047 			{
1048 				// first update all inserted named formulas if they contain other
1049 				// range names and used indices changed
1050 				for (sal_uInt16 i = 0; i < nSrcRangeNames; i++)		//! DB-Bereiche Pivot-Bereiche auch
1051 				{
1052 					if ( pSrcRangeNames[i] )
1053 						pSrcRangeNames[i]->ReplaceRangeNamesInUse( aSrcRangeMap );
1054 				}
1055 				// then update the formulas, they might need the just updated range names
1056 				pTab[nDestPos]->ReplaceRangeNamesInUse( 0, 0, MAXCOL, MAXROW, aSrcRangeMap );
1057 			}
1058 			if ( pSrcRangeNames )
1059 				delete [] pSrcRangeNames;
1060 
1061 			SCsTAB nDz = ((SCsTAB)nDestPos) - (SCsTAB)nSrcPos;
1062 			pTab[nDestPos]->UpdateReference(URM_COPY, 0, 0, nDestPos,
1063 													 MAXCOL, MAXROW, nDestPos,
1064 													 0, 0, nDz, NULL);
1065             // Test for outside absolute references for info box
1066             sal_Bool bIsAbsRef = pSrcDoc->pTab[nSrcPos]->TestTabRefAbs(nSrcPos);
1067             // Readjust self-contained absolute references to this sheet
1068             pTab[nDestPos]->TestTabRefAbs(nSrcPos);
1069 			if (bIsAbsRef)
1070 			{
1071 				nRetVal += 1;
1072                     // InfoBox AbsoluteRefs sind moeglicherweise nicht mehr korrekt!!
1073 			}
1074             if (bNamesLost)
1075             {
1076                 nRetVal += 2;
1077                 // message: duplicate names
1078             }
1079 			pTab[nDestPos]->CompileAll();
1080 		}
1081 
1082 		SetNoListening( sal_False );
1083 		if ( !bResultsOnly )
1084 			pTab[nDestPos]->StartAllListeners();
1085         SetDirty( ScRange( 0, 0, nDestPos, MAXCOL, MAXROW, nDestPos));
1086 
1087 		if ( bResultsOnly )
1088 			pSrcDoc->SetAutoCalc( bOldAutoCalcSrc );
1089 		SetAutoCalc( bOldAutoCalc );
1090 
1091 		//	Drawing kopieren
1092 
1093 		if (bInsertNew)
1094 			TransferDrawPage( pSrcDoc, nSrcPos, nDestPos );
1095 
1096         pTab[nDestPos]->SetPendingRowHeights( pSrcDoc->pTab[nSrcPos]->IsPendingRowHeights() );
1097 	}
1098 	if (!bValid)
1099 		nRetVal = 0;
1100     sal_Bool bVbaEnabled = IsInVBAMode();
1101 
1102     if ( bVbaEnabled  )
1103     {
1104         SfxObjectShell* pSrcShell = pSrcDoc ? pSrcDoc->GetDocumentShell() : NULL;
1105         if ( pSrcShell )
1106         {
1107             StarBASIC* pStarBASIC = pSrcShell ? pSrcShell->GetBasic() : NULL;
1108             String aLibName( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
1109             if ( pSrcShell && pSrcShell->GetBasicManager()->GetName().Len() > 0 )
1110             {
1111                 aLibName = pSrcShell->GetBasicManager()->GetName();
1112                 pStarBASIC = pSrcShell->GetBasicManager()->GetLib( aLibName );
1113             }
1114 
1115             String sCodeName;
1116             String sSource;
1117             uno::Reference< script::XLibraryContainer > xLibContainer = pSrcShell->GetBasicContainer();
1118             uno::Reference< container::XNameContainer > xLib;
1119             if( xLibContainer.is() )
1120             {
1121                 uno::Any aLibAny = xLibContainer->getByName( aLibName );
1122                 aLibAny >>= xLib;
1123             }
1124 
1125             if( xLib.is() )
1126             {
1127                 String sSrcCodeName;
1128                 pSrcDoc->GetCodeName( nSrcPos, sSrcCodeName );
1129                 rtl::OUString sRTLSource;
1130                 xLib->getByName( sSrcCodeName ) >>= sRTLSource;
1131                 sSource = sRTLSource;
1132             }
1133             VBA_InsertModule( *this, nDestPos, sCodeName, sSource );
1134         }
1135     }
1136 
1137 	return nRetVal;
1138 }
1139 
1140 //	----------------------------------------------------------------------------
1141 
1142 void ScDocument::SetError( SCCOL nCol, SCROW nRow, SCTAB nTab, const sal_uInt16 nError)
1143 {
1144 	if (VALIDTAB(nTab))
1145 		if (pTab[nTab])
1146 			pTab[nTab]->SetError( nCol, nRow, nError );
1147 }
1148 
1149 void ScDocument::EraseNonUsedSharedNames(sal_uInt16 nLevel)
1150 {
1151 	for (sal_uInt16 i = 0; i < pRangeName->GetCount(); i++)
1152 	{
1153 		ScRangeData* pRangeData = (*pRangeName)[i];
1154 		if (pRangeData && pRangeData->HasType(RT_SHARED))
1155 		{
1156 			String aName;
1157 			pRangeData->GetName(aName);
1158 			aName.Erase(0, 6);						// !!! vgl. Table4, FillFormula !!
1159 			sal_uInt16 nInd = (sal_uInt16) aName.ToInt32();
1160 			if (nInd <= nLevel)
1161 			{
1162 				sal_uInt16 nIndex = pRangeData->GetIndex();
1163 				sal_Bool bInUse = sal_False;
1164 				for (SCTAB j = 0; !bInUse && (j <= MAXTAB); j++)
1165 				{
1166 					if (pTab[j])
1167 						bInUse = pTab[j]->IsRangeNameInUse(0, 0, MAXCOL-1, MAXROW-1,
1168 														   nIndex);
1169 				}
1170 				if (!bInUse)
1171 					pRangeName->AtFree(i);
1172 			}
1173 		}
1174 	}
1175 }
1176 
1177 //	----------------------------------------------------------------------------
1178 
1179 void ScDocument::SetConsolidateDlgData( const ScConsolidateParam* pData )
1180 {
1181 	delete pConsolidateDlgData;
1182 
1183 	if ( pData )
1184 		pConsolidateDlgData = new ScConsolidateParam( *pData );
1185 	else
1186 		pConsolidateDlgData = NULL;
1187 }
1188 
1189 void ScDocument::SetChangeViewSettings(const ScChangeViewSettings& rNew)
1190 {
1191 	if (pChangeViewSettings==NULL)
1192 		pChangeViewSettings = new ScChangeViewSettings;
1193 
1194 	DBG_ASSERT( pChangeViewSettings, "Oops. No ChangeViewSettings :-( by!" );
1195 
1196 	*pChangeViewSettings=rNew;
1197 }
1198 
1199 //	----------------------------------------------------------------------------
1200 
1201 ScFieldEditEngine* ScDocument::CreateFieldEditEngine()
1202 {
1203     ScFieldEditEngine* pNewEditEngine = NULL;
1204 	if (!pCacheFieldEditEngine)
1205 	{
1206         pNewEditEngine = new ScFieldEditEngine( GetEnginePool(),
1207 			GetEditPool(), sal_False );
1208 	}
1209 	else
1210 	{
1211         if ( !bImportingXML )
1212         {
1213             // #i66209# previous use might not have restored update mode,
1214             // ensure same state as for a new EditEngine (UpdateMode = sal_True)
1215             if ( !pCacheFieldEditEngine->GetUpdateMode() )
1216                 pCacheFieldEditEngine->SetUpdateMode(sal_True);
1217         }
1218 
1219         pNewEditEngine = pCacheFieldEditEngine;
1220 		pCacheFieldEditEngine = NULL;
1221 	}
1222     return pNewEditEngine;
1223 }
1224 
1225 void ScDocument::DisposeFieldEditEngine(ScFieldEditEngine*& rpEditEngine)
1226 {
1227 	if (!pCacheFieldEditEngine && rpEditEngine)
1228 	{
1229 		pCacheFieldEditEngine = rpEditEngine;
1230 		pCacheFieldEditEngine->Clear();
1231 	}
1232 	else
1233 		delete rpEditEngine;
1234 	rpEditEngine = NULL;
1235 }
1236 
1237 //	----------------------------------------------------------------------------
1238 
1239 // static
1240 ScRecursionHelper* ScDocument::CreateRecursionHelperInstance()
1241 {
1242     return new ScRecursionHelper;
1243 }
1244 
1245 //	----------------------------------------------------------------------------
1246 
1247 ScLookupCache & ScDocument::GetLookupCache( const ScRange & rRange )
1248 {
1249     ScLookupCache* pCache = 0;
1250     if (!pLookupCacheMapImpl)
1251         pLookupCacheMapImpl = new ScLookupCacheMapImpl;
1252     ScLookupCacheMap::iterator it( pLookupCacheMapImpl->aCacheMap.find( rRange));
1253     if (it == pLookupCacheMapImpl->aCacheMap.end())
1254     {
1255         pCache = new ScLookupCache( this, rRange);
1256         AddLookupCache( *pCache);
1257     }
1258     else
1259         pCache = (*it).second;
1260     return *pCache;
1261 }
1262 
1263 void ScDocument::AddLookupCache( ScLookupCache & rCache )
1264 {
1265     if (!pLookupCacheMapImpl->aCacheMap.insert( ::std::pair< const ScRange,
1266                 ScLookupCache*>( rCache.getRange(), &rCache)).second)
1267     {
1268         DBG_ERRORFILE( "ScDocument::AddLookupCache: couldn't add to hash map");
1269     }
1270     else
1271         StartListeningArea( rCache.getRange(), &rCache);
1272 }
1273 
1274 void ScDocument::RemoveLookupCache( ScLookupCache & rCache )
1275 {
1276     ScLookupCacheMap::iterator it( pLookupCacheMapImpl->aCacheMap.find(
1277                 rCache.getRange()));
1278     if (it == pLookupCacheMapImpl->aCacheMap.end())
1279     {
1280         DBG_ERRORFILE( "ScDocument::RemoveLookupCache: range not found in hash map");
1281     }
1282     else
1283     {
1284         ScLookupCache* pCache = (*it).second;
1285         pLookupCacheMapImpl->aCacheMap.erase( it);
1286         EndListeningArea( pCache->getRange(), &rCache);
1287     }
1288 }
1289 
1290 void ScDocument::ClearLookupCaches()
1291 {
1292     if( pLookupCacheMapImpl )
1293         pLookupCacheMapImpl->clear();
1294 }
1295