re-enabled user-defined numeric fields for dBase export
[LibreOffice.git] / sc / source / core / data / documen2.cxx
blobdcdae98dd47902e61b8a2b3f3bd95d6598cdad51
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 "scitems.hxx"
21 #include <editeng/eeitem.hxx>
23 #include <editeng/editeng.hxx>
24 #include <editeng/forbiddencharacterstable.hxx>
25 #include <svx/xtable.hxx>
26 #include <sfx2/linkmgr.hxx>
27 #include <svx/svdpool.hxx>
28 #include <svx/svdobj.hxx>
29 #include <sfx2/bindings.hxx>
30 #include <sfx2/objsh.hxx>
31 #include <sfx2/docfile.hxx>
32 #include <sfx2/printer.hxx>
33 #include <svl/zforlist.hxx>
34 #include <svl/zformat.hxx>
35 #include <vcl/virdev.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <svl/PasswordHelper.hxx>
38 #include <tools/tenccvt.hxx>
39 #include <tools/urlobj.hxx>
40 #include <rtl/crc.h>
41 #include <basic/basmgr.hxx>
43 #include "document.hxx"
44 #include "table.hxx"
45 #include "attrib.hxx"
46 #include "patattr.hxx"
47 #include "rangenam.hxx"
48 #include "dbdata.hxx"
49 #include "pivot.hxx"
50 #include "docpool.hxx"
51 #include "stlpool.hxx"
52 #include "stlsheet.hxx"
53 #include "globstr.hrc"
54 #include "chartarr.hxx"
55 #include "chartlock.hxx"
56 #include "rechead.hxx"
57 #include "global.hxx"
58 #include "brdcst.hxx"
59 #include "bcaslot.hxx"
60 #include "adiasync.hxx"
61 #include "addinlis.hxx"
62 #include "chartlis.hxx"
63 #include "markdata.hxx"
64 #include "conditio.hxx"
65 #include "colorscale.hxx"
66 #include "validat.hxx"
67 #include "progress.hxx"
68 #include "detdata.hxx"
69 #include "sc.hrc" // FID_DATACHANGED
70 #include "ddelink.hxx"
71 #include "chgtrack.hxx"
72 #include "chgviset.hxx"
73 #include "editutil.hxx"
74 #include "hints.hxx"
75 #include "dpobject.hxx"
76 #include "scrdata.hxx"
77 #include "poolhelp.hxx"
78 #include "unoreflist.hxx"
79 #include "listenercalls.hxx"
80 #include "recursionhelper.hxx"
81 #include "lookupcache.hxx"
82 #include "externalrefmgr.hxx"
83 #include "tabprotection.hxx"
84 #include "formulaparserpool.hxx"
85 #include "clipparam.hxx"
86 #include "macromgr.hxx"
87 #include "cell.hxx"
88 #include "formulacell.hxx"
89 #include "clipcontext.hxx"
91 using namespace com::sun::star;
93 // pImpl because including lookupcache.hxx in document.hxx isn't wanted, and
94 // dtor plus helpers are convenient.
95 struct ScLookupCacheMapImpl
97 ScLookupCacheMap aCacheMap;
98 ~ScLookupCacheMapImpl()
100 freeCaches();
102 void clear()
104 freeCaches();
105 // Zap map.
106 ScLookupCacheMap aTmp;
107 aCacheMap.swap( aTmp);
109 private:
110 void freeCaches()
112 for (ScLookupCacheMap::iterator it( aCacheMap.begin()); it != aCacheMap.end(); ++it)
113 delete (*it).second;
117 // STATIC DATA -----------------------------------------------------------
119 ScDocument::ScDocument( ScDocumentMode eMode,
120 SfxObjectShell* pDocShell ) :
121 xServiceManager( ::comphelper::getProcessServiceFactory() ),
122 mpUndoManager( NULL ),
123 pEditEngine( NULL ),
124 pNoteEngine( NULL ),
125 pShell( pDocShell ),
126 pPrinter( NULL ),
127 pVirtualDevice_100th_mm( NULL ),
128 pDrawLayer( NULL ),
129 pValidationList( NULL ),
130 pFormatExchangeList( NULL ),
131 pRangeName(NULL),
132 pDPCollection( NULL ),
133 pLinkManager( NULL ),
134 pFormulaTree( NULL ),
135 pEOFormulaTree( NULL ),
136 pFormulaTrack( NULL ),
137 pEOFormulaTrack( NULL ),
138 pClipData( NULL ),
139 pDetOpList(NULL),
140 pChangeTrack( NULL ),
141 pUnoBroadcaster( NULL ),
142 pUnoListenerCalls( NULL ),
143 pUnoRefUndoList( NULL ),
144 pChangeViewSettings( NULL ),
145 pScriptTypeData( NULL ),
146 pCacheFieldEditEngine( NULL ),
147 pDocProtection( NULL ),
148 mpClipParam( NULL),
149 pExternalRefMgr( NULL ),
150 mpMacroMgr( NULL ),
151 pViewOptions( NULL ),
152 pDocOptions( NULL ),
153 pExtDocOptions( NULL ),
154 pConsolidateDlgData( NULL ),
155 pRecursionHelper( NULL ),
156 pAutoNameCache( NULL ),
157 pLookupCacheMapImpl( NULL ),
158 pPreviewFont( NULL ),
159 pPreviewSelection( NULL ),
160 nUnoObjectId( 0 ),
161 nRangeOverflowType( 0 ),
162 aCurTextWidthCalcPos(MAXCOL,0,0),
163 aTableOpList( 0 ),
164 nFormulaCodeInTree(0),
165 nXMLImportedFormulaCount( 0 ),
166 nInterpretLevel(0),
167 nMacroInterpretLevel(0),
168 nInterpreterTableOpLevel(0),
169 nSrcVer( SC_CURRENT_VERSION ),
170 nSrcMaxRow( MAXROW ),
171 nFormulaTrackCount(0),
172 bHardRecalcState(false),
173 nVisibleTab( 0 ),
174 eLinkMode(LM_UNKNOWN),
175 bAutoCalc( eMode == SCDOCMODE_DOCUMENT ),
176 bAutoCalcShellDisabled( false ),
177 bForcedFormulaPending( false ),
178 bCalculatingFormulaTree( false ),
179 bIsClip( eMode == SCDOCMODE_CLIP ),
180 bIsUndo( eMode == SCDOCMODE_UNDO ),
181 bIsVisible( false ),
182 bIsEmbedded( false ),
183 bInsertingFromOtherDoc( false ),
184 bLoadingMedium( false ),
185 bImportingXML( false ),
186 bXMLFromWrapper( false ),
187 bCalcingAfterLoad( false ),
188 bNoListening( false ),
189 mbIdleEnabled(true),
190 bInLinkUpdate( false ),
191 bChartListenerCollectionNeedsUpdate( false ),
192 bHasForcedFormulas( false ),
193 bInDtorClear( false ),
194 bExpandRefs( false ),
195 bDetectiveDirty( false ),
196 nMacroCallMode( SC_MACROCALL_ALLOWED ),
197 bHasMacroFunc( false ),
198 nVisSpellState( 0 ),
199 nAsianCompression(SC_ASIANCOMPRESSION_INVALID),
200 nAsianKerning(SC_ASIANKERNING_INVALID),
201 bSetDrawDefaults( false ),
202 bPastingDrawFromOtherDoc( false ),
203 nInDdeLinkUpdate( 0 ),
204 bInUnoBroadcast( false ),
205 bInUnoListenerCall( false ),
206 eGrammar( formula::FormulaGrammar::GRAM_NATIVE ),
207 bStyleSheetUsageInvalid( true ),
208 mbUndoEnabled( true ),
209 mbAdjustHeightEnabled( true ),
210 mbExecuteLinkEnabled( true ),
211 mbChangeReadOnlyEnabled( false ),
212 mbStreamValidLocked( false ),
213 mbUserInteractionEnabled(true),
214 mnNamedRangesLockCount(0),
215 mbUseEmbedFonts(false)
217 SetStorageGrammar( formula::FormulaGrammar::GRAM_STORAGE_DEFAULT);
219 eSrcSet = osl_getThreadTextEncoding();
221 if ( eMode == SCDOCMODE_DOCUMENT )
223 if ( pDocShell )
224 pLinkManager = new sfx2::LinkManager( pDocShell );
226 xPoolHelper = new ScPoolHelper( this );
228 pBASM = new ScBroadcastAreaSlotMachine( this );
229 pChartListenerCollection = new ScChartListenerCollection( this );
230 pRefreshTimerControl = new ScRefreshTimerControl;
232 else
234 pBASM = NULL;
235 pChartListenerCollection = NULL;
236 pRefreshTimerControl = NULL;
238 pDBCollection = new ScDBCollection(this);
239 pSelectionAttr = NULL;
240 pChartCollection = new ScChartCollection;
241 apTemporaryChartLock = std::auto_ptr< ScTemporaryChartLock >( new ScTemporaryChartLock(this) );
242 xColNameRanges = new ScRangePairList;
243 xRowNameRanges = new ScRangePairList;
244 ImplCreateOptions();
245 // languages for a visible document are set by docshell later (from options)
246 SetLanguage( ScGlobal::eLnge, ScGlobal::eLnge, ScGlobal::eLnge );
248 aTrackTimer.SetTimeoutHdl( LINK( this, ScDocument, TrackTimeHdl ) );
249 aTrackTimer.SetTimeout( 100 );
252 sfx2::LinkManager* ScDocument::GetLinkManager() const
254 if ( bAutoCalc && !pLinkManager && pShell)
256 pLinkManager = new sfx2::LinkManager( pShell );
258 return pLinkManager;
262 void ScDocument::SetStorageGrammar( formula::FormulaGrammar::Grammar eGram )
264 OSL_PRECOND(
265 eGram == formula::FormulaGrammar::GRAM_ODFF ||
266 eGram == formula::FormulaGrammar::GRAM_PODF,
267 "ScDocument::SetStorageGrammar: wrong storage grammar");
269 eStorageGrammar = eGram;
271 // FIXME: the XML import shouldn't strip brackets, the compiler should
272 // digest them instead, which could also speedup reference recognition
273 // during import.
275 eXmlImportGrammar = formula::FormulaGrammar::mergeToGrammar( eGram,
276 formula::FormulaGrammar::CONV_OOO);
280 void ScDocument::SetDocVisible( bool bSet )
282 // called from view ctor - only for a visible document,
283 // each new sheet's RTL flag is initialized from the locale
284 bIsVisible = bSet;
288 sal_uInt32 ScDocument::GetDocumentID() const
290 const ScDocument* pThis = this;
291 sal_uInt32 nCrc = rtl_crc32( 0, &pThis, sizeof(ScDocument*) );
292 // the this pointer only might not be sufficient
293 nCrc = rtl_crc32( nCrc, &pShell, sizeof(SfxObjectShell*) );
294 return nCrc;
298 void ScDocument::StartChangeTracking()
300 if (!pChangeTrack)
301 pChangeTrack = new ScChangeTrack( this );
304 void ScDocument::EndChangeTracking()
306 delete pChangeTrack;
307 pChangeTrack = NULL;
310 void ScDocument::SetChangeTrack( ScChangeTrack* pTrack )
312 OSL_ENSURE( pTrack->GetDocument() == this, "SetChangeTrack: different documents" );
313 if ( !pTrack || pTrack == pChangeTrack || pTrack->GetDocument() != this )
314 return ;
315 EndChangeTracking();
316 pChangeTrack = pTrack;
320 IMPL_LINK_NOARG(ScDocument, TrackTimeHdl)
322 if ( ScDdeLink::IsInUpdate() ) // nicht verschachteln
324 aTrackTimer.Start(); // spaeter nochmal versuchen
326 else if (pShell) // ausfuehren
328 TrackFormulas();
329 pShell->Broadcast( SfxSimpleHint( FID_DATACHANGED ) );
331 // modified...
333 if (!pShell->IsModified())
335 pShell->SetModified( true );
336 SfxBindings* pBindings = GetViewBindings();
337 if (pBindings)
339 pBindings->Invalidate( SID_SAVEDOC );
340 pBindings->Invalidate( SID_DOC_MODIFIED );
345 return 0;
348 void ScDocument::StartTrackTimer()
350 if (!aTrackTimer.IsActive()) // nicht ewig aufschieben
351 aTrackTimer.Start();
354 ScDocument::~ScDocument()
356 OSL_PRECOND( !bInLinkUpdate, "bInLinkUpdate in dtor" );
358 bInDtorClear = true;
360 // first of all disable all refresh timers by deleting the control
361 if ( pRefreshTimerControl )
362 { // To be sure there isn't anything running do it with a protector,
363 // this ensures also that nothing needs the control anymore.
364 ScRefreshTimerProtector aProt( GetRefreshTimerControlAddress() );
365 delete pRefreshTimerControl, pRefreshTimerControl = NULL;
368 // Links aufrauemen
370 if ( GetLinkManager() )
372 // BaseLinks freigeben
373 ::sfx2::SvLinkSources aTemp(pLinkManager->GetServers());
374 for( ::sfx2::SvLinkSources::const_iterator it = aTemp.begin(); it != aTemp.end(); ++it )
375 (*it)->Closed();
377 if ( pLinkManager->GetLinks().size() )
378 pLinkManager->Remove( 0, pLinkManager->GetLinks().size() );
381 mxFormulaParserPool.reset();
382 // Destroy the external ref mgr instance here because it has a timer
383 // which needs to be stopped before the app closes.
384 pExternalRefMgr.reset();
386 ScAddInAsync::RemoveDocument( this );
387 ScAddInListener::RemoveDocument( this );
388 DELETEZ( pChartListenerCollection); // vor pBASM wg. evtl. Listener!
389 DELETEZ( pLookupCacheMapImpl); // before pBASM because of listeners
390 // BroadcastAreas vor allen Zellen zerstoeren um unnoetige
391 // Einzel-EndListenings der Formelzellen zu vermeiden
392 delete pBASM; // BroadcastAreaSlotMachine
393 pBASM = NULL;
395 delete pUnoBroadcaster; // broadcasted nochmal SFX_HINT_DYING
396 pUnoBroadcaster = NULL;
398 delete pUnoRefUndoList;
399 delete pUnoListenerCalls;
401 Clear( true ); // true = from destructor (needed for SdrModel::ClearModel)
403 if (pValidationList)
405 for( ScValidationDataList::iterator it = pValidationList->begin(); it != pValidationList->end(); ++it )
406 delete *it;
407 pValidationList->clear();
408 DELETEZ(pValidationList);
410 delete pRangeName;
411 delete pDBCollection;
412 delete pSelectionAttr;
413 apTemporaryChartLock.reset();
414 delete pChartCollection;
415 DeleteDrawLayer();
416 delete pFormatExchangeList;
417 delete pPrinter;
418 ImplDeleteOptions();
419 delete pConsolidateDlgData;
420 delete pLinkManager;
421 delete pClipData;
422 delete pDetOpList; // loescht auch die Eintraege
423 delete pChangeTrack;
424 delete pEditEngine;
425 delete pNoteEngine;
426 delete pChangeViewSettings; // und weg damit
427 delete pVirtualDevice_100th_mm;
429 delete pDPCollection;
431 // delete the EditEngine before destroying the xPoolHelper
432 delete pCacheFieldEditEngine;
434 if ( xPoolHelper.is() && !bIsClip )
435 xPoolHelper->SourceDocumentGone();
436 xPoolHelper.clear();
438 delete pScriptTypeData;
439 delete pRecursionHelper;
441 OSL_POSTCOND( !pAutoNameCache, "AutoNameCache still set in dtor" );
444 void ScDocument::InitClipPtrs( ScDocument* pSourceDoc )
446 OSL_ENSURE(bIsClip, "InitClipPtrs and not bIsClip");
448 if (pValidationList)
450 for(ScValidationDataList::iterator it = pValidationList->begin(); it != pValidationList->end(); ++it )
451 delete *it;
452 pValidationList->clear();
453 DELETEZ(pValidationList);
456 Clear();
458 xPoolHelper = pSourceDoc->xPoolHelper;
460 // bedingte Formate / Gueltigkeiten
461 //! Vorlagen kopieren?
462 const ScValidationDataList* pSourceValid = pSourceDoc->pValidationList;
463 if ( pSourceValid )
464 pValidationList = new ScValidationDataList(this, *pSourceValid);
466 // Links in Stream speichern
467 delete pClipData;
468 if (pSourceDoc->HasDdeLinks())
470 pClipData = new SvMemoryStream;
471 pSourceDoc->SaveDdeLinks(*pClipData);
473 else
474 pClipData = NULL;
476 // Options pointers exist (ImplCreateOptions) for any document.
477 // Must be copied for correct results in OLE objects (#i42666#).
478 SetDocOptions( pSourceDoc->GetDocOptions() );
479 SetViewOptions( pSourceDoc->GetViewOptions() );
482 SvNumberFormatter* ScDocument::GetFormatTable() const
484 return xPoolHelper->GetFormTable();
487 SfxItemPool* ScDocument::GetEditPool() const
489 return xPoolHelper->GetEditPool();
492 SfxItemPool* ScDocument::GetEnginePool() const
494 return xPoolHelper->GetEnginePool();
497 ScFieldEditEngine& ScDocument::GetEditEngine()
499 if ( !pEditEngine )
501 pEditEngine = new ScFieldEditEngine(this, GetEnginePool(), GetEditPool());
502 pEditEngine->SetUpdateMode( false );
503 pEditEngine->EnableUndo( false );
504 pEditEngine->SetRefMapMode( MAP_100TH_MM );
505 ApplyAsianEditSettings( *pEditEngine );
507 return *pEditEngine;
510 ScNoteEditEngine& ScDocument::GetNoteEngine()
512 if ( !pNoteEngine )
514 pNoteEngine = new ScNoteEditEngine( GetEnginePool(), GetEditPool() );
515 pNoteEngine->SetUpdateMode( false );
516 pNoteEngine->EnableUndo( false );
517 pNoteEngine->SetRefMapMode( MAP_100TH_MM );
518 ApplyAsianEditSettings( *pNoteEngine );
519 const SfxItemSet& rItemSet = GetDefPattern()->GetItemSet();
520 SfxItemSet* pEEItemSet = new SfxItemSet( pNoteEngine->GetEmptyItemSet() );
521 ScPatternAttr::FillToEditItemSet( *pEEItemSet, rItemSet );
522 pNoteEngine->SetDefaults( pEEItemSet ); // edit engine takes ownership
524 return *pNoteEngine;
528 void ScDocument::ResetClip( ScDocument* pSourceDoc, const ScMarkData* pMarks )
530 if (bIsClip)
532 InitClipPtrs(pSourceDoc);
534 for (SCTAB i = 0; i < static_cast<SCTAB>(pSourceDoc->maTabs.size()); i++)
535 if (pSourceDoc->maTabs[i])
536 if (!pMarks || pMarks->GetTableSelect(i))
538 OUString aString;
539 pSourceDoc->maTabs[i]->GetName(aString);
540 if ( i < static_cast<SCTAB>(maTabs.size()) )
542 maTabs[i] = new ScTable(this, i, aString);
545 else
547 if( i > static_cast<SCTAB>(maTabs.size()) )
549 maTabs.resize(i, NULL );
551 maTabs.push_back(new ScTable(this, i, aString));
553 maTabs[i]->SetLayoutRTL( pSourceDoc->maTabs[i]->IsLayoutRTL() );
556 else
558 OSL_FAIL("ResetClip");
562 void ScDocument::ResetClip( ScDocument* pSourceDoc, SCTAB nTab )
564 if (bIsClip)
566 InitClipPtrs(pSourceDoc);
567 if (nTab >= static_cast<SCTAB>(maTabs.size()))
569 maTabs.resize(nTab+1, NULL );
571 maTabs[nTab] = new ScTable(this, nTab,
572 OUString("baeh"));
573 if (nTab < static_cast<SCTAB>(pSourceDoc->maTabs.size()) && pSourceDoc->maTabs[nTab])
574 maTabs[nTab]->SetLayoutRTL( pSourceDoc->maTabs[nTab]->IsLayoutRTL() );
576 else
578 OSL_FAIL("ResetClip");
582 void ScDocument::EnsureTable( SCTAB nTab )
584 bool bExtras = !bIsUndo; // Spaltenbreiten, Zeilenhoehen, Flags
585 if (static_cast<size_t>(nTab) >= maTabs.size())
586 maTabs.resize(nTab+1, NULL);
588 if (!maTabs[nTab])
589 maTabs[nTab] = new ScTable(this, nTab, "temp", bExtras, bExtras);
592 void ScDocument::PutCell( SCCOL nCol, SCROW nRow, SCTAB nTab,
593 ScBaseCell* pCell, sal_uLong nFormatIndex, bool bForceTab )
595 if (ValidTab(nTab))
597 if (bForceTab)
598 EnsureTable(nTab);
600 if ( nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab] )
601 maTabs[nTab]->PutCell( nCol, nRow, nFormatIndex, pCell );
605 ScRefCellValue ScDocument::GetRefCellValue( const ScAddress& rPos )
607 if (!TableExists(rPos.Tab()))
608 return ScRefCellValue(); // empty
610 return maTabs[rPos.Tab()]->GetRefCellValue(rPos.Col(), rPos.Row());
613 bool ScDocument::GetPrintArea( SCTAB nTab, SCCOL& rEndCol, SCROW& rEndRow,
614 bool bNotes ) const
616 if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab])
618 bool bAny = maTabs[nTab]->GetPrintArea( rEndCol, rEndRow, bNotes );
619 if (pDrawLayer)
621 ScRange aDrawRange(0,0,nTab, MAXCOL,MAXROW,nTab);
622 if (DrawGetPrintArea( aDrawRange, true, true ))
624 if (aDrawRange.aEnd.Col()>rEndCol) rEndCol=aDrawRange.aEnd.Col();
625 if (aDrawRange.aEnd.Row()>rEndRow) rEndRow=aDrawRange.aEnd.Row();
626 bAny = true;
629 return bAny;
632 rEndCol = 0;
633 rEndRow = 0;
634 return false;
637 bool ScDocument::GetPrintAreaHor( SCTAB nTab, SCROW nStartRow, SCROW nEndRow,
638 SCCOL& rEndCol, bool bNotes ) const
640 if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab])
642 bool bAny = maTabs[nTab]->GetPrintAreaHor( nStartRow, nEndRow, rEndCol, bNotes );
643 if (pDrawLayer)
645 ScRange aDrawRange(0,nStartRow,nTab, MAXCOL,nEndRow,nTab);
646 if (DrawGetPrintArea( aDrawRange, true, false ))
648 if (aDrawRange.aEnd.Col()>rEndCol) rEndCol=aDrawRange.aEnd.Col();
649 bAny = true;
652 return bAny;
655 rEndCol = 0;
656 return false;
659 bool ScDocument::GetPrintAreaVer( SCTAB nTab, SCCOL nStartCol, SCCOL nEndCol,
660 SCROW& rEndRow, bool bNotes ) const
662 if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab])
664 bool bAny = maTabs[nTab]->GetPrintAreaVer( nStartCol, nEndCol, rEndRow, bNotes );
665 if (pDrawLayer)
667 ScRange aDrawRange(nStartCol,0,nTab, nEndCol,MAXROW,nTab);
668 if (DrawGetPrintArea( aDrawRange, false, true ))
670 if (aDrawRange.aEnd.Row()>rEndRow) rEndRow=aDrawRange.aEnd.Row();
671 bAny = true;
674 return bAny;
677 rEndRow = 0;
678 return false;
681 bool ScDocument::GetDataStart( SCTAB nTab, SCCOL& rStartCol, SCROW& rStartRow ) const
683 if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab])
685 bool bAny = maTabs[nTab]->GetDataStart( rStartCol, rStartRow );
686 if (pDrawLayer)
688 ScRange aDrawRange(0,0,nTab, MAXCOL,MAXROW,nTab);
689 if (DrawGetPrintArea( aDrawRange, true, true ))
691 if (aDrawRange.aStart.Col()<rStartCol) rStartCol=aDrawRange.aStart.Col();
692 if (aDrawRange.aStart.Row()<rStartRow) rStartRow=aDrawRange.aStart.Row();
693 bAny = true;
696 return bAny;
699 rStartCol = 0;
700 rStartRow = 0;
701 return false;
704 bool ScDocument::MoveTab( SCTAB nOldPos, SCTAB nNewPos, ScProgress* pProgress )
706 if (nOldPos == nNewPos)
707 return false;
709 SCTAB nTabCount = static_cast<SCTAB>(maTabs.size());
710 if(nTabCount < 2)
711 return false;
713 bool bValid = false;
714 if (ValidTab(nOldPos) && nOldPos < nTabCount )
716 if (maTabs[nOldPos])
718 bool bOldAutoCalc = GetAutoCalc();
719 SetAutoCalc( false ); // Mehrfachberechnungen vermeiden
720 SetNoListening( true );
721 if (nNewPos == SC_TAB_APPEND || nNewPos >= nTabCount)
722 nNewPos = nTabCount-1;
724 // Referenz-Updaterei
725 //! mit UpdateReference zusammenfassen!
727 SCsTAB nDz = ((SCsTAB)nNewPos) - (SCsTAB)nOldPos;
728 ScRange aSourceRange( 0,0,nOldPos, MAXCOL,MAXROW,nOldPos );
729 if (pRangeName)
730 pRangeName->UpdateTabRef(nOldPos, 3, nNewPos);
731 pDBCollection->UpdateMoveTab( nOldPos, nNewPos );
732 xColNameRanges->UpdateReference( URM_REORDER, this, aSourceRange, 0,0,nDz );
733 xRowNameRanges->UpdateReference( URM_REORDER, this, aSourceRange, 0,0,nDz );
734 if (pDPCollection)
735 pDPCollection->UpdateReference( URM_REORDER, aSourceRange, 0,0,nDz );
736 if (pDetOpList)
737 pDetOpList->UpdateReference( this, URM_REORDER, aSourceRange, 0,0,nDz );
738 UpdateChartRef( URM_REORDER,
739 0,0,nOldPos, MAXCOL,MAXROW,nOldPos, 0,0,nDz );
740 UpdateRefAreaLinks( URM_REORDER, aSourceRange, 0,0,nDz );
741 if ( pValidationList )
742 pValidationList->UpdateMoveTab( nOldPos, nNewPos );
743 if ( pUnoBroadcaster )
744 pUnoBroadcaster->Broadcast( ScUpdateRefHint( URM_REORDER,
745 aSourceRange, 0,0,nDz ) );
747 ScTable* pSaveTab = maTabs[nOldPos];
748 maTabs.erase(maTabs.begin()+nOldPos);
749 maTabs.insert(maTabs.begin()+nNewPos, pSaveTab);
750 TableContainer::iterator it = maTabs.begin();
751 for (SCTAB i = 0; i < nTabCount; i++)
752 if (maTabs[i])
753 maTabs[i]->UpdateMoveTab( nOldPos, nNewPos, i, pProgress );
754 it = maTabs.begin();
755 for (; it != maTabs.end(); ++it)
756 if (*it)
757 (*it)->UpdateCompile();
758 SetNoListening( false );
759 it = maTabs.begin();
760 for (; it != maTabs.end(); ++it)
761 if (*it)
762 (*it)->StartAllListeners();
763 // sheet names of references may not be valid until sheet is moved
764 pChartListenerCollection->UpdateScheduledSeriesRanges();
765 SetDirty();
766 SetAutoCalc( bOldAutoCalc );
768 if (pDrawLayer)
769 DrawMovePage( static_cast<sal_uInt16>(nOldPos), static_cast<sal_uInt16>(nNewPos) );
771 bValid = true;
774 return bValid;
777 bool ScDocument::CopyTab( SCTAB nOldPos, SCTAB nNewPos, const ScMarkData* pOnlyMarked )
779 if (SC_TAB_APPEND == nNewPos || nNewPos >= static_cast<SCTAB>(maTabs.size()))
780 nNewPos = static_cast<SCTAB>(maTabs.size());
781 OUString aName;
782 GetName(nOldPos, aName);
784 // vorneweg testen, ob der Prefix als gueltig erkannt wird
785 // wenn nicht, nur doppelte vermeiden
786 bool bPrefix = ValidTabName( aName );
787 OSL_ENSURE(bPrefix, "invalid table name");
788 SCTAB nDummy;
790 CreateValidTabName(aName);
792 bool bValid;
793 if (bPrefix)
794 bValid = ValidNewTabName(aName);
795 else
796 bValid = !GetTable( aName, nDummy );
798 bool bOldAutoCalc = GetAutoCalc();
799 SetAutoCalc( false ); // Mehrfachberechnungen vermeiden
800 if (bValid)
802 if (nNewPos >= static_cast<SCTAB>(maTabs.size()))
804 nNewPos = static_cast<SCTAB>(maTabs.size());
805 maTabs.push_back(new ScTable(this, nNewPos, aName));
807 else
809 if (ValidTab(nNewPos) && (nNewPos < static_cast<SCTAB>(maTabs.size())))
811 SetNoListening( true );
813 ScRange aRange( 0,0,nNewPos, MAXCOL,MAXROW,MAXTAB );
814 xColNameRanges->UpdateReference( URM_INSDEL, this, aRange, 0,0,1 );
815 xRowNameRanges->UpdateReference( URM_INSDEL, this, aRange, 0,0,1 );
816 if (pRangeName)
817 pRangeName->UpdateTabRef(nNewPos, 1);
818 pDBCollection->UpdateReference(
819 URM_INSDEL, 0,0,nNewPos, MAXCOL,MAXROW,MAXTAB, 0,0,1 );
820 if (pDPCollection)
821 pDPCollection->UpdateReference( URM_INSDEL, aRange, 0,0,1 );
822 if (pDetOpList)
823 pDetOpList->UpdateReference( this, URM_INSDEL, aRange, 0,0,1 );
824 UpdateChartRef( URM_INSDEL, 0,0,nNewPos, MAXCOL,MAXROW,MAXTAB, 0,0,1 );
825 UpdateRefAreaLinks( URM_INSDEL, aRange, 0,0,1 );
826 if ( pUnoBroadcaster )
827 pUnoBroadcaster->Broadcast( ScUpdateRefHint( URM_INSDEL, aRange, 0,0,1 ) );
829 SCTAB i;
830 for (TableContainer::iterator it = maTabs.begin(); it != maTabs.end(); ++it)
831 if (*it && it != (maTabs.begin() + nOldPos))
832 (*it)->UpdateInsertTab(nNewPos);
833 maTabs.push_back(NULL);
834 for (i = static_cast<SCTAB>(maTabs.size())-1; i > nNewPos; i--)
835 maTabs[i] = maTabs[i - 1];
836 if (nNewPos <= nOldPos)
837 nOldPos++;
838 maTabs[nNewPos] = new ScTable(this, nNewPos, aName);
839 bValid = true;
840 for (TableContainer::iterator it = maTabs.begin(); it != maTabs.end(); ++it)
841 if (*it && it != maTabs.begin()+nOldPos && it != maTabs.begin() + nNewPos)
842 (*it)->UpdateCompile();
843 SetNoListening( false );
844 for (TableContainer::iterator it = maTabs.begin(); it != maTabs.end(); ++it)
845 if (*it && it != maTabs.begin()+nOldPos && it != maTabs.begin()+nNewPos)
846 (*it)->StartAllListeners();
848 if ( pValidationList )
849 pValidationList->UpdateReference( URM_INSDEL, aRange, 0,0,1 );
850 // sheet names of references may not be valid until sheet is copied
851 pChartListenerCollection->UpdateScheduledSeriesRanges();
853 else
854 bValid = false;
857 if (bValid)
859 sc::CopyToDocContext aCxt(*this);
860 SetNoListening( true ); // noch nicht bei CopyToTable/Insert
861 maTabs[nOldPos]->CopyToTable(aCxt, 0, 0, MAXCOL, MAXROW, IDF_ALL, (pOnlyMarked != NULL),
862 maTabs[nNewPos], pOnlyMarked );
863 maTabs[nNewPos]->SetTabBgColor(maTabs[nOldPos]->GetTabBgColor());
865 SCsTAB nDz = (static_cast<SCsTAB>(nNewPos)) - static_cast<SCsTAB>(nOldPos);
866 maTabs[nNewPos]->UpdateReference(URM_COPY, 0, 0, nNewPos , MAXCOL, MAXROW,
867 nNewPos, 0, 0, nDz, NULL);
869 maTabs[nNewPos]->UpdateInsertTabAbs(nNewPos); // alle abs. um eins hoch!!
870 maTabs[nOldPos]->UpdateInsertTab(nNewPos);
872 maTabs[nOldPos]->UpdateCompile();
873 maTabs[nNewPos]->UpdateCompile( true ); // maybe already compiled in Clone, but used names need recompilation
874 SetNoListening( false );
875 maTabs[nOldPos]->StartAllListeners();
876 maTabs[nNewPos]->StartAllListeners();
878 ScConditionalFormatList* pNewList = new ScConditionalFormatList(*maTabs[nOldPos]->GetCondFormList());
879 pNewList->UpdateReference(URM_COPY, ScRange( 0, 0, nNewPos , MAXCOL, MAXROW,
880 nNewPos), 0, 0, nDz);
881 maTabs[nNewPos]->SetCondFormList( pNewList );
883 SetDirty();
884 SetAutoCalc( bOldAutoCalc );
886 if (pDrawLayer)
887 DrawCopyPage( static_cast<sal_uInt16>(nOldPos), static_cast<sal_uInt16>(nNewPos) );
889 if (pDPCollection)
890 pDPCollection->CopyToTab(nOldPos, nNewPos);
892 maTabs[nNewPos]->SetPageStyle( maTabs[nOldPos]->GetPageStyle() );
893 maTabs[nNewPos]->SetPendingRowHeights( maTabs[nOldPos]->IsPendingRowHeights() );
895 // Copy the custom print range if exists.
896 maTabs[nNewPos]->CopyPrintRange(*maTabs[nOldPos]);
898 // Copy the RTL settings
899 maTabs[nNewPos]->SetLayoutRTL(maTabs[nOldPos]->IsLayoutRTL());
900 maTabs[nNewPos]->SetLoadingRTL(maTabs[nOldPos]->IsLoadingRTL());
902 else
903 SetAutoCalc( bOldAutoCalc );
904 return bValid;
907 void VBA_InsertModule( ScDocument& rDoc, SCTAB nTab, const OUString& sModuleName, const OUString& sModuleSource );
909 sal_uLong ScDocument::TransferTab( ScDocument* pSrcDoc, SCTAB nSrcPos,
910 SCTAB nDestPos, bool bInsertNew,
911 bool bResultsOnly )
913 sal_uLong nRetVal = 1; // 0 => Fehler 1 = ok
914 // 3 => NameBox
915 // 4 => beides
917 if (pSrcDoc->pShell->GetMedium())
919 pSrcDoc->maFileURL = pSrcDoc->pShell->GetMedium()->GetURLObject().GetMainURL(INetURLObject::DECODE_TO_IURI);
920 // for unsaved files use the title name and adjust during save of file
921 if (pSrcDoc->maFileURL.isEmpty())
922 pSrcDoc->maFileURL = pSrcDoc->pShell->GetName();
924 else
926 pSrcDoc->maFileURL = pSrcDoc->pShell->GetName();
929 bool bValid = true;
930 if (bInsertNew) // neu einfuegen
932 OUString aName;
933 pSrcDoc->GetName(nSrcPos, aName);
934 CreateValidTabName(aName);
935 bValid = InsertTab(nDestPos, aName);
937 // Copy the RTL settings
938 maTabs[nDestPos]->SetLayoutRTL(pSrcDoc->maTabs[nSrcPos]->IsLayoutRTL());
939 maTabs[nDestPos]->SetLoadingRTL(pSrcDoc->maTabs[nSrcPos]->IsLoadingRTL());
941 else // bestehende Tabelle ersetzen
943 if (ValidTab(nDestPos) && nDestPos < static_cast<SCTAB>(maTabs.size()) && maTabs[nDestPos])
945 maTabs[nDestPos]->DeleteArea( 0,0, MAXCOL,MAXROW, IDF_ALL );
947 else
948 bValid = false;
951 if (bValid)
953 bool bOldAutoCalcSrc = false;
954 bool bOldAutoCalc = GetAutoCalc();
955 SetAutoCalc( false ); // Mehrfachberechnungen vermeiden
956 SetNoListening( true );
957 if ( bResultsOnly )
959 bOldAutoCalcSrc = pSrcDoc->GetAutoCalc();
960 pSrcDoc->SetAutoCalc( true ); // falls was berechnet werden muss
964 NumFmtMergeHandler aNumFmtMergeHdl(this, pSrcDoc);
966 sc::CopyToDocContext aCxt(*this);
967 nDestPos = std::min(nDestPos, (SCTAB)(GetTableCount() - 1));
968 { // scope for bulk broadcast
969 ScBulkBroadcast aBulkBroadcast( pBASM);
970 pSrcDoc->maTabs[nSrcPos]->CopyToTable(aCxt, 0, 0, MAXCOL, MAXROW,
971 ( bResultsOnly ? IDF_ALL & ~IDF_FORMULA : IDF_ALL),
972 false, maTabs[nDestPos] );
973 maTabs[nDestPos]->CopyConditionalFormat(0, 0, MAXCOL, MAXROW,
974 0, 0, pSrcDoc->maTabs[nSrcPos]);
977 maTabs[nDestPos]->SetTabNo(nDestPos);
978 maTabs[nDestPos]->SetTabBgColor(pSrcDoc->maTabs[nSrcPos]->GetTabBgColor());
980 if ( !bResultsOnly )
983 SCsTAB nDz = ((SCsTAB)nDestPos) - (SCsTAB)nSrcPos;
984 maTabs[nDestPos]->UpdateReference(URM_COPY, 0, 0, nDestPos,
985 MAXCOL, MAXROW, nDestPos,
986 0, 0, nDz, NULL);
987 // Readjust self-contained absolute references to this sheet
988 maTabs[nDestPos]->TestTabRefAbs(nSrcPos);
989 maTabs[nDestPos]->CompileAll();
992 SetNoListening( false );
993 if ( !bResultsOnly )
994 maTabs[nDestPos]->StartAllListeners();
995 SetDirty( ScRange( 0, 0, nDestPos, MAXCOL, MAXROW, nDestPos));
997 if ( bResultsOnly )
998 pSrcDoc->SetAutoCalc( bOldAutoCalcSrc );
999 SetAutoCalc( bOldAutoCalc );
1001 // Drawing kopieren
1003 if (bInsertNew)
1004 TransferDrawPage( pSrcDoc, nSrcPos, nDestPos );
1006 maTabs[nDestPos]->SetPendingRowHeights( pSrcDoc->maTabs[nSrcPos]->IsPendingRowHeights() );
1008 if (!bValid)
1009 nRetVal = 0;
1010 bool bVbaEnabled = IsInVBAMode();
1012 if ( bVbaEnabled )
1014 SfxObjectShell* pSrcShell = pSrcDoc ? pSrcDoc->GetDocumentShell() : NULL;
1015 if ( pSrcShell )
1017 OUString aLibName("Standard");
1018 const BasicManager *pBasicManager = pSrcShell->GetBasicManager();
1019 if (pBasicManager && !pBasicManager->GetName().isEmpty())
1021 aLibName = pSrcShell->GetBasicManager()->GetName();
1023 OUString sCodeName;
1024 OUString sSource;
1025 uno::Reference< script::XLibraryContainer > xLibContainer = pSrcShell->GetBasicContainer();
1026 uno::Reference< container::XNameContainer > xLib;
1027 if( xLibContainer.is() )
1029 uno::Any aLibAny = xLibContainer->getByName(aLibName);
1030 aLibAny >>= xLib;
1033 if( xLib.is() )
1035 OUString sSrcCodeName;
1036 pSrcDoc->GetCodeName( nSrcPos, sSrcCodeName );
1037 OUString sRTLSource;
1038 xLib->getByName( sSrcCodeName ) >>= sRTLSource;
1039 sSource = sRTLSource;
1041 VBA_InsertModule( *this, nDestPos, sCodeName, sSource );
1045 return nRetVal;
1048 void ScDocument::SetError( SCCOL nCol, SCROW nRow, SCTAB nTab, const sal_uInt16 nError)
1050 if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()))
1051 if (maTabs[nTab])
1052 maTabs[nTab]->SetError( nCol, nRow, nError );
1055 void ScDocument::SetFormula(
1056 const ScAddress& rPos, const ScTokenArray& rArray, formula::FormulaGrammar::Grammar eGram )
1058 if (!TableExists(rPos.Tab()))
1059 return;
1061 maTabs[rPos.Tab()]->SetFormula(rPos.Col(), rPos.Row(), rArray, eGram);
1064 void ScDocument::SetFormula(
1065 const ScAddress& rPos, const OUString& rFormula, formula::FormulaGrammar::Grammar eGram )
1067 if (!TableExists(rPos.Tab()))
1068 return;
1070 maTabs[rPos.Tab()]->SetFormula(rPos.Col(), rPos.Row(), rFormula, eGram);
1073 void ScDocument::SetFormulaCell( const ScAddress& rPos, ScFormulaCell* pCell )
1075 if (!TableExists(rPos.Tab()))
1077 pCell->Delete();
1078 return;
1081 maTabs[rPos.Tab()]->SetFormulaCell(rPos.Col(), rPos.Row(), pCell);
1084 void ScDocument::SetConsolidateDlgData( const ScConsolidateParam* pData )
1086 delete pConsolidateDlgData;
1088 if ( pData )
1089 pConsolidateDlgData = new ScConsolidateParam( *pData );
1090 else
1091 pConsolidateDlgData = NULL;
1094 void ScDocument::SetChangeViewSettings(const ScChangeViewSettings& rNew)
1096 if (pChangeViewSettings==NULL)
1097 pChangeViewSettings = new ScChangeViewSettings;
1099 OSL_ENSURE( pChangeViewSettings, "Oops. No ChangeViewSettings :-( by!" );
1101 *pChangeViewSettings=rNew;
1104 // ----------------------------------------------------------------------------
1106 ScFieldEditEngine* ScDocument::CreateFieldEditEngine()
1108 ScFieldEditEngine* pNewEditEngine = NULL;
1109 if (!pCacheFieldEditEngine)
1111 pNewEditEngine = new ScFieldEditEngine(
1112 this, GetEnginePool(), GetEditPool(), false);
1114 else
1116 if ( !bImportingXML )
1118 // #i66209# previous use might not have restored update mode,
1119 // ensure same state as for a new EditEngine (UpdateMode = true)
1120 if ( !pCacheFieldEditEngine->GetUpdateMode() )
1121 pCacheFieldEditEngine->SetUpdateMode(true);
1124 pNewEditEngine = pCacheFieldEditEngine;
1125 pCacheFieldEditEngine = NULL;
1127 return pNewEditEngine;
1130 void ScDocument::DisposeFieldEditEngine(ScFieldEditEngine*& rpEditEngine)
1132 if (!pCacheFieldEditEngine && rpEditEngine)
1134 pCacheFieldEditEngine = rpEditEngine;
1135 pCacheFieldEditEngine->Clear();
1137 else
1138 delete rpEditEngine;
1139 rpEditEngine = NULL;
1142 // ----------------------------------------------------------------------------
1144 ScRecursionHelper* ScDocument::CreateRecursionHelperInstance()
1146 return new ScRecursionHelper;
1149 // ----------------------------------------------------------------------------
1151 ScLookupCache & ScDocument::GetLookupCache( const ScRange & rRange )
1153 ScLookupCache* pCache = 0;
1154 if (!pLookupCacheMapImpl)
1155 pLookupCacheMapImpl = new ScLookupCacheMapImpl;
1156 ScLookupCacheMap::iterator it( pLookupCacheMapImpl->aCacheMap.find( rRange));
1157 if (it == pLookupCacheMapImpl->aCacheMap.end())
1159 pCache = new ScLookupCache( this, rRange);
1160 AddLookupCache( *pCache);
1162 else
1163 pCache = (*it).second;
1164 return *pCache;
1167 void ScDocument::AddLookupCache( ScLookupCache & rCache )
1169 if (!pLookupCacheMapImpl->aCacheMap.insert( ::std::pair< const ScRange,
1170 ScLookupCache*>( rCache.getRange(), &rCache)).second)
1172 OSL_FAIL( "ScDocument::AddLookupCache: couldn't add to hash map");
1174 else
1175 StartListeningArea( rCache.getRange(), &rCache);
1178 void ScDocument::RemoveLookupCache( ScLookupCache & rCache )
1180 ScLookupCacheMap::iterator it( pLookupCacheMapImpl->aCacheMap.find(
1181 rCache.getRange()));
1182 if (it == pLookupCacheMapImpl->aCacheMap.end())
1184 OSL_FAIL( "ScDocument::RemoveLookupCache: range not found in hash map");
1186 else
1188 ScLookupCache* pCache = (*it).second;
1189 pLookupCacheMapImpl->aCacheMap.erase( it);
1190 EndListeningArea( pCache->getRange(), &rCache);
1194 void ScDocument::ClearLookupCaches()
1196 if( pLookupCacheMapImpl )
1197 pLookupCacheMapImpl->clear();
1200 void ScDocument::SetPreviewFont( SfxItemSet* pFont )
1202 delete pPreviewFont;
1203 pPreviewFont = pFont;
1206 const ScMarkData& ScDocument::GetPreviewSelection()
1208 if ( !pPreviewSelection )
1209 pPreviewSelection = new ScMarkData();
1211 return *pPreviewSelection;
1214 void ScDocument::SetPreviewSelection( ScMarkData& rSel )
1216 // yeuch, why do I have a pointer here ???? ( other problems
1217 // to fix right now though )
1218 if ( !pPreviewSelection )
1219 pPreviewSelection = new ScMarkData();
1220 *pPreviewSelection = rSel;
1223 SfxItemSet* ScDocument::GetPreviewFont( SCCOL nCol, SCROW nRow, SCTAB nTab )
1225 SfxItemSet* pRet = NULL;
1226 if ( pPreviewFont )
1228 if ( GetPreviewSelection().IsCellMarked( nCol, nRow ) && GetPreviewSelection().GetFirstSelected() == nTab )
1229 pRet = pPreviewFont;
1231 return pRet;
1234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */