Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "scitems.hxx"
21 :
22 : #include <svx/pageitem.hxx>
23 : #include <sfx2/linkmgr.hxx>
24 :
25 : #include "docsh.hxx"
26 :
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"
38 :
39 : #include <vcl/msgbox.hxx>
40 :
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 :
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;
55 :
56 : namespace {
57 :
58 : struct ScStylePair
59 : {
60 : SfxStyleSheetBase *pSource;
61 : SfxStyleSheetBase *pDest;
62 : };
63 :
64 : }
65 :
66 : // STATIC DATA -----------------------------------------------------------
67 :
68 : //----------------------------------------------------------------------
69 :
70 : //
71 : // Ole
72 : //
73 :
74 124 : void ScDocShell::SetVisArea( const Rectangle & rVisArea )
75 : {
76 : // with the SnapVisArea call in SetVisAreaOrSize, it's safe to always
77 : // use both the size and position of the VisArea
78 124 : SetVisAreaOrSize( rVisArea, sal_True );
79 124 : }
80 :
81 0 : static void lcl_SetTopRight( Rectangle& rRect, const Point& rPos )
82 : {
83 0 : Size aSize = rRect.GetSize();
84 0 : rRect.Right() = rPos.X();
85 0 : rRect.Left() = rPos.X() - aSize.Width() + 1;
86 0 : rRect.Top() = rPos.Y();
87 0 : rRect.Bottom() = rPos.Y() + aSize.Height() - 1;
88 0 : }
89 :
90 362 : void ScDocShell::SetVisAreaOrSize( const Rectangle& rVisArea, sal_Bool bModifyStart )
91 : {
92 362 : sal_Bool bNegativePage = aDocument.IsNegativePage( aDocument.GetVisibleTab() );
93 :
94 362 : Rectangle aArea = rVisArea;
95 362 : if (bModifyStart)
96 : {
97 : // when loading, don't check for negative values, because the sheet orientation
98 : // might be set later
99 362 : if ( !aDocument.IsImportingXML() )
100 : {
101 287 : if ( ( bNegativePage ? (aArea.Right() > 0) : (aArea.Left() < 0) ) || aArea.Top() < 0 )
102 : {
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.
106 :
107 0 : Point aNewPos( 0, std::max( aArea.Top(), (long) 0 ) );
108 0 : if ( bNegativePage )
109 : {
110 0 : aNewPos.X() = std::min( aArea.Right(), (long) 0 );
111 0 : lcl_SetTopRight( aArea, aNewPos );
112 : }
113 : else
114 : {
115 0 : aNewPos.X() = std::max( aArea.Left(), (long) 0 );
116 0 : aArea.SetPos( aNewPos );
117 : }
118 : }
119 : }
120 : }
121 : else
122 : {
123 0 : Rectangle aOldVisArea = SfxObjectShell::GetVisArea();
124 0 : if ( bNegativePage )
125 0 : lcl_SetTopRight( aArea, aOldVisArea.TopRight() );
126 : else
127 0 : aArea.SetPos( aOldVisArea.TopLeft() );
128 : }
129 :
130 : // hier Position anpassen!
131 :
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 362 : if ( !aDocument.IsImportingXML() )
135 287 : SnapVisArea( aArea );
136 :
137 : //TODO/LATER: it's unclear which IPEnv is used here
138 : /*
139 : SvInPlaceEnvironment* pEnv = GetIPEnv();
140 : if (pEnv)
141 : {
142 : Window* pWin = pEnv->GetEditWin();
143 : pEnv->MakeScale( aArea.GetSize(), MAP_100TH_MM,
144 : pWin->LogicToPixel( aArea.GetSize() ) );
145 : } */
146 :
147 : //TODO/LATER: formerly in SvInplaceObject
148 362 : SfxObjectShell::SetVisArea( aArea );
149 :
150 362 : if (bIsInplace) // Zoom in der InPlace View einstellen
151 : {
152 126 : ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
153 126 : if (pViewSh)
154 : {
155 5 : if (pViewSh->GetViewData()->GetDocShell() == this)
156 0 : pViewSh->UpdateOleZoom();
157 : }
158 : }
159 :
160 362 : if (aDocument.IsEmbedded())
161 : {
162 0 : ScRange aOld;
163 0 : aDocument.GetEmbedded( aOld);
164 0 : aDocument.SetEmbedded( aDocument.GetVisibleTab(), aArea );
165 0 : ScRange aNew;
166 0 : aDocument.GetEmbedded( aNew);
167 0 : if (aOld != aNew)
168 0 : PostPaint(0,0,0,MAXCOL,MAXROW,MAXTAB,PAINT_GRID);
169 :
170 : //TODO/LATER: currently not implemented
171 : //ViewChanged( ASPECT_CONTENT ); // auch im Container anzeigen
172 : }
173 362 : }
174 :
175 222 : sal_Bool ScDocShell::IsOle()
176 : {
177 222 : return (GetCreateMode() == SFX_CREATE_MODE_EMBEDDED);
178 : }
179 :
180 178 : void ScDocShell::UpdateOle( const ScViewData* pViewData, sal_Bool bSnapSize )
181 : {
182 : // wenn's gar nicht Ole ist, kann man sich die Berechnungen sparen
183 : // (VisArea wird dann beim Save wieder zurueckgesetzt)
184 :
185 178 : if (GetCreateMode() == SFX_CREATE_MODE_STANDARD)
186 356 : return;
187 :
188 : OSL_ENSURE(pViewData,"pViewData==0 bei ScDocShell::UpdateOle");
189 :
190 0 : Rectangle aOldArea = SfxObjectShell::GetVisArea();
191 0 : Rectangle aNewArea = aOldArea;
192 :
193 0 : sal_Bool bEmbedded = aDocument.IsEmbedded();
194 0 : if (bEmbedded)
195 0 : aNewArea = aDocument.GetEmbeddedRect();
196 : else
197 : {
198 0 : SCTAB nTab = pViewData->GetTabNo();
199 0 : if ( nTab != aDocument.GetVisibleTab() )
200 0 : aDocument.SetVisibleTab( nTab );
201 :
202 0 : sal_Bool bNegativePage = aDocument.IsNegativePage( nTab );
203 0 : SCCOL nX = pViewData->GetPosX(SC_SPLIT_LEFT);
204 0 : SCROW nY = pViewData->GetPosY(SC_SPLIT_BOTTOM);
205 0 : Rectangle aMMRect = aDocument.GetMMRect( nX,nY, nX,nY, nTab );
206 0 : if (bNegativePage)
207 0 : lcl_SetTopRight( aNewArea, aMMRect.TopRight() );
208 : else
209 0 : aNewArea.SetPos( aMMRect.TopLeft() );
210 0 : if (bSnapSize)
211 0 : SnapVisArea(aNewArea); // uses the new VisibleTab
212 : }
213 :
214 0 : if (aNewArea != aOldArea)
215 0 : SetVisAreaOrSize( aNewArea, true ); // hier muss auch der Start angepasst werden
216 : }
217 :
218 : //
219 : // Style-Krempel fuer Organizer etc.
220 : //
221 :
222 0 : SfxStyleSheetBasePool* ScDocShell::GetStyleSheetPool()
223 : {
224 0 : return (SfxStyleSheetBasePool*)aDocument.GetStyleSheetPool();
225 : }
226 :
227 :
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.
231 :
232 0 : static void lcl_AdjustPool( SfxStyleSheetBasePool* pStylePool )
233 : {
234 0 : pStylePool->SetSearchMask(SFX_STYLE_FAMILY_PAGE, SFXSTYLEBIT_ALL);
235 0 : SfxStyleSheetBase *pStyle = pStylePool->First();
236 0 : while ( pStyle )
237 : {
238 0 : SfxItemSet& rStyleSet = pStyle->GetItemSet();
239 :
240 : const SfxPoolItem* pItem;
241 0 : if (rStyleSet.GetItemState(ATTR_PAGE_HEADERSET,false,&pItem) == SFX_ITEM_SET)
242 : {
243 0 : SfxItemSet& rSrcSet = ((SvxSetItem*)pItem)->GetItemSet();
244 0 : SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
245 0 : pDestSet->Put(rSrcSet);
246 0 : rStyleSet.Put(SvxSetItem(ATTR_PAGE_HEADERSET,pDestSet));
247 : }
248 0 : if (rStyleSet.GetItemState(ATTR_PAGE_FOOTERSET,false,&pItem) == SFX_ITEM_SET)
249 : {
250 0 : SfxItemSet& rSrcSet = ((SvxSetItem*)pItem)->GetItemSet();
251 0 : SfxItemSet* pDestSet = new SfxItemSet(*rStyleSet.GetPool(),rSrcSet.GetRanges());
252 0 : pDestSet->Put(rSrcSet);
253 0 : rStyleSet.Put(SvxSetItem(ATTR_PAGE_FOOTERSET,pDestSet));
254 : }
255 :
256 0 : pStyle = pStylePool->Next();
257 : }
258 0 : }
259 :
260 0 : void ScDocShell::LoadStyles( SfxObjectShell &rSource )
261 : {
262 0 : aDocument.StylesToNames();
263 :
264 0 : SfxObjectShell::LoadStyles(rSource);
265 0 : lcl_AdjustPool( GetStyleSheetPool() ); // SetItems anpassen
266 :
267 0 : aDocument.UpdStlShtPtrsFrmNms();
268 :
269 0 : UpdateAllRowHeights();
270 :
271 : // Paint
272 :
273 0 : PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT );
274 0 : }
275 :
276 0 : void ScDocShell::LoadStylesArgs( ScDocShell& rSource, bool bReplace, bool bCellStyles, bool bPageStyles )
277 : {
278 : // similar to LoadStyles, but with selectable behavior for XStyleLoader::loadStylesFromURL call
279 :
280 0 : if ( !bCellStyles && !bPageStyles ) // nothing to do
281 0 : return;
282 :
283 0 : ScStyleSheetPool* pSourcePool = rSource.GetDocument()->GetStyleSheetPool();
284 0 : ScStyleSheetPool* pDestPool = aDocument.GetStyleSheetPool();
285 :
286 : SfxStyleFamily eFamily = bCellStyles ?
287 : ( bPageStyles ? SFX_STYLE_FAMILY_ALL : SFX_STYLE_FAMILY_PARA ) :
288 0 : SFX_STYLE_FAMILY_PAGE;
289 0 : SfxStyleSheetIterator aIter( pSourcePool, eFamily );
290 0 : sal_uInt16 nSourceCount = aIter.Count();
291 0 : if ( nSourceCount == 0 )
292 0 : return; // no source styles
293 :
294 0 : ScStylePair* pStyles = new ScStylePair[ nSourceCount ];
295 0 : sal_uInt16 nFound = 0;
296 :
297 : // first create all new styles
298 :
299 0 : SfxStyleSheetBase* pSourceStyle = aIter.First();
300 0 : while (pSourceStyle)
301 : {
302 0 : String aName = pSourceStyle->GetName();
303 0 : SfxStyleSheetBase* pDestStyle = pDestPool->Find( pSourceStyle->GetName(), pSourceStyle->GetFamily() );
304 0 : if ( pDestStyle )
305 : {
306 : // touch existing styles only if replace flag is set
307 0 : if ( bReplace )
308 : {
309 0 : pStyles[nFound].pSource = pSourceStyle;
310 0 : pStyles[nFound].pDest = pDestStyle;
311 0 : ++nFound;
312 : }
313 : }
314 : else
315 : {
316 0 : pStyles[nFound].pSource = pSourceStyle;
317 0 : pStyles[nFound].pDest = &pDestPool->Make( aName, pSourceStyle->GetFamily(), pSourceStyle->GetMask() );
318 0 : ++nFound;
319 : }
320 :
321 0 : pSourceStyle = aIter.Next();
322 0 : }
323 :
324 : // then copy contents (after inserting all styles, for parent etc.)
325 :
326 0 : for ( sal_uInt16 i = 0; i < nFound; ++i )
327 : {
328 0 : pStyles[i].pDest->GetItemSet().PutExtended(
329 0 : pStyles[i].pSource->GetItemSet(), SFX_ITEM_DONTCARE, SFX_ITEM_DEFAULT);
330 0 : if(pStyles[i].pSource->HasParentSupport())
331 0 : pStyles[i].pDest->SetParent(pStyles[i].pSource->GetParent());
332 : // follow is never used
333 : }
334 :
335 0 : lcl_AdjustPool( GetStyleSheetPool() ); // adjust SetItems
336 0 : UpdateAllRowHeights();
337 0 : PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID | PAINT_LEFT ); // Paint
338 :
339 0 : delete[] pStyles;
340 : }
341 :
342 :
343 0 : sal_Bool ScDocShell::Insert( SfxObjectShell &rSource,
344 : sal_uInt16 nSourceIdx1, sal_uInt16 nSourceIdx2, sal_uInt16 nSourceIdx3,
345 : sal_uInt16 &nIdx1, sal_uInt16 &nIdx2, sal_uInt16 &nIdx3, sal_uInt16 &rIdxDeleted )
346 : {
347 : sal_Bool bRet = SfxObjectShell::Insert( rSource, nSourceIdx1, nSourceIdx2, nSourceIdx3,
348 0 : nIdx1, nIdx2, nIdx3, rIdxDeleted );
349 0 : if (bRet)
350 0 : lcl_AdjustPool( GetStyleSheetPool() ); // SetItems anpassen
351 :
352 0 : return bRet;
353 : }
354 :
355 228 : void ScDocShell::ReconnectDdeLink(SfxObjectShell& rServer)
356 : {
357 228 : ::sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
358 228 : if (!pLinkManager)
359 228 : return;
360 :
361 228 : pLinkManager->ReconnectDdeLink(rServer);
362 : }
363 :
364 99 : void ScDocShell::UpdateLinks()
365 : {
366 : typedef boost::unordered_set<OUString, OUStringHash> StrSetType;
367 :
368 99 : sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
369 99 : StrSetType aNames;
370 :
371 : // nicht mehr benutzte Links raus
372 :
373 99 : size_t nCount = pLinkManager->GetLinks().size();
374 198 : for (size_t k=nCount; k>0; )
375 : {
376 0 : --k;
377 0 : ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[k];
378 0 : if (pBase->ISA(ScTableLink))
379 : {
380 0 : ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
381 0 : if (pTabLink->IsUsed())
382 0 : aNames.insert(pTabLink->GetFileName());
383 : else // nicht mehr benutzt -> loeschen
384 : {
385 0 : pTabLink->SetAddUndo(sal_True);
386 0 : pLinkManager->Remove(k);
387 : }
388 : }
389 : }
390 :
391 : // neue Links eintragen
392 :
393 99 : SCTAB nTabCount = aDocument.GetTableCount();
394 313 : for (SCTAB i = 0; i < nTabCount; ++i)
395 : {
396 214 : if (!aDocument.IsLinked(i))
397 212 : continue;
398 :
399 2 : OUString aDocName = aDocument.GetLinkDoc(i);
400 4 : OUString aFltName = aDocument.GetLinkFlt(i);
401 4 : OUString aOptions = aDocument.GetLinkOpt(i);
402 2 : sal_uLong nRefresh = aDocument.GetLinkRefreshDelay(i);
403 2 : bool bThere = false;
404 2 : for (SCTAB j = 0; j < i && !bThere; ++j) // im Dokument mehrfach?
405 : {
406 0 : if (aDocument.IsLinked(j)
407 0 : && aDocument.GetLinkDoc(j) == aDocName
408 0 : && aDocument.GetLinkFlt(j) == aFltName
409 0 : && aDocument.GetLinkOpt(j) == aOptions)
410 : // Ignore refresh delay in compare, it should be the
411 : // same for identical links and we don't want dupes
412 : // if it ain't.
413 0 : bThere = true;
414 : }
415 :
416 2 : if (!bThere) // schon als Filter eingetragen?
417 : {
418 2 : if (!aNames.insert(aDocName).second)
419 0 : bThere = true;
420 : }
421 :
422 2 : if (!bThere)
423 : {
424 2 : ScTableLink* pLink = new ScTableLink( this, aDocName, aFltName, aOptions, nRefresh );
425 2 : pLink->SetInCreate(true);
426 2 : pLinkManager->InsertFileLink(*pLink, OBJECT_CLIENT_FILE, aDocName, &aFltName);
427 2 : pLink->Update();
428 2 : pLink->SetInCreate(false);
429 : }
430 101 : }
431 99 : }
432 :
433 0 : sal_Bool ScDocShell::ReloadTabLinks()
434 : {
435 0 : sfx2::LinkManager* pLinkManager = aDocument.GetLinkManager();
436 :
437 0 : bool bAny = false;
438 0 : size_t nCount = pLinkManager->GetLinks().size();
439 0 : for (size_t i=0; i<nCount; i++ )
440 : {
441 0 : ::sfx2::SvBaseLink* pBase = *pLinkManager->GetLinks()[i];
442 0 : if (pBase->ISA(ScTableLink))
443 : {
444 0 : ScTableLink* pTabLink = static_cast<ScTableLink*>(pBase);
445 : // pTabLink->SetAddUndo(sal_False); //! Undo's zusammenfassen
446 :
447 : // Painting only after Update() makes no sense:
448 : // ScTableLink::Refresh() will post a Paint only is bDoPaint is true
449 : // pTabLink->SetPaint(false); // Paint nur einmal am Ende
450 0 : pTabLink->Update();
451 : //pTabLink->SetPaint(true);
452 : // pTabLink->SetAddUndo(sal_True);
453 0 : bAny = true;
454 : }
455 : }
456 :
457 0 : if ( bAny )
458 : {
459 : // Paint nur einmal
460 : PostPaint( ScRange(0,0,0,MAXCOL,MAXROW,MAXTAB),
461 0 : PAINT_GRID | PAINT_TOP | PAINT_LEFT );
462 :
463 0 : SetDocumentModified();
464 : }
465 :
466 0 : return sal_True; //! Fehler erkennen
467 : }
468 :
469 313 : void ScDocShell::SetFormulaOptions(const ScFormulaOptions& rOpt )
470 : {
471 313 : aDocument.SetGrammar( rOpt.GetFormulaSyntax() );
472 :
473 : // This needs to be called first since it may re-initialize the entire
474 : // opcode map.
475 313 : if (rOpt.GetUseEnglishFuncName())
476 : {
477 : // switch native symbols to English.
478 0 : ScCompiler aComp(NULL, ScAddress());
479 0 : ScCompiler::OpCodeMapPtr xMap = aComp.GetOpCodeMap(::com::sun::star::sheet::FormulaLanguage::ENGLISH);
480 0 : ScCompiler::SetNativeSymbols(xMap);
481 : }
482 : else
483 : // re-initialize native symbols with localized function names.
484 313 : ScCompiler::ResetNativeSymbols();
485 :
486 : // Force re-population of function names for the function wizard, function tip etc.
487 313 : ScGlobal::ResetFunctionList();
488 :
489 : // Update the separators.
490 : ScCompiler::UpdateSeparatorsNative(
491 313 : rOpt.GetFormulaSepArg(), rOpt.GetFormulaSepArrayCol(), rOpt.GetFormulaSepArrayRow());
492 :
493 : // Global interpreter settings.
494 313 : ScInterpreter::SetGlobalConfig(rOpt.GetCalcConfig());
495 313 : }
496 :
497 262 : void ScDocShell::CheckConfigOptions()
498 : {
499 262 : if (IsConfigOptionsChecked())
500 : // no need to check repeatedly.
501 326 : return;
502 :
503 198 : OUString aDecSep = ScGlobal::GetpLocaleData()->getNumDecimalSep();
504 :
505 198 : ScModule* pScMod = SC_MOD();
506 198 : const ScFormulaOptions& rOpt=pScMod->GetFormulaOptions();
507 396 : OUString aSepArg = rOpt.GetFormulaSepArg();
508 396 : OUString aSepArrRow = rOpt.GetFormulaSepArrayRow();
509 396 : OUString aSepArrCol = rOpt.GetFormulaSepArrayCol();
510 :
511 198 : if (aDecSep == aSepArg || aDecSep == aSepArrRow || aDecSep == aSepArrCol)
512 : {
513 : // One of arg separators conflicts with the current decimal
514 : // separator. Reset them to default.
515 0 : ScFormulaOptions aNew = rOpt;
516 0 : aNew.ResetFormulaSeparators();
517 0 : SetFormulaOptions(aNew);
518 0 : pScMod->SetFormulaOptions(aNew);
519 :
520 : // Launch a nice warning dialog to let the users know of this change.
521 0 : ScTabViewShell* pViewShell = GetBestViewShell();
522 0 : if (pViewShell)
523 : {
524 0 : Window* pParent = pViewShell->GetFrameWin();
525 0 : InfoBox aBox(pParent, ScGlobal::GetRscString(STR_OPTIONS_WARN_SEPARATORS));
526 0 : aBox.Execute();
527 0 : }
528 :
529 : // For now, this is the only option setting that could launch info
530 : // dialog. But in the future we may want to implement a nicer
531 : // dialog to display a list of warnings in case we have several
532 : // pieces of information to display.
533 : }
534 :
535 396 : SetConfigOptionsChecked(true);
536 93 : }
537 :
538 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|