fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / docshell / docsh6.cxx
blob6d320959f46e80d8166c02562892b9d24570dd48
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 -----------------------------------------------------------
69 // Ole
71 void ScDocShell::SetVisArea( const Rectangle & rVisArea )
73 // with the SnapVisArea call in SetVisAreaOrSize, it's safe to always
74 // use both the size and position of the VisArea
75 SetVisAreaOrSize( rVisArea, true );
78 static void lcl_SetTopRight( Rectangle& rRect, const Point& rPos )
80 Size aSize = rRect.GetSize();
81 rRect.Right() = rPos.X();
82 rRect.Left() = rPos.X() - aSize.Width() + 1;
83 rRect.Top() = rPos.Y();
84 rRect.Bottom() = rPos.Y() + aSize.Height() - 1;
87 void ScDocShell::SetVisAreaOrSize( const Rectangle& rVisArea, bool bModifyStart )
89 bool bNegativePage = aDocument.IsNegativePage( aDocument.GetVisibleTab() );
91 Rectangle aArea = rVisArea;
92 if (bModifyStart)
94 // when loading, don't check for negative values, because the sheet orientation
95 // might be set later
96 if ( !aDocument.IsImportingXML() )
98 if ( ( bNegativePage ? (aArea.Right() > 0) : (aArea.Left() < 0) ) || aArea.Top() < 0 )
100 // VisArea start position can't be negative.
101 // Move the VisArea, otherwise only the upper left position would
102 // be changed in SnapVisArea, and the size would be wrong.
104 Point aNewPos( 0, std::max( aArea.Top(), (long) 0 ) );
105 if ( bNegativePage )
107 aNewPos.X() = std::min( aArea.Right(), (long) 0 );
108 lcl_SetTopRight( aArea, aNewPos );
110 else
112 aNewPos.X() = std::max( aArea.Left(), (long) 0 );
113 aArea.SetPos( aNewPos );
118 else
120 Rectangle aOldVisArea = SfxObjectShell::GetVisArea();
121 if ( bNegativePage )
122 lcl_SetTopRight( aArea, aOldVisArea.TopRight() );
123 else
124 aArea.SetPos( aOldVisArea.TopLeft() );
127 // hier Position anpassen!
129 // when loading an ole object, the VisArea is set from the document's
130 // view settings and must be used as-is (document content may not be complete yet).
131 if ( !aDocument.IsImportingXML() )
132 SnapVisArea( aArea );
134 //TODO/LATER: it's unclear which IPEnv is used here
136 SvInPlaceEnvironment* pEnv = GetIPEnv();
137 if (pEnv)
139 vcl::Window* pWin = pEnv->GetEditWin();
140 pEnv->MakeScale( aArea.GetSize(), MAP_100TH_MM,
141 pWin->LogicToPixel( aArea.GetSize() ) );
142 } */
144 //TODO/LATER: formerly in SvInplaceObject
145 SfxObjectShell::SetVisArea( aArea );
147 if (bIsInplace) // Zoom in der InPlace View einstellen
149 ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
150 if (pViewSh)
152 if (pViewSh->GetViewData().GetDocShell() == this)
153 pViewSh->UpdateOleZoom();
157 if (aDocument.IsEmbedded())
159 ScRange aOld;
160 aDocument.GetEmbedded( aOld);
161 aDocument.SetEmbedded( aDocument.GetVisibleTab(), aArea );
162 ScRange aNew;
163 aDocument.GetEmbedded( aNew);
164 if (aOld != aNew)
165 PostPaint(0,0,0,MAXCOL,MAXROW,MAXTAB,PAINT_GRID);
167 //TODO/LATER: currently not implemented
168 //ViewChanged( ASPECT_CONTENT ); // auch im Container anzeigen
172 bool ScDocShell::IsOle()
174 return (GetCreateMode() == SfxObjectCreateMode::EMBEDDED);
177 void ScDocShell::UpdateOle( const ScViewData* pViewData, bool bSnapSize )
179 // wenn's gar nicht Ole ist, kann man sich die Berechnungen sparen
180 // (VisArea wird dann beim Save wieder zurueckgesetzt)
182 if (GetCreateMode() == SfxObjectCreateMode::STANDARD)
183 return;
185 OSL_ENSURE(pViewData,"pViewData==0 bei ScDocShell::UpdateOle");
187 Rectangle aOldArea = SfxObjectShell::GetVisArea();
188 Rectangle aNewArea = aOldArea;
190 bool bEmbedded = aDocument.IsEmbedded();
191 if (bEmbedded)
192 aNewArea = aDocument.GetEmbeddedRect();
193 else
195 SCTAB nTab = pViewData->GetTabNo();
196 if ( nTab != aDocument.GetVisibleTab() )
197 aDocument.SetVisibleTab( nTab );
199 bool bNegativePage = aDocument.IsNegativePage( nTab );
200 SCCOL nX = pViewData->GetPosX(SC_SPLIT_LEFT);
201 SCROW nY = pViewData->GetPosY(SC_SPLIT_BOTTOM);
202 Rectangle aMMRect = aDocument.GetMMRect( nX,nY, nX,nY, nTab );
203 if (bNegativePage)
204 lcl_SetTopRight( aNewArea, aMMRect.TopRight() );
205 else
206 aNewArea.SetPos( aMMRect.TopLeft() );
207 if (bSnapSize)
208 SnapVisArea(aNewArea); // uses the new VisibleTab
211 if (aNewArea != aOldArea)
212 SetVisAreaOrSize( aNewArea, true ); // hier muss auch der Start angepasst werden
215 // Style-Krempel fuer Organizer etc.
217 SfxStyleSheetBasePool* ScDocShell::GetStyleSheetPool()
219 return (SfxStyleSheetBasePool*)aDocument.GetStyleSheetPool();
222 // nach dem Laden von Vorlagen aus einem anderen Dokment (LoadStyles, Insert)
223 // muessen die SetItems (ATTR_PAGE_HEADERSET, ATTR_PAGE_FOOTERSET) auf den richtigen
224 // Pool umgesetzt werden, bevor der Quell-Pool geloescht wird.
226 static void lcl_AdjustPool( SfxStyleSheetBasePool* pStylePool )
228 pStylePool->SetSearchMask(SFX_STYLE_FAMILY_PAGE, SFXSTYLEBIT_ALL);
229 SfxStyleSheetBase *pStyle = pStylePool->First();
230 while ( pStyle )
232 SfxItemSet& rStyleSet = pStyle->GetItemSet();
234 const SfxPoolItem* pItem;
235 if (rStyleSet.GetItemState(ATTR_PAGE_HEADERSET,false,&pItem) == SfxItemState::SET)
237 const SfxItemSet& rSrcSet = static_cast<const SvxSetItem*>(pItem)->GetItemSet();
238 SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
239 pDestSet->Put(rSrcSet);
240 rStyleSet.Put(SvxSetItem(ATTR_PAGE_HEADERSET,pDestSet));
242 if (rStyleSet.GetItemState(ATTR_PAGE_FOOTERSET,false,&pItem) == SfxItemState::SET)
244 const SfxItemSet& rSrcSet = static_cast<const SvxSetItem*>(pItem)->GetItemSet();
245 SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
246 pDestSet->Put(rSrcSet);
247 rStyleSet.Put(SvxSetItem(ATTR_PAGE_FOOTERSET,pDestSet));
250 pStyle = pStylePool->Next();
254 void ScDocShell::LoadStyles( SfxObjectShell &rSource )
256 aDocument.StylesToNames();
258 SfxObjectShell::LoadStyles(rSource);
259 lcl_AdjustPool( GetStyleSheetPool() ); // SetItems anpassen
261 aDocument.UpdStlShtPtrsFrmNms();
263 UpdateAllRowHeights();
265 // Paint
267 PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT );
270 void ScDocShell::LoadStylesArgs( ScDocShell& rSource, bool bReplace, bool bCellStyles, bool bPageStyles )
272 // similar to LoadStyles, but with selectable behavior for XStyleLoader::loadStylesFromURL call
274 if ( !bCellStyles && !bPageStyles ) // nothing to do
275 return;
277 ScStyleSheetPool* pSourcePool = rSource.GetDocument().GetStyleSheetPool();
278 ScStyleSheetPool* pDestPool = aDocument.GetStyleSheetPool();
280 SfxStyleFamily eFamily = bCellStyles ?
281 ( bPageStyles ? SFX_STYLE_FAMILY_ALL : SFX_STYLE_FAMILY_PARA ) :
282 SFX_STYLE_FAMILY_PAGE;
283 SfxStyleSheetIterator aIter( pSourcePool, eFamily );
284 sal_uInt16 nSourceCount = aIter.Count();
285 if ( nSourceCount == 0 )
286 return; // no source styles
288 boost::scoped_array<ScStylePair> pStyles(new ScStylePair[ nSourceCount ]);
289 sal_uInt16 nFound = 0;
291 // first create all new styles
293 SfxStyleSheetBase* pSourceStyle = aIter.First();
294 while (pSourceStyle)
296 OUString aName = pSourceStyle->GetName();
297 SfxStyleSheetBase* pDestStyle = pDestPool->Find( pSourceStyle->GetName(), pSourceStyle->GetFamily() );
298 if ( pDestStyle )
300 // touch existing styles only if replace flag is set
301 if ( bReplace )
303 pStyles[nFound].pSource = pSourceStyle;
304 pStyles[nFound].pDest = pDestStyle;
305 ++nFound;
308 else
310 pStyles[nFound].pSource = pSourceStyle;
311 pStyles[nFound].pDest = &pDestPool->Make( aName, pSourceStyle->GetFamily(), pSourceStyle->GetMask() );
312 ++nFound;
315 pSourceStyle = aIter.Next();
318 // then copy contents (after inserting all styles, for parent etc.)
320 for ( sal_uInt16 i = 0; i < nFound; ++i )
322 pStyles[i].pDest->GetItemSet().PutExtended(
323 pStyles[i].pSource->GetItemSet(), SfxItemState::DONTCARE, SfxItemState::DEFAULT);
324 if(pStyles[i].pSource->HasParentSupport())
325 pStyles[i].pDest->SetParent(pStyles[i].pSource->GetParent());
326 // follow is never used
329 lcl_AdjustPool( GetStyleSheetPool() ); // adjust SetItems
330 UpdateAllRowHeights();
331 PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT ); // Paint
334 void ScDocShell::ReconnectDdeLink(SfxObjectShell& rServer)
336 ::sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
337 if (!pLinkManager)
338 return;
340 pLinkManager->ReconnectDdeLink(rServer);
343 void ScDocShell::UpdateLinks()
345 typedef std::unordered_set<OUString, OUStringHash> StrSetType;
347 sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
348 StrSetType aNames;
350 // nicht mehr benutzte Links raus
352 size_t nCount = pLinkManager->GetLinks().size();
353 for (size_t k=nCount; k>0; )
355 --k;
356 ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[k];
357 if (pBase->ISA(ScTableLink))
359 ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
360 if (pTabLink->IsUsed())
361 aNames.insert(pTabLink->GetFileName());
362 else // nicht mehr benutzt -> loeschen
364 pTabLink->SetAddUndo(true);
365 pLinkManager->Remove(k);
370 // neue Links eintragen
372 SCTAB nTabCount = aDocument.GetTableCount();
373 for (SCTAB i = 0; i < nTabCount; ++i)
375 if (!aDocument.IsLinked(i))
376 continue;
378 OUString aDocName = aDocument.GetLinkDoc(i);
379 OUString aFltName = aDocument.GetLinkFlt(i);
380 OUString aOptions = aDocument.GetLinkOpt(i);
381 sal_uLong nRefresh = aDocument.GetLinkRefreshDelay(i);
382 bool bThere = false;
383 for (SCTAB j = 0; j < i && !bThere; ++j) // im Dokument mehrfach?
385 if (aDocument.IsLinked(j)
386 && aDocument.GetLinkDoc(j) == aDocName
387 && aDocument.GetLinkFlt(j) == aFltName
388 && aDocument.GetLinkOpt(j) == aOptions)
389 // Ignore refresh delay in compare, it should be the
390 // same for identical links and we don't want dupes
391 // if it ain't.
392 bThere = true;
395 if (!bThere) // schon als Filter eingetragen?
397 if (!aNames.insert(aDocName).second)
398 bThere = true;
401 if (!bThere)
403 ScTableLink* pLink = new ScTableLink( this, aDocName, aFltName, aOptions, nRefresh );
404 pLink->SetInCreate(true);
405 pLinkManager->InsertFileLink(*pLink, OBJECT_CLIENT_FILE, aDocName, &aFltName);
406 pLink->Update();
407 pLink->SetInCreate(false);
412 bool ScDocShell::ReloadTabLinks()
414 sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
416 bool bAny = false;
417 size_t nCount = pLinkManager->GetLinks().size();
418 for (size_t i=0; i<nCount; i++ )
420 ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[i];
421 if (pBase->ISA(ScTableLink))
423 ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
424 // pTabLink->SetAddUndo(sal_False); //! Undo's zusammenfassen
426 // Painting only after Update() makes no sense:
427 // ScTableLink::Refresh() will post a Paint only is bDoPaint is true
428 // pTabLink->SetPaint(false); // Paint nur einmal am Ende
429 pTabLink->Update();
430 //pTabLink->SetPaint(true);
431 // pTabLink->SetAddUndo(sal_True);
432 bAny = true;
436 if ( bAny )
438 // Paint nur einmal
439 PostPaint( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB),
440 PAINT_GRID | PAINT_TOP | PAINT_LEFT );
442 SetDocumentModified();
445 return true; //! Fehler erkennen
448 void ScDocShell::SetFormulaOptions( const ScFormulaOptions& rOpt, bool bForLoading )
450 aDocument.SetGrammar( rOpt.GetFormulaSyntax() );
452 // This is nasty because it resets module globals from within a docshell!
453 // For actual damage caused see fdo#82183 where an unconditional
454 // ScGlobal::ResetFunctionList() (without checking GetUseEnglishFuncName())
455 // lead to a crash becasuse the function list was still used by the Formula
456 // Wizard when loading the second document.
457 // Do the stupid stuff only when we're not called while loading a document.
459 /* TODO: bForLoading is a workaround, rather get rid of setting any
460 * globals from per document instances like ScDocShell. */
462 /* XXX this is utter crap, we rely on the options being set here at least
463 * once, for the very first document, empty or loaded. */
464 static bool bInitOnce = true;
466 if (!bForLoading || bInitOnce)
468 bool bForceInit = bInitOnce;
469 bInitOnce = false;
470 if (bForceInit || rOpt.GetUseEnglishFuncName() != SC_MOD()->GetFormulaOptions().GetUseEnglishFuncName())
472 // This needs to be called first since it may re-initialize the entire
473 // opcode map.
474 if (rOpt.GetUseEnglishFuncName())
476 // switch native symbols to English.
477 ScCompiler aComp(NULL, ScAddress());
478 ScCompiler::OpCodeMapPtr xMap = aComp.GetOpCodeMap(::com::sun::star::sheet::FormulaLanguage::ENGLISH);
479 ScCompiler::SetNativeSymbols(xMap);
481 else
482 // re-initialize native symbols with localized function names.
483 ScCompiler::ResetNativeSymbols();
485 // Force re-population of function names for the function wizard, function tip etc.
486 ScGlobal::ResetFunctionList();
489 // Update the separators.
490 ScCompiler::UpdateSeparatorsNative(
491 rOpt.GetFormulaSepArg(), rOpt.GetFormulaSepArrayCol(), rOpt.GetFormulaSepArrayRow());
493 // Global interpreter settings.
494 ScInterpreter::SetGlobalConfig(rOpt.GetCalcConfig());
497 // Per document interpreter settings.
498 SetCalcConfig( rOpt.GetCalcConfig());
501 void ScDocShell::SetCalcConfig( const ScCalcConfig& rConfig )
503 aDocument.SetCalcConfig( rConfig);
506 void ScDocShell::CheckConfigOptions()
508 if (IsConfigOptionsChecked())
509 // no need to check repeatedly.
510 return;
512 OUString aDecSep = ScGlobal::GetpLocaleData()->getNumDecimalSep();
514 ScModule* pScMod = SC_MOD();
515 const ScFormulaOptions& rOpt=pScMod->GetFormulaOptions();
516 OUString aSepArg = rOpt.GetFormulaSepArg();
517 OUString aSepArrRow = rOpt.GetFormulaSepArrayRow();
518 OUString aSepArrCol = rOpt.GetFormulaSepArrayCol();
520 if (aDecSep == aSepArg || aDecSep == aSepArrRow || aDecSep == aSepArrCol)
522 // One of arg separators conflicts with the current decimal
523 // separator. Reset them to default.
524 ScFormulaOptions aNew = rOpt;
525 aNew.ResetFormulaSeparators();
526 SetFormulaOptions(aNew);
527 pScMod->SetFormulaOptions(aNew);
529 // Launch a nice warning dialog to let the users know of this change.
530 ScTabViewShell* pViewShell = GetBestViewShell();
531 if (pViewShell)
533 vcl::Window* pParent = pViewShell->GetFrameWin();
534 ScopedVclPtrInstance< InfoBox > aBox(pParent, ScGlobal::GetRscString(STR_OPTIONS_WARN_SEPARATORS));
535 aBox->Execute();
538 // For now, this is the only option setting that could launch info
539 // dialog. But in the future we may want to implement a nicer
540 // dialog to display a list of warnings in case we have several
541 // pieces of information to display.
544 SetConfigOptionsChecked(true);
547 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */