1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: documen2.cxx,v $
10 * $Revision: 1.75.18.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 // INCLUDE ---------------------------------------------------------------
36 #define _ZFORLIST_DECLARE_TABLE
37 #include "scitems.hxx"
38 #include <svx/eeitem.hxx>
40 #include <svx/editeng.hxx>
41 #include <svx/forbiddencharacterstable.hxx>
42 #include <svx/linkmgr.hxx>
43 #include <svx/svdpool.hxx>
44 #include <svx/svdobj.hxx>
45 #include <sfx2/bindings.hxx>
46 #include <sfx2/objsh.hxx>
47 #include <sfx2/printer.hxx>
48 #include <svtools/zforlist.hxx>
49 #include <svtools/zformat.hxx>
50 #include <vcl/virdev.hxx>
51 #include <comphelper/processfactory.hxx>
52 #include <svtools/PasswordHelper.hxx>
53 #include <tools/tenccvt.hxx>
54 #include <tools/list.hxx>
57 #include "document.hxx"
60 #include "patattr.hxx"
61 #include "rangenam.hxx"
62 #include "dbcolect.hxx"
64 #include "docpool.hxx"
65 #include "stlpool.hxx"
66 #include "stlsheet.hxx"
67 #include "globstr.hrc"
68 #include "chartarr.hxx"
69 #include "chartlock.hxx"
70 #include "rechead.hxx"
73 #include "bcaslot.hxx"
74 #include "adiasync.hxx"
75 #include "addinlis.hxx"
76 #include "chartlis.hxx"
77 #include "markdata.hxx"
78 #include "conditio.hxx"
79 #include "validat.hxx"
80 #include "progress.hxx"
81 #include "detdata.hxx"
82 #include "sc.hrc" // FID_DATACHANGED
83 #include "ddelink.hxx"
84 #include "chgtrack.hxx"
85 #include "chgviset.hxx"
86 #include "editutil.hxx"
88 #include "dpobject.hxx"
89 #include "indexmap.hxx"
90 #include "scrdata.hxx"
91 #include "poolhelp.hxx"
92 #include "unoreflist.hxx"
93 #include "listenercalls.hxx"
94 #include "recursionhelper.hxx"
95 #include "lookupcache.hxx"
96 #include "externalrefmgr.hxx"
97 #include "tabprotection.hxx"
98 #include "clipparam.hxx"
99 #include "macromgr.hxx"
100 #include <com/sun/star/document/XVbaEventsHelper.hpp>
101 #include "formulaparserpool.hxx"
103 // pImpl because including lookupcache.hxx in document.hxx isn't wanted, and
104 // dtor plus helpers are convenient.
105 struct ScLookupCacheMapImpl
107 ScLookupCacheMap aCacheMap
;
108 ~ScLookupCacheMapImpl()
116 ScLookupCacheMap aTmp
;
117 aCacheMap
.swap( aTmp
);
122 for (ScLookupCacheMap::iterator
it( aCacheMap
.begin()); it
!= aCacheMap
.end(); ++it
)
127 // STATIC DATA -----------------------------------------------------------
129 ScDocument::ScDocument( ScDocumentMode eMode
,
130 SfxObjectShell
* pDocShell
) :
131 xServiceManager( ::comphelper::getProcessServiceFactory() ),
132 mpUndoManager( NULL
),
135 pNoteItemPool( NULL
),
138 pVirtualDevice_100th_mm( NULL
),
141 pCondFormList( NULL
),
142 pValidationList( NULL
),
143 pFormatExchangeList( NULL
),
144 pDPCollection( NULL
),
145 pLinkManager( NULL
),
146 pFormulaTree( NULL
),
147 pEOFormulaTree( NULL
),
148 pFormulaTrack( NULL
),
149 pEOFormulaTrack( NULL
),
150 pOtherObjects( NULL
),
153 pChangeTrack( NULL
),
154 pUnoBroadcaster( NULL
),
155 pUnoListenerCalls( NULL
),
156 pUnoRefUndoList( NULL
),
157 pChangeViewSettings( NULL
),
158 pScriptTypeData( NULL
),
159 pCacheFieldEditEngine( NULL
),
160 pDocProtection( NULL
),
163 pViewOptions( NULL
),
165 pExtDocOptions( NULL
),
166 pConsolidateDlgData( NULL
),
167 pRecursionHelper( NULL
),
168 pAutoNameCache( NULL
),
169 pLookupCacheMapImpl( NULL
),
171 nRangeOverflowType( 0 ),
172 aCurTextWidthCalcPos(MAXCOL
,0,0),
173 nFormulaCodeInTree(0),
174 nXMLImportedFormulaCount( 0 ),
176 nMacroInterpretLevel(0),
177 nInterpreterTableOpLevel(0),
178 nMaxTableNumber( 0 ),
179 nSrcVer( SC_CURRENT_VERSION
),
180 nSrcMaxRow( MAXROW
),
181 nFormulaTrackCount(0),
184 eLinkMode(LM_UNKNOWN
),
185 bAutoCalc( eMode
== SCDOCMODE_DOCUMENT
),
186 bAutoCalcShellDisabled( FALSE
),
187 bForcedFormulaPending( FALSE
),
188 bCalculatingFormulaTree( FALSE
),
189 bIsClip( eMode
== SCDOCMODE_CLIP
),
190 bIsUndo( eMode
== SCDOCMODE_UNDO
),
192 bIsEmbedded( FALSE
),
193 // bNoSetDirty( TRUE ),
194 bNoSetDirty( FALSE
),
195 bInsertingFromOtherDoc( FALSE
),
196 bLoadingMedium(false),
197 bImportingXML( FALSE
),
198 bXMLFromWrapper( FALSE
),
199 bCalcingAfterLoad( FALSE
),
200 bNoListening( FALSE
),
201 bLoadingDone( TRUE
),
202 bIdleDisabled( FALSE
),
203 bInLinkUpdate( FALSE
),
204 bChartListenerCollectionNeedsUpdate( FALSE
),
205 bHasForcedFormulas( FALSE
),
206 bInDtorClear( FALSE
),
207 bExpandRefs( FALSE
),
208 bDetectiveDirty( FALSE
),
209 nMacroCallMode( SC_MACROCALL_ALLOWED
),
210 bHasMacroFunc( FALSE
),
212 nAsianCompression(SC_ASIANCOMPRESSION_INVALID
),
213 nAsianKerning(SC_ASIANKERNING_INVALID
),
214 bSetDrawDefaults( FALSE
),
215 bPastingDrawFromOtherDoc( FALSE
),
216 nInDdeLinkUpdate( 0 ),
217 bInUnoBroadcast( FALSE
),
218 bInUnoListenerCall( FALSE
),
219 eGrammar( formula::FormulaGrammar::GRAM_NATIVE
),
220 bStyleSheetUsageInvalid( TRUE
),
221 mbUndoEnabled( true ),
222 mbAdjustHeightEnabled( true ),
223 mbExecuteLinkEnabled( true ),
224 mbChangeReadOnlyEnabled( false ),
225 mnNamedRangesLockCount( 0 )
227 SetStorageGrammar( formula::FormulaGrammar::GRAM_STORAGE_DEFAULT
);
229 eSrcSet
= gsl_getSystemTextEncoding();
231 if ( eMode
== SCDOCMODE_DOCUMENT
)
234 pLinkManager
= new SvxLinkManager( pDocShell
);
236 xPoolHelper
= new ScPoolHelper( this );
239 pBASM
= new ScBroadcastAreaSlotMachine( this );
240 pChartListenerCollection
= new ScChartListenerCollection( this );
241 pRefreshTimerControl
= new ScRefreshTimerControl
;
247 pChartListenerCollection
= NULL
;
248 pRefreshTimerControl
= NULL
;
251 for (SCTAB i
=1; i
<=MAXTAB
; i
++)
254 pRangeName
= new ScRangeName( 4, 4, FALSE
, this );
255 pDBCollection
= new ScDBCollection( 4, 4, FALSE
, this );
256 pSelectionAttr
= NULL
;
257 pChartCollection
= new ScChartCollection
;
258 apTemporaryChartLock
= std::auto_ptr
< ScTemporaryChartLock
>( new ScTemporaryChartLock(this) );
259 xColNameRanges
= new ScRangePairList
;
260 xRowNameRanges
= new ScRangePairList
;
262 // languages for a visible document are set by docshell later (from options)
263 SetLanguage( ScGlobal::eLnge
, ScGlobal::eLnge
, ScGlobal::eLnge
);
265 aTrackTimer
.SetTimeoutHdl( LINK( this, ScDocument
, TrackTimeHdl
) );
266 aTrackTimer
.SetTimeout( 100 );
270 void ScDocument::SetStorageGrammar( formula::FormulaGrammar::Grammar eGram
)
273 eGram
== formula::FormulaGrammar::GRAM_ODFF
||
274 eGram
== formula::FormulaGrammar::GRAM_PODF
,
275 "ScDocument::SetStorageGrammar: wrong storage grammar");
277 eStorageGrammar
= eGram
;
279 // FIXME: the XML import shouldn't strip brackets, the compiler should
280 // digest them instead, which could also speedup reference recognition
283 eXmlImportGrammar
= formula::FormulaGrammar::mergeToGrammar( eGram
,
284 formula::FormulaGrammar::CONV_OOO
);
288 void ScDocument::SetDocVisible( BOOL bSet
)
290 // called from view ctor - only for a visible document,
291 // each new sheet's RTL flag is initialized from the locale
296 sal_uInt32
ScDocument::GetDocumentID() const
298 const ScDocument
* pThis
= this;
299 sal_uInt32 nCrc
= rtl_crc32( 0, &pThis
, sizeof(ScDocument
*) );
300 // the this pointer only might not be sufficient
301 nCrc
= rtl_crc32( nCrc
, &pShell
, sizeof(SfxObjectShell
*) );
306 void ScDocument::StartChangeTracking()
309 pChangeTrack
= new ScChangeTrack( this );
312 void ScDocument::EndChangeTracking()
318 void ScDocument::SetChangeTrack( ScChangeTrack
* pTrack
)
320 DBG_ASSERT( pTrack
->GetDocument() == this, "SetChangeTrack: different documents" );
321 if ( !pTrack
|| pTrack
== pChangeTrack
|| pTrack
->GetDocument() != this )
324 pChangeTrack
= pTrack
;
328 IMPL_LINK( ScDocument
, TrackTimeHdl
, Timer
*, EMPTYARG
)
330 if ( ScDdeLink::IsInUpdate() ) // nicht verschachteln
332 aTrackTimer
.Start(); // spaeter nochmal versuchen
334 else if (pShell
) // ausfuehren
337 pShell
->Broadcast( SfxSimpleHint( FID_DATACHANGED
) );
338 ResetChanged( ScRange(0,0,0,MAXCOL
,MAXROW
,MAXTAB
) );
342 if (!pShell
->IsModified())
344 pShell
->SetModified( TRUE
);
345 SfxBindings
* pBindings
= GetViewBindings();
348 pBindings
->Invalidate( SID_SAVEDOC
);
349 pBindings
->Invalidate( SID_DOC_MODIFIED
);
357 void ScDocument::StartTrackTimer()
359 if (!aTrackTimer
.IsActive()) // nicht ewig aufschieben
363 ScDocument::~ScDocument()
365 DBG_ASSERT( !bInLinkUpdate
, "bInLinkUpdate in dtor" );
369 // first of all disable all refresh timers by deleting the control
370 if ( pRefreshTimerControl
)
371 { // To be sure there isn't anything running do it with a protector,
372 // this ensures also that nothing needs the control anymore.
373 ScRefreshTimerProtector
aProt( GetRefreshTimerControlAddress() );
374 delete pRefreshTimerControl
, pRefreshTimerControl
= NULL
;
381 // BaseLinks freigeben
382 for ( USHORT n
= pLinkManager
->GetServers().Count(); n
; )
383 pLinkManager
->GetServers()[ --n
]->Closed();
385 if ( pLinkManager
->GetLinks().Count() )
386 pLinkManager
->Remove( 0, pLinkManager
->GetLinks().Count() );
389 mxFormulaParserPool
.reset();
390 // Destroy the external ref mgr instance here because it has a timer
391 // which needs to be stopped before the app closes.
392 pExternalRefMgr
.reset();
394 ScAddInAsync::RemoveDocument( this );
395 ScAddInListener::RemoveDocument( this );
396 DELETEZ( pChartListenerCollection
); // vor pBASM wg. evtl. Listener!
397 DELETEZ( pLookupCacheMapImpl
); // before pBASM because of listeners
398 // BroadcastAreas vor allen Zellen zerstoeren um unnoetige
399 // Einzel-EndListenings der Formelzellen zu vermeiden
400 delete pBASM
; // BroadcastAreaSlotMachine
405 delete pUnoBroadcaster
; // broadcasted nochmal SFX_HINT_DYING
406 pUnoBroadcaster
= NULL
;
409 delete pUnoRefUndoList
;
410 delete pUnoListenerCalls
;
412 Clear( sal_True
); // TRUE = from destructor (needed for SdrModel::ClearModel)
416 pCondFormList
->DeleteAndDestroy( 0, pCondFormList
->Count() );
417 DELETEZ(pCondFormList
);
421 pValidationList
->DeleteAndDestroy( 0, pValidationList
->Count() );
422 DELETEZ(pValidationList
);
425 delete pDBCollection
;
426 delete pSelectionAttr
;
427 apTemporaryChartLock
.reset();
428 delete pChartCollection
;
430 delete pFormatExchangeList
;
433 delete pConsolidateDlgData
;
436 delete pDetOpList
; // loescht auch die Eintraege
440 SfxItemPool::Free(pNoteItemPool
);
441 delete pChangeViewSettings
; // und weg damit
442 delete pVirtualDevice_100th_mm
;
444 delete pDPCollection
;
446 // delete the EditEngine before destroying the xPoolHelper
447 delete pCacheFieldEditEngine
;
449 if ( xPoolHelper
.isValid() && !bIsClip
)
450 xPoolHelper
->SourceDocumentGone();
451 xPoolHelper
.unbind();
454 delete pScriptTypeData
;
455 delete pOtherObjects
;
456 delete pRecursionHelper
;
458 DBG_ASSERT( !pAutoNameCache
, "AutoNameCache still set in dtor" );
461 void ScDocument::InitClipPtrs( ScDocument
* pSourceDoc
)
463 DBG_ASSERT(bIsClip
, "InitClipPtrs und nicht bIsClip");
467 pCondFormList
->DeleteAndDestroy( 0, pCondFormList
->Count() );
468 DELETEZ(pCondFormList
);
472 pValidationList
->DeleteAndDestroy( 0, pValidationList
->Count() );
473 DELETEZ(pValidationList
);
478 xPoolHelper
= pSourceDoc
->xPoolHelper
;
480 // bedingte Formate / Gueltigkeiten
481 //! Vorlagen kopieren?
482 const ScConditionalFormatList
* pSourceCond
= pSourceDoc
->pCondFormList
;
484 pCondFormList
= new ScConditionalFormatList(this, *pSourceCond
);
485 const ScValidationDataList
* pSourceValid
= pSourceDoc
->pValidationList
;
487 pValidationList
= new ScValidationDataList(this, *pSourceValid
);
489 // Links in Stream speichern
491 if (pSourceDoc
->HasDdeLinks())
493 pClipData
= new SvMemoryStream
;
494 pSourceDoc
->SaveDdeLinks(*pClipData
);
499 // Options pointers exist (ImplCreateOptions) for any document.
500 // Must be copied for correct results in OLE objects (#i42666#).
501 SetDocOptions( pSourceDoc
->GetDocOptions() );
502 SetViewOptions( pSourceDoc
->GetViewOptions() );
505 SvNumberFormatter
* ScDocument::GetFormatTable() const
507 return xPoolHelper
->GetFormTable();
510 SfxItemPool
* ScDocument::GetEditPool() const
512 return xPoolHelper
->GetEditPool();
515 SfxItemPool
* ScDocument::GetEnginePool() const
517 return xPoolHelper
->GetEnginePool();
520 ScFieldEditEngine
& ScDocument::GetEditEngine()
524 pEditEngine
= new ScFieldEditEngine( GetEnginePool(), GetEditPool() );
525 pEditEngine
->SetUpdateMode( FALSE
);
526 pEditEngine
->EnableUndo( FALSE
);
527 pEditEngine
->SetRefMapMode( MAP_100TH_MM
);
528 pEditEngine
->SetForbiddenCharsTable( xForbiddenCharacters
);
533 ScNoteEditEngine
& ScDocument::GetNoteEngine()
537 pNoteEngine
= new ScNoteEditEngine( GetEnginePool(), GetEditPool() );
538 pNoteEngine
->SetUpdateMode( FALSE
);
539 pNoteEngine
->EnableUndo( FALSE
);
540 pNoteEngine
->SetRefMapMode( MAP_100TH_MM
);
541 pNoteEngine
->SetForbiddenCharsTable( xForbiddenCharacters
);
542 const SfxItemSet
& rItemSet
= GetDefPattern()->GetItemSet();
543 SfxItemSet
* pEEItemSet
= new SfxItemSet( pNoteEngine
->GetEmptyItemSet() );
544 ScPatternAttr::FillToEditItemSet( *pEEItemSet
, rItemSet
);
545 pNoteEngine
->SetDefaults( pEEItemSet
); // edit engine takes ownership
550 //UNUSED2009-05 SfxItemPool& ScDocument::GetNoteItemPool()
552 //UNUSED2009-05 if ( !pNoteItemPool )
553 //UNUSED2009-05 pNoteItemPool = new SfxItemPool(SdrObject::GetGlobalDrawObjectItemPool());
554 //UNUSED2009-05 return *pNoteItemPool;
557 void ScDocument::ResetClip( ScDocument
* pSourceDoc
, const ScMarkData
* pMarks
)
561 InitClipPtrs(pSourceDoc
);
563 for (SCTAB i
= 0; i
<= MAXTAB
; i
++)
564 if (pSourceDoc
->pTab
[i
])
565 if (!pMarks
|| pMarks
->GetTableSelect(i
))
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;
576 DBG_ERROR("ResetClip");
580 void ScDocument::ResetClip( ScDocument
* pSourceDoc
, SCTAB nTab
)
584 InitClipPtrs(pSourceDoc
);
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;
594 DBG_ERROR("ResetClip");
598 void ScDocument::DeleteNumberFormat( const sal_uInt32
* /* pDelKeys */, sal_uInt32
/* nCount */ )
601 for (ULONG i = 0; i < nCount; i++)
602 xPoolHelper->GetFormTable()->DeleteEntry(pDelKeys[i]);
606 void ScDocument::PutCell( SCCOL nCol
, SCROW nRow
, SCTAB nTab
,
607 ScBaseCell
* pCell
, ULONG nFormatIndex
, BOOL bForceTab
)
611 if ( bForceTab
&& !pTab
[nTab
] )
613 BOOL bExtras
= !bIsUndo
; // Spaltenbreiten, Zeilenhoehen, Flags
615 pTab
[nTab
] = new ScTable(this, nTab
,
616 String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM("temp")),
621 pTab
[nTab
]->PutCell( nCol
, nRow
, nFormatIndex
, pCell
);
625 //UNUSED2009-05 void ScDocument::PutCell( const ScAddress& rPos, ScBaseCell* pCell,
626 //UNUSED2009-05 ULONG nFormatIndex, BOOL bForceTab )
628 //UNUSED2009-05 SCTAB nTab = rPos.Tab();
629 //UNUSED2009-05 if ( bForceTab && !pTab[nTab] )
631 //UNUSED2009-05 BOOL bExtras = !bIsUndo; // Spaltenbreiten, Zeilenhoehen, Flags
633 //UNUSED2009-05 pTab[nTab] = new ScTable(this, nTab,
634 //UNUSED2009-05 String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM("temp")),
635 //UNUSED2009-05 bExtras, bExtras);
638 //UNUSED2009-05 if (pTab[nTab])
639 //UNUSED2009-05 pTab[nTab]->PutCell( rPos, nFormatIndex, pCell );
642 BOOL
ScDocument::GetPrintArea( SCTAB nTab
, SCCOL
& rEndCol
, SCROW
& rEndRow
,
645 if (ValidTab(nTab
) && pTab
[nTab
])
647 BOOL bAny
= pTab
[nTab
]->GetPrintArea( rEndCol
, rEndRow
, bNotes
);
650 ScRange
aDrawRange(0,0,nTab
, MAXCOL
,MAXROW
,nTab
);
651 if (DrawGetPrintArea( aDrawRange
, TRUE
, TRUE
))
653 if (aDrawRange
.aEnd
.Col()>rEndCol
) rEndCol
=aDrawRange
.aEnd
.Col();
654 if (aDrawRange
.aEnd
.Row()>rEndRow
) rEndRow
=aDrawRange
.aEnd
.Row();
666 BOOL
ScDocument::GetPrintAreaHor( SCTAB nTab
, SCROW nStartRow
, SCROW nEndRow
,
667 SCCOL
& rEndCol
, BOOL bNotes
) const
669 if (ValidTab(nTab
) && pTab
[nTab
])
671 BOOL bAny
= pTab
[nTab
]->GetPrintAreaHor( nStartRow
, nEndRow
, rEndCol
, bNotes
);
674 ScRange
aDrawRange(0,nStartRow
,nTab
, MAXCOL
,nEndRow
,nTab
);
675 if (DrawGetPrintArea( aDrawRange
, TRUE
, FALSE
))
677 if (aDrawRange
.aEnd
.Col()>rEndCol
) rEndCol
=aDrawRange
.aEnd
.Col();
688 BOOL
ScDocument::GetPrintAreaVer( SCTAB nTab
, SCCOL nStartCol
, SCCOL nEndCol
,
689 SCROW
& rEndRow
, BOOL bNotes
) const
691 if (ValidTab(nTab
) && pTab
[nTab
])
693 BOOL bAny
= pTab
[nTab
]->GetPrintAreaVer( nStartCol
, nEndCol
, rEndRow
, bNotes
);
696 ScRange
aDrawRange(nStartCol
,0,nTab
, nEndCol
,MAXROW
,nTab
);
697 if (DrawGetPrintArea( aDrawRange
, FALSE
, TRUE
))
699 if (aDrawRange
.aEnd
.Row()>rEndRow
) rEndRow
=aDrawRange
.aEnd
.Row();
710 BOOL
ScDocument::GetDataStart( SCTAB nTab
, SCCOL
& rStartCol
, SCROW
& rStartRow
) const
712 if (ValidTab(nTab
) && pTab
[nTab
])
714 BOOL bAny
= pTab
[nTab
]->GetDataStart( rStartCol
, rStartRow
);
717 ScRange
aDrawRange(0,0,nTab
, MAXCOL
,MAXROW
,nTab
);
718 if (DrawGetPrintArea( aDrawRange
, TRUE
, TRUE
))
720 if (aDrawRange
.aStart
.Col()<rStartCol
) rStartCol
=aDrawRange
.aStart
.Col();
721 if (aDrawRange
.aStart
.Row()<rStartRow
) rStartRow
=aDrawRange
.aStart
.Row();
733 BOOL
ScDocument::MoveTab( SCTAB nOldPos
, SCTAB nNewPos
)
735 if (nOldPos
== nNewPos
) return FALSE
;
737 if (VALIDTAB(nOldPos
))
741 SCTAB nTabCount
= GetTableCount();
744 BOOL bOldAutoCalc
= GetAutoCalc();
745 SetAutoCalc( FALSE
); // Mehrfachberechnungen vermeiden
746 SetNoListening( 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;
752 // Referenz-Updaterei
753 //! mit UpdateReference zusammenfassen!
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
);
762 pDPCollection
->UpdateReference( URM_REORDER
, aSourceRange
, 0,0,nDz
);
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
);
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
) );
776 ScTable
* pSaveTab
= pTab
[nOldPos
];
778 for (i
= nOldPos
+ 1; i
< nTabCount
; i
++)
779 pTab
[i
- 1] = pTab
[i
];
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
++)
786 pTab
[i
]->UpdateMoveTab( nOldPos
, nNewPos
, i
, *pProgress
);
787 delete pProgress
; // freimachen fuer evtl. andere
788 for (i
= 0; i
<= MAXTAB
; i
++)
790 pTab
[i
]->UpdateCompile();
791 SetNoListening( FALSE
);
792 for (i
= 0; i
<= MAXTAB
; i
++)
794 pTab
[i
]->StartAllListeners();
795 // #81844# sheet names of references may not be valid until sheet is moved
796 pChartListenerCollection
->UpdateScheduledSeriesRanges();
798 SetAutoCalc( bOldAutoCalc
);
801 DrawMovePage( static_cast<sal_uInt16
>(nOldPos
), static_cast<sal_uInt16
>(nNewPos
) );
803 // Update cells containing external references.
804 if (pExternalRefMgr
.get())
805 pExternalRefMgr
->updateRefMoveTable(nOldPos
, nNewPos
, false);
814 BOOL
ScDocument::CopyTab( SCTAB nOldPos
, SCTAB nNewPos
, const ScMarkData
* pOnlyMarked
)
816 if (SC_TAB_APPEND
== nNewPos
) nNewPos
= nMaxTableNumber
;
818 GetName(nOldPos
, aName
);
820 // vorneweg testen, ob der Prefix als gueltig erkannt wird
821 // wenn nicht, nur doppelte vermeiden
822 BOOL bPrefix
= ValidTabName( aName
);
823 DBG_ASSERT(bPrefix
, "ungueltiger Tabellenname");
826 CreateValidTabName(aName
);
830 bValid
= ( ValidNewTabName(aName
) && (nMaxTableNumber
<= MAXTAB
) );
832 bValid
= ( !GetTable( aName
, nDummy
) && (nMaxTableNumber
<= MAXTAB
) );
834 BOOL bOldAutoCalc
= GetAutoCalc();
835 SetAutoCalc( FALSE
); // Mehrfachberechnungen vermeiden
838 if (nNewPos
== nMaxTableNumber
)
840 pTab
[nMaxTableNumber
] = new ScTable(this, nMaxTableNumber
, aName
);
845 if (VALIDTAB(nNewPos
) && (nNewPos
< nMaxTableNumber
))
847 SetNoListening( TRUE
);
849 ScRange
aRange( 0,0,nNewPos
, MAXCOL
,MAXROW
,MAXTAB
);
850 xColNameRanges
->UpdateReference( URM_INSDEL
, this, aRange
, 0,0,1 );
851 xRowNameRanges
->UpdateReference( URM_INSDEL
, this, aRange
, 0,0,1 );
852 pRangeName
->UpdateTabRef(nNewPos
, 1);
853 pDBCollection
->UpdateReference(
854 URM_INSDEL
, 0,0,nNewPos
, MAXCOL
,MAXROW
,MAXTAB
, 0,0,1 );
856 pDPCollection
->UpdateReference( URM_INSDEL
, aRange
, 0,0,1 );
858 pDetOpList
->UpdateReference( this, URM_INSDEL
, aRange
, 0,0,1 );
859 UpdateChartRef( URM_INSDEL
, 0,0,nNewPos
, MAXCOL
,MAXROW
,MAXTAB
, 0,0,1 );
860 UpdateRefAreaLinks( URM_INSDEL
, aRange
, 0,0,1 );
861 if ( pUnoBroadcaster
)
862 pUnoBroadcaster
->Broadcast( ScUpdateRefHint( URM_INSDEL
, aRange
, 0,0,1 ) );
865 for (i
= 0; i
<= MAXTAB
; i
++)
866 if (pTab
[i
] && i
!= nOldPos
)
867 pTab
[i
]->UpdateInsertTab(nNewPos
);
868 for (i
= nMaxTableNumber
; i
> nNewPos
; i
--)
869 pTab
[i
] = pTab
[i
- 1];
870 if (nNewPos
<= nOldPos
)
872 pTab
[nNewPos
] = new ScTable(this, nNewPos
, aName
);
875 for (i
= 0; i
<= MAXTAB
; i
++)
876 if (pTab
[i
] && i
!= nOldPos
&& i
!= nNewPos
)
877 pTab
[i
]->UpdateCompile();
878 SetNoListening( FALSE
);
879 for (i
= 0; i
<= MAXTAB
; i
++)
880 if (pTab
[i
] && i
!= nOldPos
&& i
!= nNewPos
)
881 pTab
[i
]->StartAllListeners();
883 // update conditional formats after table is inserted
885 pCondFormList
->UpdateReference( URM_INSDEL
, aRange
, 0,0,1 );
886 if ( pValidationList
)
887 pValidationList
->UpdateReference( URM_INSDEL
, aRange
, 0,0,1 );
888 // #81844# sheet names of references may not be valid until sheet is copied
889 pChartListenerCollection
->UpdateScheduledSeriesRanges();
897 SetNoListening( TRUE
); // noch nicht bei CopyToTable/Insert
898 pTab
[nOldPos
]->CopyToTable(0, 0, MAXCOL
, MAXROW
, IDF_ALL
, (pOnlyMarked
!= NULL
),
899 pTab
[nNewPos
], pOnlyMarked
);
901 /* if (nNewPos < nOldPos)
902 nDz = ((short)nNewPos) - (short)nOldPos + 1;
904 */ nDz
= ((short)nNewPos
) - (short)nOldPos
;
905 pTab
[nNewPos
]->UpdateReference(URM_COPY
, 0, 0, nNewPos
, MAXCOL
, MAXROW
,
906 nNewPos
, 0, 0, nDz
, NULL
);
908 pTab
[nNewPos
]->UpdateInsertTabAbs(nNewPos
); // alle abs. um eins hoch!!
909 pTab
[nOldPos
]->UpdateInsertTab(nNewPos
);
911 pTab
[nOldPos
]->UpdateCompile();
912 pTab
[nNewPos
]->UpdateCompile( TRUE
); // #67996# maybe already compiled in Clone, but used names need recompilation
913 SetNoListening( FALSE
);
914 pTab
[nOldPos
]->StartAllListeners();
915 pTab
[nNewPos
]->StartAllListeners();
917 SetAutoCalc( bOldAutoCalc
);
920 DrawCopyPage( static_cast<sal_uInt16
>(nOldPos
), static_cast<sal_uInt16
>(nNewPos
) );
922 pTab
[nNewPos
]->SetPageStyle( pTab
[nOldPos
]->GetPageStyle() );
923 pTab
[nNewPos
]->SetPendingRowHeights( pTab
[nOldPos
]->IsPendingRowHeights() );
925 // Update cells containing external references.
926 if (pExternalRefMgr
.get())
927 pExternalRefMgr
->updateRefMoveTable(nOldPos
, nNewPos
, true);
930 SetAutoCalc( bOldAutoCalc
);
934 ULONG
ScDocument::TransferTab( ScDocument
* pSrcDoc
, SCTAB nSrcPos
,
935 SCTAB nDestPos
, BOOL bInsertNew
,
938 ULONG nRetVal
= 1; // 0 => Fehler 1 = ok
939 // 2 => RefBox, 3 => NameBox
942 if (bInsertNew
) // neu einfuegen
945 pSrcDoc
->GetName(nSrcPos
, aName
);
946 CreateValidTabName(aName
);
947 bValid
= InsertTab(nDestPos
, aName
);
949 else // bestehende Tabelle ersetzen
951 if (VALIDTAB(nDestPos
) && pTab
[nDestPos
])
953 pTab
[nDestPos
]->DeleteArea( 0,0, MAXCOL
,MAXROW
, IDF_ALL
);
961 BOOL bOldAutoCalcSrc
= FALSE
;
962 BOOL bOldAutoCalc
= GetAutoCalc();
963 SetAutoCalc( FALSE
); // Mehrfachberechnungen vermeiden
964 SetNoListening( TRUE
);
967 bOldAutoCalcSrc
= pSrcDoc
->GetAutoCalc();
968 pSrcDoc
->SetAutoCalc( TRUE
); // falls was berechnet werden muss
972 NumFmtMergeHandler
aNumFmtMergeHdl(this, pSrcDoc
);
974 nDestPos
= Min(nDestPos
, (SCTAB
)(GetTableCount() - 1));
975 { // scope for bulk broadcast
976 ScBulkBroadcast
aBulkBroadcast( pBASM
);
977 pSrcDoc
->pTab
[nSrcPos
]->CopyToTable(0, 0, MAXCOL
, MAXROW
,
978 ( bResultsOnly
? IDF_ALL
& ~IDF_FORMULA
: IDF_ALL
),
979 FALSE
, pTab
[nDestPos
] );
982 pTab
[nDestPos
]->SetTabNo(nDestPos
);
986 BOOL bNamesLost
= FALSE
;
987 USHORT nSrcRangeNames
= pSrcDoc
->pRangeName
->GetCount();
988 // array containing range names which might need update of indices
989 ScRangeData
** pSrcRangeNames
= nSrcRangeNames
? new ScRangeData
* [nSrcRangeNames
] : NULL
;
990 // the index mapping thereof
991 ScIndexMap
aSrcRangeMap( nSrcRangeNames
);
992 BOOL bRangeNameReplace
= FALSE
;
994 // find named ranges that are used in the source sheet
995 std::set
<USHORT
> aUsedNames
;
996 pSrcDoc
->pTab
[nSrcPos
]->FindRangeNamesInUse( 0, 0, MAXCOL
, MAXROW
, aUsedNames
);
998 for (USHORT i
= 0; i
< nSrcRangeNames
; i
++) //! DB-Bereiche Pivot-Bereiche auch !!!
1000 ScRangeData
* pSrcData
= (*pSrcDoc
->pRangeName
)[i
];
1001 USHORT nOldIndex
= pSrcData
->GetIndex();
1002 bool bInUse
= ( aUsedNames
.find(nOldIndex
) != aUsedNames
.end() );
1005 USHORT nExisting
= 0;
1006 if ( pRangeName
->SearchName( pSrcData
->GetName(), nExisting
) )
1008 // the name exists already in the destination document
1009 // -> use the existing name, but show a warning
1010 // (when refreshing links, the existing name is used and the warning not shown)
1012 ScRangeData
* pExistingData
= (*pRangeName
)[nExisting
];
1013 USHORT nExistingIndex
= pExistingData
->GetIndex();
1015 pSrcRangeNames
[i
] = NULL
; // don't modify the named range
1016 aSrcRangeMap
.SetPair( i
, nOldIndex
, nExistingIndex
);
1017 bRangeNameReplace
= TRUE
;
1022 ScRangeData
* pData
= new ScRangeData( *pSrcData
);
1023 pData
->SetDocument(this);
1024 if ( pRangeName
->FindIndex( pData
->GetIndex() ) )
1025 pData
->SetIndex(0); // need new index, done in Insert
1026 if (!pRangeName
->Insert(pData
))
1028 DBG_ERROR("can't insert name"); // shouldn't happen
1033 pData
->TransferTabRef( nSrcPos
, nDestPos
);
1034 pSrcRangeNames
[i
] = pData
;
1035 USHORT nNewIndex
= pData
->GetIndex();
1036 aSrcRangeMap
.SetPair( i
, nOldIndex
, nNewIndex
);
1037 if ( !bRangeNameReplace
)
1038 bRangeNameReplace
= ( nOldIndex
!= nNewIndex
);
1044 pSrcRangeNames
[i
] = NULL
;
1045 //aSrcRangeMap.SetPair( i, 0, 0 ); // not needed, defaulted
1048 if ( bRangeNameReplace
)
1050 // first update all inserted named formulas if they contain other
1051 // range names and used indices changed
1052 for (USHORT i
= 0; i
< nSrcRangeNames
; i
++) //! DB-Bereiche Pivot-Bereiche auch
1054 if ( pSrcRangeNames
[i
] )
1055 pSrcRangeNames
[i
]->ReplaceRangeNamesInUse( aSrcRangeMap
);
1057 // then update the formulas, they might need the just updated range names
1058 pTab
[nDestPos
]->ReplaceRangeNamesInUse( 0, 0, MAXCOL
, MAXROW
, aSrcRangeMap
);
1060 if ( pSrcRangeNames
)
1061 delete [] pSrcRangeNames
;
1063 SCsTAB nDz
= ((SCsTAB
)nDestPos
) - (SCsTAB
)nSrcPos
;
1064 pTab
[nDestPos
]->UpdateReference(URM_COPY
, 0, 0, nDestPos
,
1065 MAXCOL
, MAXROW
, nDestPos
,
1067 // Test for outside absolute references for info box
1068 BOOL bIsAbsRef
= pSrcDoc
->pTab
[nSrcPos
]->TestTabRefAbs(nSrcPos
);
1069 // Readjust self-contained absolute references to this sheet
1070 pTab
[nDestPos
]->TestTabRefAbs(nSrcPos
);
1074 // InfoBox AbsoluteRefs sind moeglicherweise nicht mehr korrekt!!
1079 // message: duplicate names
1081 pTab
[nDestPos
]->CompileAll();
1084 SetNoListening( FALSE
);
1085 if ( !bResultsOnly
)
1086 pTab
[nDestPos
]->StartAllListeners();
1087 SetDirty( ScRange( 0, 0, nDestPos
, MAXCOL
, MAXROW
, nDestPos
));
1090 pSrcDoc
->SetAutoCalc( bOldAutoCalcSrc
);
1091 SetAutoCalc( bOldAutoCalc
);
1096 TransferDrawPage( pSrcDoc
, nSrcPos
, nDestPos
);
1098 pTab
[nDestPos
]->SetPendingRowHeights( pSrcDoc
->pTab
[nSrcPos
]->IsPendingRowHeights() );
1105 // ----------------------------------------------------------------------------
1107 void ScDocument::SetError( SCCOL nCol
, SCROW nRow
, SCTAB nTab
, const USHORT nError
)
1111 pTab
[nTab
]->SetError( nCol
, nRow
, nError
);
1114 void ScDocument::EraseNonUsedSharedNames(USHORT nLevel
)
1116 for (USHORT i
= 0; i
< pRangeName
->GetCount(); i
++)
1118 ScRangeData
* pRangeData
= (*pRangeName
)[i
];
1119 if (pRangeData
&& pRangeData
->HasType(RT_SHARED
))
1122 pRangeData
->GetName(aName
);
1123 aName
.Erase(0, 6); // !!! vgl. Table4, FillFormula !!
1124 USHORT nInd
= (USHORT
) aName
.ToInt32();
1127 USHORT nIndex
= pRangeData
->GetIndex();
1128 BOOL bInUse
= FALSE
;
1129 for (SCTAB j
= 0; !bInUse
&& (j
<= MAXTAB
); j
++)
1132 bInUse
= pTab
[j
]->IsRangeNameInUse(0, 0, MAXCOL
-1, MAXROW
-1,
1136 pRangeName
->AtFree(i
);
1142 // ----------------------------------------------------------------------------
1144 void ScDocument::SetConsolidateDlgData( const ScConsolidateParam
* pData
)
1146 delete pConsolidateDlgData
;
1149 pConsolidateDlgData
= new ScConsolidateParam( *pData
);
1151 pConsolidateDlgData
= NULL
;
1154 void ScDocument::SetChangeViewSettings(const ScChangeViewSettings
& rNew
)
1156 if (pChangeViewSettings
==NULL
)
1157 pChangeViewSettings
= new ScChangeViewSettings
;
1159 DBG_ASSERT( pChangeViewSettings
, "Oops. No ChangeViewSettings :-( by!" );
1161 *pChangeViewSettings
=rNew
;
1164 // ----------------------------------------------------------------------------
1166 ScFieldEditEngine
* ScDocument::CreateFieldEditEngine()
1168 ScFieldEditEngine
* pNewEditEngine
= NULL
;
1169 if (!pCacheFieldEditEngine
)
1171 pNewEditEngine
= new ScFieldEditEngine( GetEnginePool(),
1172 GetEditPool(), FALSE
);
1176 if ( !bImportingXML
)
1178 // #i66209# previous use might not have restored update mode,
1179 // ensure same state as for a new EditEngine (UpdateMode = TRUE)
1180 if ( !pCacheFieldEditEngine
->GetUpdateMode() )
1181 pCacheFieldEditEngine
->SetUpdateMode(TRUE
);
1184 pNewEditEngine
= pCacheFieldEditEngine
;
1185 pCacheFieldEditEngine
= NULL
;
1187 return pNewEditEngine
;
1190 void ScDocument::DisposeFieldEditEngine(ScFieldEditEngine
*& rpEditEngine
)
1192 if (!pCacheFieldEditEngine
&& rpEditEngine
)
1194 pCacheFieldEditEngine
= rpEditEngine
;
1195 pCacheFieldEditEngine
->Clear();
1198 delete rpEditEngine
;
1199 rpEditEngine
= NULL
;
1202 // ----------------------------------------------------------------------------
1205 ScRecursionHelper
* ScDocument::CreateRecursionHelperInstance()
1207 return new ScRecursionHelper
;
1210 // ----------------------------------------------------------------------------
1212 ScLookupCache
& ScDocument::GetLookupCache( const ScRange
& rRange
)
1214 ScLookupCache
* pCache
= 0;
1215 if (!pLookupCacheMapImpl
)
1216 pLookupCacheMapImpl
= new ScLookupCacheMapImpl
;
1217 ScLookupCacheMap::iterator
it( pLookupCacheMapImpl
->aCacheMap
.find( rRange
));
1218 if (it
== pLookupCacheMapImpl
->aCacheMap
.end())
1220 pCache
= new ScLookupCache( this, rRange
);
1221 AddLookupCache( *pCache
);
1224 pCache
= (*it
).second
;
1228 void ScDocument::AddLookupCache( ScLookupCache
& rCache
)
1230 if (!pLookupCacheMapImpl
->aCacheMap
.insert( ::std::pair
< const ScRange
,
1231 ScLookupCache
*>( rCache
.getRange(), &rCache
)).second
)
1233 DBG_ERRORFILE( "ScDocument::AddLookupCache: couldn't add to hash map");
1236 StartListeningArea( rCache
.getRange(), &rCache
);
1239 void ScDocument::RemoveLookupCache( ScLookupCache
& rCache
)
1241 ScLookupCacheMap::iterator
it( pLookupCacheMapImpl
->aCacheMap
.find(
1242 rCache
.getRange()));
1243 if (it
== pLookupCacheMapImpl
->aCacheMap
.end())
1245 DBG_ERRORFILE( "ScDocument::RemoveLookupCache: range not found in hash map");
1249 ScLookupCache
* pCache
= (*it
).second
;
1250 pLookupCacheMapImpl
->aCacheMap
.erase( it
);
1251 EndListeningArea( pCache
->getRange(), &rCache
);
1255 using namespace com::sun::star
;
1256 uno::Reference
< document::XVbaEventsHelper
>
1257 ScDocument::GetVbaEventsHelper()
1259 if( !mxVbaEventsHelper
.is() )
1263 uno::Reference
< lang::XMultiServiceFactory
> xSF( comphelper::getProcessServiceFactory(), uno::UNO_QUERY
);
1264 uno::Reference
< frame::XModel
> xModel( pShell
? pShell
->GetModel() : NULL
, uno::UNO_QUERY
);
1265 uno::Sequence
< uno::Any
> aArgs(1);
1266 aArgs
[0] = uno::Any( xModel
);
1267 uno::Reference
< document::XVbaEventsHelper
> xVbaEventsHelper( xSF
->createInstanceWithArguments( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.VbaEventsHelper" ) ), aArgs
), uno::UNO_QUERY
);
1268 // helper will always be created successfully.
1269 mxVbaEventsHelper
.set( xVbaEventsHelper
, uno::UNO_QUERY
);
1271 catch( uno::Exception
& e
)
1275 return mxVbaEventsHelper
;
1278 void ScDocument::ClearLookupCaches()
1280 if( pLookupCacheMapImpl
)
1281 pLookupCacheMapImpl
->clear();