Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / docshell / docsh6.cxx
blob988dafda10c57da0a417806a52f61ea7261925bd
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"
22 #include <svx/pageitem.hxx>
23 #include <sfx2/linkmgr.hxx>
25 #include "docsh.hxx"
27 #include "stlsheet.hxx"
28 #include "stlpool.hxx"
29 #include "global.hxx"
30 #include "viewdata.hxx"
31 #include "tabvwsh.hxx"
32 #include "tablink.hxx"
33 #include "globstr.hrc"
34 #include "scmod.hxx"
35 #include "compiler.hxx"
36 #include "interpre.hxx"
37 #include "calcconfig.hxx"
39 #include <vcl/msgbox.hxx>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/container/XNameAccess.hpp>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/util/XChangesBatch.hpp>
45 #include <boost/scoped_array.hpp>
47 using ::com::sun::star::beans::XPropertySet;
48 using ::com::sun::star::lang::XMultiServiceFactory;
49 using ::com::sun::star::container::XNameAccess;
50 using ::com::sun::star::util::XChangesBatch;
51 using ::com::sun::star::uno::Any;
52 using ::com::sun::star::uno::Exception;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::uno::Sequence;
55 using ::com::sun::star::uno::UNO_QUERY_THROW;
57 namespace {
59 struct ScStylePair
61 SfxStyleSheetBase *pSource;
62 SfxStyleSheetBase *pDest;
67 // STATIC DATA -----------------------------------------------------------
70 // Ole
73 void ScDocShell::SetVisArea( const Rectangle & rVisArea )
75 // with the SnapVisArea call in SetVisAreaOrSize, it's safe to always
76 // use both the size and position of the VisArea
77 SetVisAreaOrSize( rVisArea, true );
80 static void lcl_SetTopRight( Rectangle& rRect, const Point& rPos )
82 Size aSize = rRect.GetSize();
83 rRect.Right() = rPos.X();
84 rRect.Left() = rPos.X() - aSize.Width() + 1;
85 rRect.Top() = rPos.Y();
86 rRect.Bottom() = rPos.Y() + aSize.Height() - 1;
89 void ScDocShell::SetVisAreaOrSize( const Rectangle& rVisArea, bool bModifyStart )
91 bool bNegativePage = aDocument.IsNegativePage( aDocument.GetVisibleTab() );
93 Rectangle aArea = rVisArea;
94 if (bModifyStart)
96 // when loading, don't check for negative values, because the sheet orientation
97 // might be set later
98 if ( !aDocument.IsImportingXML() )
100 if ( ( bNegativePage ? (aArea.Right() > 0) : (aArea.Left() < 0) ) || aArea.Top() < 0 )
102 // VisArea start position can't be negative.
103 // Move the VisArea, otherwise only the upper left position would
104 // be changed in SnapVisArea, and the size would be wrong.
106 Point aNewPos( 0, std::max( aArea.Top(), (long) 0 ) );
107 if ( bNegativePage )
109 aNewPos.X() = std::min( aArea.Right(), (long) 0 );
110 lcl_SetTopRight( aArea, aNewPos );
112 else
114 aNewPos.X() = std::max( aArea.Left(), (long) 0 );
115 aArea.SetPos( aNewPos );
120 else
122 Rectangle aOldVisArea = SfxObjectShell::GetVisArea();
123 if ( bNegativePage )
124 lcl_SetTopRight( aArea, aOldVisArea.TopRight() );
125 else
126 aArea.SetPos( aOldVisArea.TopLeft() );
129 // hier Position anpassen!
131 // when loading an ole object, the VisArea is set from the document's
132 // view settings and must be used as-is (document content may not be complete yet).
133 if ( !aDocument.IsImportingXML() )
134 SnapVisArea( aArea );
136 //TODO/LATER: it's unclear which IPEnv is used here
138 SvInPlaceEnvironment* pEnv = GetIPEnv();
139 if (pEnv)
141 Window* pWin = pEnv->GetEditWin();
142 pEnv->MakeScale( aArea.GetSize(), MAP_100TH_MM,
143 pWin->LogicToPixel( aArea.GetSize() ) );
144 } */
146 //TODO/LATER: formerly in SvInplaceObject
147 SfxObjectShell::SetVisArea( aArea );
149 if (bIsInplace) // Zoom in der InPlace View einstellen
151 ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
152 if (pViewSh)
154 if (pViewSh->GetViewData()->GetDocShell() == this)
155 pViewSh->UpdateOleZoom();
159 if (aDocument.IsEmbedded())
161 ScRange aOld;
162 aDocument.GetEmbedded( aOld);
163 aDocument.SetEmbedded( aDocument.GetVisibleTab(), aArea );
164 ScRange aNew;
165 aDocument.GetEmbedded( aNew);
166 if (aOld != aNew)
167 PostPaint(0,0,0,MAXCOL,MAXROW,MAXTAB,PAINT_GRID);
169 //TODO/LATER: currently not implemented
170 //ViewChanged( ASPECT_CONTENT ); // auch im Container anzeigen
174 bool ScDocShell::IsOle()
176 return (GetCreateMode() == SFX_CREATE_MODE_EMBEDDED);
179 void ScDocShell::UpdateOle( const ScViewData* pViewData, bool bSnapSize )
181 // wenn's gar nicht Ole ist, kann man sich die Berechnungen sparen
182 // (VisArea wird dann beim Save wieder zurueckgesetzt)
184 if (GetCreateMode() == SFX_CREATE_MODE_STANDARD)
185 return;
187 OSL_ENSURE(pViewData,"pViewData==0 bei ScDocShell::UpdateOle");
189 Rectangle aOldArea = SfxObjectShell::GetVisArea();
190 Rectangle aNewArea = aOldArea;
192 bool bEmbedded = aDocument.IsEmbedded();
193 if (bEmbedded)
194 aNewArea = aDocument.GetEmbeddedRect();
195 else
197 SCTAB nTab = pViewData->GetTabNo();
198 if ( nTab != aDocument.GetVisibleTab() )
199 aDocument.SetVisibleTab( nTab );
201 bool bNegativePage = aDocument.IsNegativePage( nTab );
202 SCCOL nX = pViewData->GetPosX(SC_SPLIT_LEFT);
203 SCROW nY = pViewData->GetPosY(SC_SPLIT_BOTTOM);
204 Rectangle aMMRect = aDocument.GetMMRect( nX,nY, nX,nY, nTab );
205 if (bNegativePage)
206 lcl_SetTopRight( aNewArea, aMMRect.TopRight() );
207 else
208 aNewArea.SetPos( aMMRect.TopLeft() );
209 if (bSnapSize)
210 SnapVisArea(aNewArea); // uses the new VisibleTab
213 if (aNewArea != aOldArea)
214 SetVisAreaOrSize( aNewArea, true ); // hier muss auch der Start angepasst werden
218 // Style-Krempel fuer Organizer etc.
221 SfxStyleSheetBasePool* ScDocShell::GetStyleSheetPool()
223 return (SfxStyleSheetBasePool*)aDocument.GetStyleSheetPool();
227 // nach dem Laden von Vorlagen aus einem anderen Dokment (LoadStyles, Insert)
228 // muessen die SetItems (ATTR_PAGE_HEADERSET, ATTR_PAGE_FOOTERSET) auf den richtigen
229 // Pool umgesetzt werden, bevor der Quell-Pool geloescht wird.
231 static void lcl_AdjustPool( SfxStyleSheetBasePool* pStylePool )
233 pStylePool->SetSearchMask(SFX_STYLE_FAMILY_PAGE, SFXSTYLEBIT_ALL);
234 SfxStyleSheetBase *pStyle = pStylePool->First();
235 while ( pStyle )
237 SfxItemSet& rStyleSet = pStyle->GetItemSet();
239 const SfxPoolItem* pItem;
240 if (rStyleSet.GetItemState(ATTR_PAGE_HEADERSET,false,&pItem) == SFX_ITEM_SET)
242 SfxItemSet& rSrcSet = ((SvxSetItem*)pItem)->GetItemSet();
243 SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
244 pDestSet->Put(rSrcSet);
245 rStyleSet.Put(SvxSetItem(ATTR_PAGE_HEADERSET,pDestSet));
247 if (rStyleSet.GetItemState(ATTR_PAGE_FOOTERSET,false,&pItem) == SFX_ITEM_SET)
249 SfxItemSet& rSrcSet = ((SvxSetItem*)pItem)->GetItemSet();
250 SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
251 pDestSet->Put(rSrcSet);
252 rStyleSet.Put(SvxSetItem(ATTR_PAGE_FOOTERSET,pDestSet));
255 pStyle = pStylePool->Next();
259 void ScDocShell::LoadStyles( SfxObjectShell &rSource )
261 aDocument.StylesToNames();
263 SfxObjectShell::LoadStyles(rSource);
264 lcl_AdjustPool( GetStyleSheetPool() ); // SetItems anpassen
266 aDocument.UpdStlShtPtrsFrmNms();
268 UpdateAllRowHeights();
270 // Paint
272 PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT );
275 void ScDocShell::LoadStylesArgs( ScDocShell& rSource, bool bReplace, bool bCellStyles, bool bPageStyles )
277 // similar to LoadStyles, but with selectable behavior for XStyleLoader::loadStylesFromURL call
279 if ( !bCellStyles && !bPageStyles ) // nothing to do
280 return;
282 ScStyleSheetPool* pSourcePool = rSource.GetDocument()->GetStyleSheetPool();
283 ScStyleSheetPool* pDestPool = aDocument.GetStyleSheetPool();
285 SfxStyleFamily eFamily = bCellStyles ?
286 ( bPageStyles ? SFX_STYLE_FAMILY_ALL : SFX_STYLE_FAMILY_PARA ) :
287 SFX_STYLE_FAMILY_PAGE;
288 SfxStyleSheetIterator aIter( pSourcePool, eFamily );
289 sal_uInt16 nSourceCount = aIter.Count();
290 if ( nSourceCount == 0 )
291 return; // no source styles
293 boost::scoped_array<ScStylePair> pStyles(new ScStylePair[ nSourceCount ]);
294 sal_uInt16 nFound = 0;
296 // first create all new styles
298 SfxStyleSheetBase* pSourceStyle = aIter.First();
299 while (pSourceStyle)
301 OUString aName = pSourceStyle->GetName();
302 SfxStyleSheetBase* pDestStyle = pDestPool->Find( pSourceStyle->GetName(), pSourceStyle->GetFamily() );
303 if ( pDestStyle )
305 // touch existing styles only if replace flag is set
306 if ( bReplace )
308 pStyles[nFound].pSource = pSourceStyle;
309 pStyles[nFound].pDest = pDestStyle;
310 ++nFound;
313 else
315 pStyles[nFound].pSource = pSourceStyle;
316 pStyles[nFound].pDest = &pDestPool->Make( aName, pSourceStyle->GetFamily(), pSourceStyle->GetMask() );
317 ++nFound;
320 pSourceStyle = aIter.Next();
323 // then copy contents (after inserting all styles, for parent etc.)
325 for ( sal_uInt16 i = 0; i < nFound; ++i )
327 pStyles[i].pDest->GetItemSet().PutExtended(
328 pStyles[i].pSource->GetItemSet(), SFX_ITEM_DONTCARE, SFX_ITEM_DEFAULT);
329 if(pStyles[i].pSource->HasParentSupport())
330 pStyles[i].pDest->SetParent(pStyles[i].pSource->GetParent());
331 // follow is never used
334 lcl_AdjustPool( GetStyleSheetPool() ); // adjust SetItems
335 UpdateAllRowHeights();
336 PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT ); // Paint
340 void ScDocShell::ReconnectDdeLink(SfxObjectShell& rServer)
342 ::sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
343 if (!pLinkManager)
344 return;
346 pLinkManager->ReconnectDdeLink(rServer);
349 void ScDocShell::UpdateLinks()
351 typedef boost::unordered_set<OUString, OUStringHash> StrSetType;
353 sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
354 StrSetType aNames;
356 // nicht mehr benutzte Links raus
358 size_t nCount = pLinkManager->GetLinks().size();
359 for (size_t k=nCount; k>0; )
361 --k;
362 ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[k];
363 if (pBase->ISA(ScTableLink))
365 ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
366 if (pTabLink->IsUsed())
367 aNames.insert(pTabLink->GetFileName());
368 else // nicht mehr benutzt -> loeschen
370 pTabLink->SetAddUndo(true);
371 pLinkManager->Remove(k);
376 // neue Links eintragen
378 SCTAB nTabCount = aDocument.GetTableCount();
379 for (SCTAB i = 0; i < nTabCount; ++i)
381 if (!aDocument.IsLinked(i))
382 continue;
384 OUString aDocName = aDocument.GetLinkDoc(i);
385 OUString aFltName = aDocument.GetLinkFlt(i);
386 OUString aOptions = aDocument.GetLinkOpt(i);
387 sal_uLong nRefresh = aDocument.GetLinkRefreshDelay(i);
388 bool bThere = false;
389 for (SCTAB j = 0; j < i && !bThere; ++j) // im Dokument mehrfach?
391 if (aDocument.IsLinked(j)
392 && aDocument.GetLinkDoc(j) == aDocName
393 && aDocument.GetLinkFlt(j) == aFltName
394 && aDocument.GetLinkOpt(j) == aOptions)
395 // Ignore refresh delay in compare, it should be the
396 // same for identical links and we don't want dupes
397 // if it ain't.
398 bThere = true;
401 if (!bThere) // schon als Filter eingetragen?
403 if (!aNames.insert(aDocName).second)
404 bThere = true;
407 if (!bThere)
409 ScTableLink* pLink = new ScTableLink( this, aDocName, aFltName, aOptions, nRefresh );
410 pLink->SetInCreate(true);
411 pLinkManager->InsertFileLink(*pLink, OBJECT_CLIENT_FILE, aDocName, &aFltName);
412 pLink->Update();
413 pLink->SetInCreate(false);
418 bool ScDocShell::ReloadTabLinks()
420 sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
422 bool bAny = false;
423 size_t nCount = pLinkManager->GetLinks().size();
424 for (size_t i=0; i<nCount; i++ )
426 ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[i];
427 if (pBase->ISA(ScTableLink))
429 ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
430 // pTabLink->SetAddUndo(sal_False); //! Undo's zusammenfassen
432 // Painting only after Update() makes no sense:
433 // ScTableLink::Refresh() will post a Paint only is bDoPaint is true
434 // pTabLink->SetPaint(false); // Paint nur einmal am Ende
435 pTabLink->Update();
436 //pTabLink->SetPaint(true);
437 // pTabLink->SetAddUndo(sal_True);
438 bAny = true;
442 if ( bAny )
444 // Paint nur einmal
445 PostPaint( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB),
446 PAINT_GRID | PAINT_TOP | PAINT_LEFT );
448 SetDocumentModified();
451 return true; //! Fehler erkennen
454 void ScDocShell::SetFormulaOptions( const ScFormulaOptions& rOpt, bool bForLoading )
456 aDocument.SetGrammar( rOpt.GetFormulaSyntax() );
458 // This is nasty because it resets module globals from within a docshell!
459 // For actual damage caused see fdo#82183 where an unconditional
460 // ScGlobal::ResetFunctionList() (without checking GetUseEnglishFuncName())
461 // lead to a crash becasuse the function list was still used by the Formula
462 // Wizard when loading the second document.
463 // Do the stupid stuff only when we're not called while loading a document.
465 /* TODO: bForLoading is a workaround, rather get rid of setting any
466 * globals from per document instances like ScDocShell. */
468 /* XXX this is utter crap, we rely on the options being set here at least
469 * once, for the very first document, empty or loaded. */
470 static bool bInitOnce = true;
472 if (!bForLoading || bInitOnce)
474 bool bForceInit = bInitOnce;
475 bInitOnce = false;
476 if (bForceInit || rOpt.GetUseEnglishFuncName() != SC_MOD()->GetFormulaOptions().GetUseEnglishFuncName())
478 // This needs to be called first since it may re-initialize the entire
479 // opcode map.
480 if (rOpt.GetUseEnglishFuncName())
482 // switch native symbols to English.
483 ScCompiler aComp(NULL, ScAddress());
484 ScCompiler::OpCodeMapPtr xMap = aComp.GetOpCodeMap(::com::sun::star::sheet::FormulaLanguage::ENGLISH);
485 ScCompiler::SetNativeSymbols(xMap);
487 else
488 // re-initialize native symbols with localized function names.
489 ScCompiler::ResetNativeSymbols();
491 // Force re-population of function names for the function wizard, function tip etc.
492 ScGlobal::ResetFunctionList();
495 // Update the separators.
496 ScCompiler::UpdateSeparatorsNative(
497 rOpt.GetFormulaSepArg(), rOpt.GetFormulaSepArrayCol(), rOpt.GetFormulaSepArrayRow());
499 // Global interpreter settings.
500 ScInterpreter::SetGlobalConfig(rOpt.GetCalcConfig());
503 // Per document interpreter settings.
504 SetCalcConfig( rOpt.GetCalcConfig());
507 void ScDocShell::SetCalcConfig( const ScCalcConfig& rConfig )
509 aDocument.SetCalcConfig( rConfig);
512 void ScDocShell::CheckConfigOptions()
514 if (IsConfigOptionsChecked())
515 // no need to check repeatedly.
516 return;
518 OUString aDecSep = ScGlobal::GetpLocaleData()->getNumDecimalSep();
520 ScModule* pScMod = SC_MOD();
521 const ScFormulaOptions& rOpt=pScMod->GetFormulaOptions();
522 OUString aSepArg = rOpt.GetFormulaSepArg();
523 OUString aSepArrRow = rOpt.GetFormulaSepArrayRow();
524 OUString aSepArrCol = rOpt.GetFormulaSepArrayCol();
526 if (aDecSep == aSepArg || aDecSep == aSepArrRow || aDecSep == aSepArrCol)
528 // One of arg separators conflicts with the current decimal
529 // separator. Reset them to default.
530 ScFormulaOptions aNew = rOpt;
531 aNew.ResetFormulaSeparators();
532 SetFormulaOptions(aNew);
533 pScMod->SetFormulaOptions(aNew);
535 // Launch a nice warning dialog to let the users know of this change.
536 ScTabViewShell* pViewShell = GetBestViewShell();
537 if (pViewShell)
539 Window* pParent = pViewShell->GetFrameWin();
540 InfoBox aBox(pParent, ScGlobal::GetRscString(STR_OPTIONS_WARN_SEPARATORS));
541 aBox.Execute();
544 // For now, this is the only option setting that could launch info
545 // dialog. But in the future we may want to implement a nicer
546 // dialog to display a list of warnings in case we have several
547 // pieces of information to display.
550 SetConfigOptionsChecked(true);
553 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */