Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / docshell / docsh6.cxx
blobca6aeb373f51fa8dfe209709a811822ce2e7e405
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>
46 using ::com::sun::star::beans::XPropertySet;
47 using ::com::sun::star::lang::XMultiServiceFactory;
48 using ::com::sun::star::container::XNameAccess;
49 using ::com::sun::star::util::XChangesBatch;
50 using ::com::sun::star::uno::Any;
51 using ::com::sun::star::uno::Exception;
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::uno::Sequence;
54 using ::com::sun::star::uno::UNO_QUERY_THROW;
56 namespace {
58 struct ScStylePair
60 SfxStyleSheetBase *pSource;
61 SfxStyleSheetBase *pDest;
66 // STATIC DATA -----------------------------------------------------------
68 //----------------------------------------------------------------------
71 // Ole
74 void ScDocShell::SetVisArea( const Rectangle & rVisArea )
76 // with the SnapVisArea call in SetVisAreaOrSize, it's safe to always
77 // use both the size and position of the VisArea
78 SetVisAreaOrSize( rVisArea, sal_True );
81 static void lcl_SetTopRight( Rectangle& rRect, const Point& rPos )
83 Size aSize = rRect.GetSize();
84 rRect.Right() = rPos.X();
85 rRect.Left() = rPos.X() - aSize.Width() + 1;
86 rRect.Top() = rPos.Y();
87 rRect.Bottom() = rPos.Y() + aSize.Height() - 1;
90 void ScDocShell::SetVisAreaOrSize( const Rectangle& rVisArea, sal_Bool bModifyStart )
92 sal_Bool bNegativePage = aDocument.IsNegativePage( aDocument.GetVisibleTab() );
94 Rectangle aArea = rVisArea;
95 if (bModifyStart)
97 // when loading, don't check for negative values, because the sheet orientation
98 // might be set later
99 if ( !aDocument.IsImportingXML() )
101 if ( ( bNegativePage ? (aArea.Right() > 0) : (aArea.Left() < 0) ) || aArea.Top() < 0 )
103 // VisArea start position can't be negative.
104 // Move the VisArea, otherwise only the upper left position would
105 // be changed in SnapVisArea, and the size would be wrong.
107 Point aNewPos( 0, std::max( aArea.Top(), (long) 0 ) );
108 if ( bNegativePage )
110 aNewPos.X() = std::min( aArea.Right(), (long) 0 );
111 lcl_SetTopRight( aArea, aNewPos );
113 else
115 aNewPos.X() = std::max( aArea.Left(), (long) 0 );
116 aArea.SetPos( aNewPos );
121 else
123 Rectangle aOldVisArea = SfxObjectShell::GetVisArea();
124 if ( bNegativePage )
125 lcl_SetTopRight( aArea, aOldVisArea.TopRight() );
126 else
127 aArea.SetPos( aOldVisArea.TopLeft() );
130 // hier Position anpassen!
132 // when loading an ole object, the VisArea is set from the document's
133 // view settings and must be used as-is (document content may not be complete yet).
134 if ( !aDocument.IsImportingXML() )
135 SnapVisArea( aArea );
137 //TODO/LATER: it's unclear which IPEnv is used here
139 SvInPlaceEnvironment* pEnv = GetIPEnv();
140 if (pEnv)
142 Window* pWin = pEnv->GetEditWin();
143 pEnv->MakeScale( aArea.GetSize(), MAP_100TH_MM,
144 pWin->LogicToPixel( aArea.GetSize() ) );
145 } */
147 //TODO/LATER: formerly in SvInplaceObject
148 SfxObjectShell::SetVisArea( aArea );
150 if (bIsInplace) // Zoom in der InPlace View einstellen
152 ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
153 if (pViewSh)
155 if (pViewSh->GetViewData()->GetDocShell() == this)
156 pViewSh->UpdateOleZoom();
160 if (aDocument.IsEmbedded())
162 ScRange aOld;
163 aDocument.GetEmbedded( aOld);
164 aDocument.SetEmbedded( aDocument.GetVisibleTab(), aArea );
165 ScRange aNew;
166 aDocument.GetEmbedded( aNew);
167 if (aOld != aNew)
168 PostPaint(0,0,0,MAXCOL,MAXROW,MAXTAB,PAINT_GRID);
170 //TODO/LATER: currently not implemented
171 //ViewChanged( ASPECT_CONTENT ); // auch im Container anzeigen
175 sal_Bool ScDocShell::IsOle()
177 return (GetCreateMode() == SFX_CREATE_MODE_EMBEDDED);
180 void ScDocShell::UpdateOle( const ScViewData* pViewData, sal_Bool bSnapSize )
182 // wenn's gar nicht Ole ist, kann man sich die Berechnungen sparen
183 // (VisArea wird dann beim Save wieder zurueckgesetzt)
185 if (GetCreateMode() == SFX_CREATE_MODE_STANDARD)
186 return;
188 OSL_ENSURE(pViewData,"pViewData==0 bei ScDocShell::UpdateOle");
190 Rectangle aOldArea = SfxObjectShell::GetVisArea();
191 Rectangle aNewArea = aOldArea;
193 sal_Bool bEmbedded = aDocument.IsEmbedded();
194 if (bEmbedded)
195 aNewArea = aDocument.GetEmbeddedRect();
196 else
198 SCTAB nTab = pViewData->GetTabNo();
199 if ( nTab != aDocument.GetVisibleTab() )
200 aDocument.SetVisibleTab( nTab );
202 sal_Bool bNegativePage = aDocument.IsNegativePage( nTab );
203 SCCOL nX = pViewData->GetPosX(SC_SPLIT_LEFT);
204 SCROW nY = pViewData->GetPosY(SC_SPLIT_BOTTOM);
205 Rectangle aMMRect = aDocument.GetMMRect( nX,nY, nX,nY, nTab );
206 if (bNegativePage)
207 lcl_SetTopRight( aNewArea, aMMRect.TopRight() );
208 else
209 aNewArea.SetPos( aMMRect.TopLeft() );
210 if (bSnapSize)
211 SnapVisArea(aNewArea); // uses the new VisibleTab
214 if (aNewArea != aOldArea)
215 SetVisAreaOrSize( aNewArea, true ); // hier muss auch der Start angepasst werden
219 // Style-Krempel fuer Organizer etc.
222 SfxStyleSheetBasePool* ScDocShell::GetStyleSheetPool()
224 return (SfxStyleSheetBasePool*)aDocument.GetStyleSheetPool();
228 // nach dem Laden von Vorlagen aus einem anderen Dokment (LoadStyles, Insert)
229 // muessen die SetItems (ATTR_PAGE_HEADERSET, ATTR_PAGE_FOOTERSET) auf den richtigen
230 // Pool umgesetzt werden, bevor der Quell-Pool geloescht wird.
232 static void lcl_AdjustPool( SfxStyleSheetBasePool* pStylePool )
234 pStylePool->SetSearchMask(SFX_STYLE_FAMILY_PAGE, SFXSTYLEBIT_ALL);
235 SfxStyleSheetBase *pStyle = pStylePool->First();
236 while ( pStyle )
238 SfxItemSet& rStyleSet = pStyle->GetItemSet();
240 const SfxPoolItem* pItem;
241 if (rStyleSet.GetItemState(ATTR_PAGE_HEADERSET,false,&pItem) == SFX_ITEM_SET)
243 SfxItemSet& rSrcSet = ((SvxSetItem*)pItem)->GetItemSet();
244 SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
245 pDestSet->Put(rSrcSet);
246 rStyleSet.Put(SvxSetItem(ATTR_PAGE_HEADERSET,pDestSet));
248 if (rStyleSet.GetItemState(ATTR_PAGE_FOOTERSET,false,&pItem) == SFX_ITEM_SET)
250 SfxItemSet& rSrcSet = ((SvxSetItem*)pItem)->GetItemSet();
251 SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
252 pDestSet->Put(rSrcSet);
253 rStyleSet.Put(SvxSetItem(ATTR_PAGE_FOOTERSET,pDestSet));
256 pStyle = pStylePool->Next();
260 void ScDocShell::LoadStyles( SfxObjectShell &rSource )
262 aDocument.StylesToNames();
264 SfxObjectShell::LoadStyles(rSource);
265 lcl_AdjustPool( GetStyleSheetPool() ); // SetItems anpassen
267 aDocument.UpdStlShtPtrsFrmNms();
269 UpdateAllRowHeights();
271 // Paint
273 PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT );
276 void ScDocShell::LoadStylesArgs( ScDocShell& rSource, bool bReplace, bool bCellStyles, bool bPageStyles )
278 // similar to LoadStyles, but with selectable behavior for XStyleLoader::loadStylesFromURL call
280 if ( !bCellStyles && !bPageStyles ) // nothing to do
281 return;
283 ScStyleSheetPool* pSourcePool = rSource.GetDocument()->GetStyleSheetPool();
284 ScStyleSheetPool* pDestPool = aDocument.GetStyleSheetPool();
286 SfxStyleFamily eFamily = bCellStyles ?
287 ( bPageStyles ? SFX_STYLE_FAMILY_ALL : SFX_STYLE_FAMILY_PARA ) :
288 SFX_STYLE_FAMILY_PAGE;
289 SfxStyleSheetIterator aIter( pSourcePool, eFamily );
290 sal_uInt16 nSourceCount = aIter.Count();
291 if ( nSourceCount == 0 )
292 return; // no source styles
294 ScStylePair* pStyles = new ScStylePair[ nSourceCount ];
295 sal_uInt16 nFound = 0;
297 // first create all new styles
299 SfxStyleSheetBase* pSourceStyle = aIter.First();
300 while (pSourceStyle)
302 OUString aName = pSourceStyle->GetName();
303 SfxStyleSheetBase* pDestStyle = pDestPool->Find( pSourceStyle->GetName(), pSourceStyle->GetFamily() );
304 if ( pDestStyle )
306 // touch existing styles only if replace flag is set
307 if ( bReplace )
309 pStyles[nFound].pSource = pSourceStyle;
310 pStyles[nFound].pDest = pDestStyle;
311 ++nFound;
314 else
316 pStyles[nFound].pSource = pSourceStyle;
317 pStyles[nFound].pDest = &pDestPool->Make( aName, pSourceStyle->GetFamily(), pSourceStyle->GetMask() );
318 ++nFound;
321 pSourceStyle = aIter.Next();
324 // then copy contents (after inserting all styles, for parent etc.)
326 for ( sal_uInt16 i = 0; i < nFound; ++i )
328 pStyles[i].pDest->GetItemSet().PutExtended(
329 pStyles[i].pSource->GetItemSet(), SFX_ITEM_DONTCARE, SFX_ITEM_DEFAULT);
330 if(pStyles[i].pSource->HasParentSupport())
331 pStyles[i].pDest->SetParent(pStyles[i].pSource->GetParent());
332 // follow is never used
335 lcl_AdjustPool( GetStyleSheetPool() ); // adjust SetItems
336 UpdateAllRowHeights();
337 PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT ); // Paint
339 delete[] pStyles;
343 void ScDocShell::ReconnectDdeLink(SfxObjectShell& rServer)
345 ::sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
346 if (!pLinkManager)
347 return;
349 pLinkManager->ReconnectDdeLink(rServer);
352 void ScDocShell::UpdateLinks()
354 typedef boost::unordered_set<OUString, OUStringHash> StrSetType;
356 sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
357 StrSetType aNames;
359 // nicht mehr benutzte Links raus
361 size_t nCount = pLinkManager->GetLinks().size();
362 for (size_t k=nCount; k>0; )
364 --k;
365 ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[k];
366 if (pBase->ISA(ScTableLink))
368 ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
369 if (pTabLink->IsUsed())
370 aNames.insert(pTabLink->GetFileName());
371 else // nicht mehr benutzt -> loeschen
373 pTabLink->SetAddUndo(sal_True);
374 pLinkManager->Remove(k);
379 // neue Links eintragen
381 SCTAB nTabCount = aDocument.GetTableCount();
382 for (SCTAB i = 0; i < nTabCount; ++i)
384 if (!aDocument.IsLinked(i))
385 continue;
387 OUString aDocName = aDocument.GetLinkDoc(i);
388 OUString aFltName = aDocument.GetLinkFlt(i);
389 OUString aOptions = aDocument.GetLinkOpt(i);
390 sal_uLong nRefresh = aDocument.GetLinkRefreshDelay(i);
391 bool bThere = false;
392 for (SCTAB j = 0; j < i && !bThere; ++j) // im Dokument mehrfach?
394 if (aDocument.IsLinked(j)
395 && aDocument.GetLinkDoc(j) == aDocName
396 && aDocument.GetLinkFlt(j) == aFltName
397 && aDocument.GetLinkOpt(j) == aOptions)
398 // Ignore refresh delay in compare, it should be the
399 // same for identical links and we don't want dupes
400 // if it ain't.
401 bThere = true;
404 if (!bThere) // schon als Filter eingetragen?
406 if (!aNames.insert(aDocName).second)
407 bThere = true;
410 if (!bThere)
412 ScTableLink* pLink = new ScTableLink( this, aDocName, aFltName, aOptions, nRefresh );
413 pLink->SetInCreate(true);
414 pLinkManager->InsertFileLink(*pLink, OBJECT_CLIENT_FILE, aDocName, &aFltName);
415 pLink->Update();
416 pLink->SetInCreate(false);
421 sal_Bool ScDocShell::ReloadTabLinks()
423 sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
425 bool bAny = false;
426 size_t nCount = pLinkManager->GetLinks().size();
427 for (size_t i=0; i<nCount; i++ )
429 ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[i];
430 if (pBase->ISA(ScTableLink))
432 ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
433 // pTabLink->SetAddUndo(sal_False); //! Undo's zusammenfassen
435 // Painting only after Update() makes no sense:
436 // ScTableLink::Refresh() will post a Paint only is bDoPaint is true
437 // pTabLink->SetPaint(false); // Paint nur einmal am Ende
438 pTabLink->Update();
439 //pTabLink->SetPaint(true);
440 // pTabLink->SetAddUndo(sal_True);
441 bAny = true;
445 if ( bAny )
447 // Paint nur einmal
448 PostPaint( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB),
449 PAINT_GRID | PAINT_TOP | PAINT_LEFT );
451 SetDocumentModified();
454 return sal_True; //! Fehler erkennen
457 void ScDocShell::SetFormulaOptions(const ScFormulaOptions& rOpt )
459 aDocument.SetGrammar( rOpt.GetFormulaSyntax() );
461 // This needs to be called first since it may re-initialize the entire
462 // opcode map.
463 if (rOpt.GetUseEnglishFuncName())
465 // switch native symbols to English.
466 ScCompiler aComp(NULL, ScAddress());
467 ScCompiler::OpCodeMapPtr xMap = aComp.GetOpCodeMap(::com::sun::star::sheet::FormulaLanguage::ENGLISH);
468 ScCompiler::SetNativeSymbols(xMap);
470 else
471 // re-initialize native symbols with localized function names.
472 ScCompiler::ResetNativeSymbols();
474 // Force re-population of function names for the function wizard, function tip etc.
475 ScGlobal::ResetFunctionList();
477 // Update the separators.
478 ScCompiler::UpdateSeparatorsNative(
479 rOpt.GetFormulaSepArg(), rOpt.GetFormulaSepArrayCol(), rOpt.GetFormulaSepArrayRow());
481 // Global interpreter settings.
482 ScInterpreter::SetGlobalConfig(rOpt.GetCalcConfig());
485 void ScDocShell::CheckConfigOptions()
487 if (IsConfigOptionsChecked())
488 // no need to check repeatedly.
489 return;
491 OUString aDecSep = ScGlobal::GetpLocaleData()->getNumDecimalSep();
493 ScModule* pScMod = SC_MOD();
494 const ScFormulaOptions& rOpt=pScMod->GetFormulaOptions();
495 OUString aSepArg = rOpt.GetFormulaSepArg();
496 OUString aSepArrRow = rOpt.GetFormulaSepArrayRow();
497 OUString aSepArrCol = rOpt.GetFormulaSepArrayCol();
499 if (aDecSep == aSepArg || aDecSep == aSepArrRow || aDecSep == aSepArrCol)
501 // One of arg separators conflicts with the current decimal
502 // separator. Reset them to default.
503 ScFormulaOptions aNew = rOpt;
504 aNew.ResetFormulaSeparators();
505 SetFormulaOptions(aNew);
506 pScMod->SetFormulaOptions(aNew);
508 // Launch a nice warning dialog to let the users know of this change.
509 ScTabViewShell* pViewShell = GetBestViewShell();
510 if (pViewShell)
512 Window* pParent = pViewShell->GetFrameWin();
513 InfoBox aBox(pParent, ScGlobal::GetRscString(STR_OPTIONS_WARN_SEPARATORS));
514 aBox.Execute();
517 // For now, this is the only option setting that could launch info
518 // dialog. But in the future we may want to implement a nicer
519 // dialog to display a list of warnings in case we have several
520 // pieces of information to display.
523 SetConfigOptionsChecked(true);
526 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */