merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / docshell / tablink.cxx
blobf49453eb85d6aa51087729f151cd6b799b6ceb05
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tablink.cxx,v $
10 * $Revision: 1.31.32.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"
33 #ifdef _MSC_VER
34 #pragma optimize("",off)
35 #endif
37 //------------------------------------------------------------------
41 // INCLUDE ---------------------------------------------------------
43 #include <sfx2/sfxsids.hrc>
44 #include <sfx2/app.hxx>
45 #include <svtools/itemset.hxx>
46 #include <svtools/stritem.hxx>
47 #include <sfx2/docfile.hxx>
48 #include <sfx2/docfilt.hxx>
49 #include <sfx2/fcontnr.hxx>
50 #include <svx/linkmgr.hxx>
51 #include <tools/urlobj.hxx>
52 #include <unotools/transliterationwrapper.hxx>
54 #include "tablink.hxx"
56 #include "scextopt.hxx"
57 #include "table.hxx"
58 #include "document.hxx"
59 #include "docsh.hxx"
60 #include "globstr.hrc"
61 #include "undoblk.hxx"
62 #include "undotab.hxx"
63 #include "global.hxx"
64 #include "hints.hxx"
65 #include "cell.hxx"
66 #include "dociter.hxx"
67 #include "formula/opcode.hxx"
69 struct TableLink_Impl
71 ScDocShell* m_pDocSh;
72 Window* m_pOldParent;
73 Link m_aEndEditLink;
75 TableLink_Impl() : m_pDocSh( NULL ), m_pOldParent( NULL ) {}
78 TYPEINIT1(ScTableLink, ::sfx2::SvBaseLink);
80 //------------------------------------------------------------------------
82 ScTableLink::ScTableLink(ScDocShell* pDocSh, const String& rFile,
83 const String& rFilter, const String& rOpt,
84 ULONG nRefresh ):
85 ::sfx2::SvBaseLink(sfx2::LINKUPDATE_ONCALL,FORMAT_FILE),
86 ScRefreshTimer( nRefresh ),
87 pImpl( new TableLink_Impl ),
88 aFileName(rFile),
89 aFilterName(rFilter),
90 aOptions(rOpt),
91 bInCreate( FALSE ),
92 bInEdit( FALSE ),
93 bAddUndo( TRUE ),
94 bDoPaint( TRUE )
96 pImpl->m_pDocSh = pDocSh;
99 ScTableLink::ScTableLink(SfxObjectShell* pShell, const String& rFile,
100 const String& rFilter, const String& rOpt,
101 ULONG nRefresh ):
102 ::sfx2::SvBaseLink(sfx2::LINKUPDATE_ONCALL,FORMAT_FILE),
103 ScRefreshTimer( nRefresh ),
104 pImpl( new TableLink_Impl ),
105 aFileName(rFile),
106 aFilterName(rFilter),
107 aOptions(rOpt),
108 bInCreate( FALSE ),
109 bInEdit( FALSE ),
110 bAddUndo( TRUE ),
111 bDoPaint( TRUE )
113 pImpl->m_pDocSh = static_cast< ScDocShell* >( pShell );
114 SetRefreshHandler( LINK( this, ScTableLink, RefreshHdl ) );
115 SetRefreshControl( pImpl->m_pDocSh->GetDocument()->GetRefreshTimerControlAddress() );
118 __EXPORT ScTableLink::~ScTableLink()
120 // Verbindung aufheben
122 StopRefreshTimer();
123 String aEmpty;
124 ScDocument* pDoc = pImpl->m_pDocSh->GetDocument();
125 SCTAB nCount = pDoc->GetTableCount();
126 for (SCTAB nTab=0; nTab<nCount; nTab++)
127 if (pDoc->IsLinked(nTab) && pDoc->GetLinkDoc(nTab)==aFileName)
128 pDoc->SetLink( nTab, SC_LINK_NONE, aEmpty, aEmpty, aEmpty, aEmpty, 0 );
129 delete pImpl;
132 void __EXPORT ScTableLink::Edit( Window* pParent, const Link& rEndEditHdl )
134 // DefModalDialogParent setzen, weil evtl. aus der DocShell beim ConvertFrom
135 // ein Optionen-Dialog kommt...
137 pImpl->m_aEndEditLink = rEndEditHdl;
138 pImpl->m_pOldParent = Application::GetDefDialogParent();
139 if (pParent)
140 Application::SetDefDialogParent(pParent);
142 bInEdit = TRUE;
143 SvBaseLink::Edit( pParent, LINK( this, ScTableLink, TableEndEditHdl ) );
146 void __EXPORT ScTableLink::DataChanged( const String&,
147 const ::com::sun::star::uno::Any& )
149 SvxLinkManager* pLinkManager=pImpl->m_pDocSh->GetDocument()->GetLinkManager();
150 if (pLinkManager!=NULL)
152 String aFile;
153 String aFilter;
154 pLinkManager->GetDisplayNames( this,0,&aFile,NULL,&aFilter);
156 // the file dialog returns the filter name with the application prefix
157 // -> remove prefix
158 ScDocumentLoader::RemoveAppPrefix( aFilter );
160 if (!bInCreate)
161 Refresh( aFile, aFilter, NULL, GetRefreshDelay() ); // don't load twice
165 void __EXPORT ScTableLink::Closed()
167 // Verknuepfung loeschen: Undo
168 ScDocument* pDoc = pImpl->m_pDocSh->GetDocument();
169 BOOL bUndo (pDoc->IsUndoEnabled());
171 if (bAddUndo && bUndo)
173 pImpl->m_pDocSh->GetUndoManager()->AddUndoAction(
174 new ScUndoRemoveLink( pImpl->m_pDocSh, aFileName ) );
176 bAddUndo = FALSE; // nur einmal
179 // Verbindung wird im dtor aufgehoben
181 SvBaseLink::Closed();
184 BOOL ScTableLink::IsUsed() const
186 return pImpl->m_pDocSh->GetDocument()->HasLink( aFileName, aFilterName, aOptions );
189 BOOL ScTableLink::Refresh(const String& rNewFile, const String& rNewFilter,
190 const String* pNewOptions, ULONG nNewRefresh )
192 // Dokument laden
194 if (!rNewFile.Len() || !rNewFilter.Len())
195 return FALSE;
197 String aNewUrl( ScGlobal::GetAbsDocName( rNewFile, pImpl->m_pDocSh ) );
198 BOOL bNewUrlName = (aNewUrl != aFileName);
200 const SfxFilter* pFilter = pImpl->m_pDocSh->GetFactory().GetFilterContainer()->GetFilter4FilterName(rNewFilter);
201 if (!pFilter)
202 return FALSE;
204 ScDocument* pDoc = pImpl->m_pDocSh->GetDocument();
205 pDoc->SetInLinkUpdate( TRUE );
207 BOOL bUndo(pDoc->IsUndoEnabled());
209 // wenn neuer Filter ausgewaehlt wurde, Optionen vergessen
210 if ( rNewFilter != aFilterName )
211 aOptions.Erase();
212 if ( pNewOptions ) // Optionen hart angegeben?
213 aOptions = *pNewOptions;
215 // ItemSet immer anlegen, damit die DocShell die Optionen setzen kann
216 SfxItemSet* pSet = new SfxAllItemSet( SFX_APP()->GetPool() );
217 if ( aOptions.Len() )
218 pSet->Put( SfxStringItem( SID_FILE_FILTEROPTIONS, aOptions ) );
220 SfxMedium* pMed = new SfxMedium(aNewUrl, STREAM_STD_READ, FALSE, pFilter, pSet);
222 if ( bInEdit ) // only if using the edit dialog,
223 pMed->UseInteractionHandler( TRUE ); // enable the filter options dialog
225 ScDocShell* pSrcShell = new ScDocShell(SFX_CREATE_MODE_INTERNAL);
226 SfxObjectShellRef aRef = pSrcShell;
227 pSrcShell->DoLoad(pMed);
229 // Optionen koennten gesetzt worden sein
230 String aNewOpt = ScDocumentLoader::GetOptions(*pMed);
231 if (!aNewOpt.Len())
232 aNewOpt = aOptions;
234 // Undo...
236 ScDocument* pUndoDoc = NULL;
237 BOOL bFirst = TRUE;
238 if (bAddUndo && bUndo)
239 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
241 // Tabellen kopieren
243 ScDocShellModificator aModificator( *pImpl->m_pDocSh );
245 BOOL bNotFound = FALSE;
246 ScDocument* pSrcDoc = pSrcShell->GetDocument();
248 // #74835# from text filters that don't set the table name,
249 // use the one table regardless of link table name
250 BOOL bAutoTab = (pSrcDoc->GetTableCount() == 1) &&
251 ScDocShell::HasAutomaticTableName( rNewFilter );
253 SCTAB nCount = pDoc->GetTableCount();
254 for (SCTAB nTab=0; nTab<nCount; nTab++)
256 BYTE nMode = pDoc->GetLinkMode(nTab);
257 if (nMode && pDoc->GetLinkDoc(nTab)==aFileName)
259 String aTabName = pDoc->GetLinkTab(nTab);
261 // Undo
263 if (bAddUndo && bUndo)
265 if (bFirst)
266 pUndoDoc->InitUndo( pDoc, nTab, nTab, TRUE, TRUE );
267 else
268 pUndoDoc->AddUndoTab( nTab, nTab, TRUE, TRUE );
269 bFirst = FALSE;
270 ScRange aRange(0,0,nTab,MAXCOL,MAXROW,nTab);
271 pDoc->CopyToDocument(aRange, IDF_ALL, FALSE, pUndoDoc);
272 pUndoDoc->TransferDrawPage( pDoc, nTab, nTab );
273 pUndoDoc->SetLink( nTab, nMode, aFileName, aFilterName,
274 aOptions, aTabName, GetRefreshDelay() );
277 // Tabellenname einer ExtDocRef anpassen
279 if ( bNewUrlName && nMode == SC_LINK_VALUE )
281 String aName;
282 pDoc->GetName( nTab, aName );
283 if ( ScGlobal::GetpTransliteration()->isEqual(
284 ScGlobal::GetDocTabName( aFileName, aTabName ), aName ) )
286 pDoc->RenameTab( nTab,
287 ScGlobal::GetDocTabName( aNewUrl, aTabName ),
288 FALSE, TRUE ); // kein RefUpdate, kein ValidTabName
292 // kopieren
294 SCTAB nSrcTab = 0;
295 bool bFound = false;
296 /* #i71497# check if external document is loaded successfully,
297 otherwise we may find the empty default sheet "Sheet1" in
298 pSrcDoc, even if the document does not exist. */
299 if( pMed->GetError() == 0 )
301 // no sheet name -> use first sheet
302 if ( aTabName.Len() && !bAutoTab )
303 bFound = pSrcDoc->GetTable( aTabName, nSrcTab );
304 else
305 bFound = true;
308 if (bFound)
309 pDoc->TransferTab( pSrcDoc, nSrcTab, nTab, FALSE, // nicht neu einfuegen
310 (nMode == SC_LINK_VALUE) ); // nur Werte?
311 else
313 pDoc->DeleteAreaTab( 0,0,MAXCOL,MAXROW, nTab, IDF_ALL );
315 bool bShowError = true;
316 if ( nMode == SC_LINK_VALUE )
318 // #139464# Value link (used with external references in formulas):
319 // Look for formulas that reference the sheet, and put errors in the referenced cells.
321 ScRangeList aErrorCells; // cells on the linked sheets that need error values
323 ScCellIterator aCellIter( pDoc, 0,0,0, MAXCOL,MAXROW,MAXTAB ); // all sheets
324 ScBaseCell* pCell = aCellIter.GetFirst();
325 while (pCell)
327 if (pCell->GetCellType() == CELLTYPE_FORMULA)
329 ScFormulaCell* pFCell = static_cast<ScFormulaCell*>(pCell);
331 ScDetectiveRefIter aRefIter( pFCell );
332 ScRange aRefRange;
333 while ( aRefIter.GetNextRef( aRefRange ) )
335 if ( aRefRange.aStart.Tab() <= nTab && aRefRange.aEnd.Tab() >= nTab )
337 // use first cell of range references (don't fill potentially large ranges)
339 aErrorCells.Join( ScRange( aRefRange.aStart ) );
343 pCell = aCellIter.GetNext();
346 ULONG nRanges = aErrorCells.Count();
347 if ( nRanges ) // found any?
349 ScTokenArray aTokenArr;
350 aTokenArr.AddOpCode( ocNotAvail );
351 aTokenArr.AddOpCode( ocOpen );
352 aTokenArr.AddOpCode( ocClose );
353 aTokenArr.AddOpCode( ocStop );
355 for (ULONG nPos=0; nPos<nRanges; nPos++)
357 const ScRange* pRange = aErrorCells.GetObject(nPos);
358 SCCOL nStartCol = pRange->aStart.Col();
359 SCROW nStartRow = pRange->aStart.Row();
360 SCCOL nEndCol = pRange->aEnd.Col();
361 SCROW nEndRow = pRange->aEnd.Row();
362 for (SCROW nRow=nStartRow; nRow<=nEndRow; nRow++)
363 for (SCCOL nCol=nStartCol; nCol<=nEndCol; nCol++)
365 ScAddress aDestPos( nCol, nRow, nTab );
366 ScFormulaCell* pNewCell = new ScFormulaCell( pDoc, aDestPos, &aTokenArr );
367 pDoc->PutCell( aDestPos, pNewCell );
371 bShowError = false;
373 // if no references were found, insert error message (don't leave the sheet empty)
376 if ( bShowError )
378 // Normal link or no references: put error message on sheet.
380 pDoc->SetString( 0,0,nTab, ScGlobal::GetRscString(STR_LINKERROR) );
381 pDoc->SetString( 0,1,nTab, ScGlobal::GetRscString(STR_LINKERRORFILE) );
382 pDoc->SetString( 1,1,nTab, aNewUrl );
383 pDoc->SetString( 0,2,nTab, ScGlobal::GetRscString(STR_LINKERRORTAB) );
384 pDoc->SetString( 1,2,nTab, aTabName );
387 bNotFound = TRUE;
390 if ( bNewUrlName || rNewFilter != aFilterName ||
391 aNewOpt != aOptions || pNewOptions ||
392 nNewRefresh != GetRefreshDelay() )
393 pDoc->SetLink( nTab, nMode, aNewUrl, rNewFilter, aNewOpt,
394 aTabName, nNewRefresh );
398 // neue Einstellungen merken
400 if ( bNewUrlName )
401 aFileName = aNewUrl;
402 if ( rNewFilter != aFilterName )
403 aFilterName = rNewFilter;
404 if ( aNewOpt != aOptions )
405 aOptions = aNewOpt;
407 // aufraeumen
409 // pSrcShell->DoClose();
410 aRef->DoClose();
412 // Undo
414 if (bAddUndo && bUndo)
415 pImpl->m_pDocSh->GetUndoManager()->AddUndoAction(
416 new ScUndoRefreshLink( pImpl->m_pDocSh, pUndoDoc ) );
418 // Paint (koennen mehrere Tabellen sein)
420 if (bDoPaint)
422 pImpl->m_pDocSh->PostPaint( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB),
423 PAINT_GRID | PAINT_TOP | PAINT_LEFT );
424 aModificator.SetDocumentModified();
427 if (bNotFound)
429 //! Fehler ausgeben ?
432 pDoc->SetInLinkUpdate( FALSE );
434 // notify Uno objects (for XRefreshListener)
435 //! also notify Uno objects if file name was changed!
436 ScLinkRefreshedHint aHint;
437 aHint.SetSheetLink( aFileName );
438 pDoc->BroadcastUno( aHint );
440 return TRUE;
443 IMPL_LINK( ScTableLink, RefreshHdl, ScTableLink*, EMPTYARG )
445 long nRes = Refresh( aFileName, aFilterName, NULL, GetRefreshDelay() ) != 0;
446 return nRes;
449 IMPL_LINK( ScTableLink, TableEndEditHdl, ::sfx2::SvBaseLink*, pLink )
451 if ( pImpl->m_aEndEditLink.IsSet() )
452 pImpl->m_aEndEditLink.Call( pLink );
453 bInEdit = FALSE;
454 Application::SetDefDialogParent( pImpl->m_pOldParent );
455 return 0;
458 // === ScDocumentLoader ==================================================
460 String ScDocumentLoader::GetOptions( SfxMedium& rMedium ) // static
462 SfxItemSet* pSet = rMedium.GetItemSet();
463 const SfxPoolItem* pItem;
464 if ( pSet && SFX_ITEM_SET == pSet->GetItemState( SID_FILE_FILTEROPTIONS, TRUE, &pItem ) )
465 return ((const SfxStringItem*)pItem)->GetValue();
467 return EMPTY_STRING;
470 BOOL ScDocumentLoader::GetFilterName( const String& rFileName,
471 String& rFilter, String& rOptions,
472 BOOL bWithContent, BOOL bWithInteraction ) // static
474 TypeId aScType = TYPE(ScDocShell);
475 SfxObjectShell* pDocSh = SfxObjectShell::GetFirst( &aScType );
476 while ( pDocSh )
478 if ( pDocSh->HasName() )
480 SfxMedium* pMed = pDocSh->GetMedium();
481 if ( rFileName == pMed->GetName() )
483 rFilter = pMed->GetFilter()->GetFilterName();
484 rOptions = GetOptions(*pMed);
485 return TRUE;
488 pDocSh = SfxObjectShell::GetNext( *pDocSh, &aScType );
491 // Filter-Detection
493 const SfxFilter* pSfxFilter = NULL;
494 SfxMedium* pMedium = new SfxMedium( rFileName, STREAM_STD_READ, FALSE );
495 if ( pMedium->GetError() == ERRCODE_NONE )
497 if ( bWithInteraction )
498 pMedium->UseInteractionHandler(TRUE); // #i73992# no longer called from GuessFilter
500 SfxFilterMatcher aMatcher( String::CreateFromAscii("scalc") );
501 if( bWithContent )
502 aMatcher.GuessFilter( *pMedium, &pSfxFilter );
503 else
504 aMatcher.GuessFilterIgnoringContent( *pMedium, &pSfxFilter );
507 BOOL bOK = FALSE;
508 if ( pMedium->GetError() == ERRCODE_NONE )
510 if ( pSfxFilter )
511 rFilter = pSfxFilter->GetFilterName();
512 else
513 rFilter = ScDocShell::GetOwnFilterName(); // sonst Calc-Datei
514 bOK = (rFilter.Len()>0);
517 delete pMedium;
518 return bOK;
521 void ScDocumentLoader::RemoveAppPrefix( String& rFilterName ) // static
523 String aAppPrefix = String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM( STRING_SCAPP ));
524 aAppPrefix.AppendAscii(RTL_CONSTASCII_STRINGPARAM( ": " ));
525 xub_StrLen nPreLen = aAppPrefix.Len();
526 if ( rFilterName.Copy(0,nPreLen) == aAppPrefix )
527 rFilterName.Erase(0,nPreLen);
530 ScDocumentLoader::ScDocumentLoader( const String& rFileName,
531 String& rFilterName, String& rOptions,
532 UINT32 nRekCnt, BOOL bWithInteraction ) :
533 pDocShell(0),
534 pMedium(0)
536 if ( !rFilterName.Len() )
537 GetFilterName( rFileName, rFilterName, rOptions, TRUE, bWithInteraction );
539 const SfxFilter* pFilter = ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( rFilterName );
541 // ItemSet immer anlegen, damit die DocShell die Optionen setzen kann
542 SfxItemSet* pSet = new SfxAllItemSet( SFX_APP()->GetPool() );
543 if ( rOptions.Len() )
544 pSet->Put( SfxStringItem( SID_FILE_FILTEROPTIONS, rOptions ) );
546 pMedium = new SfxMedium( rFileName, STREAM_STD_READ, FALSE, pFilter, pSet );
547 if ( pMedium->GetError() != ERRCODE_NONE )
548 return ;
550 if ( bWithInteraction )
551 pMedium->UseInteractionHandler( TRUE ); // to enable the filter options dialog
553 pDocShell = new ScDocShell( SFX_CREATE_MODE_INTERNAL );
554 aRef = pDocShell;
556 ScDocument* pDoc = pDocShell->GetDocument();
557 if( pDoc )
559 ScExtDocOptions* pExtDocOpt = pDoc->GetExtDocOptions();
560 if( !pExtDocOpt )
562 pExtDocOpt = new ScExtDocOptions;
563 pDoc->SetExtDocOptions( pExtDocOpt );
565 pExtDocOpt->GetDocSettings().mnLinkCnt = nRekCnt;
568 pDocShell->DoLoad( pMedium );
570 String aNew = GetOptions(*pMedium); // Optionen werden beim Laden per Dialog gesetzt
571 if (aNew.Len() && aNew != rOptions)
572 rOptions = aNew;
575 ScDocumentLoader::~ScDocumentLoader()
577 /* if ( pDocShell )
578 pDocShell->DoClose();
580 if ( aRef.Is() )
581 aRef->DoClose();
582 else if ( pMedium )
583 delete pMedium;
586 void ScDocumentLoader::ReleaseDocRef()
588 if ( aRef.Is() )
590 // release reference without calling DoClose - caller must
591 // have another reference to the doc and call DoClose later
593 pDocShell = NULL;
594 pMedium = NULL;
595 aRef.Clear();
599 ScDocument* ScDocumentLoader::GetDocument()
601 return pDocShell ? pDocShell->GetDocument() : 0;
604 BOOL ScDocumentLoader::IsError() const
606 if ( pDocShell && pMedium )
607 return pMedium->GetError() != ERRCODE_NONE;
608 else
609 return TRUE;
612 String ScDocumentLoader::GetTitle() const
614 if ( pDocShell )
615 return pDocShell->GetTitle();
616 else
617 return EMPTY_STRING;