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 : #include <vcl/msgbox.hxx>
22 : #include <vcl/waitobj.hxx>
23 : #include <sfx2/app.hxx>
24 : #include <sfx2/bindings.hxx>
25 :
26 : #include <com/sun/star/script/vba/XVBACompatibility.hpp>
27 :
28 : #include "docsh.hxx"
29 : #include "global.hxx"
30 : #include "globstr.hrc"
31 : #include "globalnames.hxx"
32 : #include "undodat.hxx"
33 : #include "undotab.hxx"
34 : #include "undoblk.hxx"
35 : #include "dpobject.hxx"
36 : #include "dpshttab.hxx"
37 : #include "dbdocfun.hxx"
38 : #include "consoli.hxx"
39 : #include "dbdata.hxx"
40 : #include "progress.hxx"
41 : #include "olinetab.hxx"
42 : #include "patattr.hxx"
43 : #include "attrib.hxx"
44 : #include "docpool.hxx"
45 : #include "uiitems.hxx"
46 : #include "sc.hrc"
47 : #include "waitoff.hxx"
48 : #include "sizedev.hxx"
49 : #include "clipparam.hxx"
50 :
51 : // defined in docfunc.cxx
52 : void VBA_InsertModule( ScDocument& rDoc, SCTAB nTab, const OUString& sModuleName, const OUString& sModuleSource );
53 :
54 : using com::sun::star::script::XLibraryContainer;
55 : using com::sun::star::script::vba::XVBACompatibility;
56 : using com::sun::star::container::XNameContainer;
57 : using com::sun::star::uno::Reference;
58 : using com::sun::star::uno::UNO_QUERY;
59 :
60 : using ::std::auto_ptr;
61 : using ::std::vector;
62 :
63 : // ---------------------------------------------------------------------------
64 :
65 : //
66 : // former viewfunc/dbfunc methods
67 : //
68 :
69 0 : void ScDocShell::ErrorMessage( sal_uInt16 nGlobStrId )
70 : {
71 : //! StopMarking an der (aktiven) View?
72 :
73 0 : Window* pParent = GetActiveDialogParent();
74 0 : ScWaitCursorOff aWaitOff( pParent );
75 0 : sal_Bool bFocus = pParent && pParent->HasFocus();
76 :
77 0 : if(nGlobStrId==STR_PROTECTIONERR)
78 : {
79 0 : if(IsReadOnly())
80 : {
81 0 : nGlobStrId=STR_READONLYERR;
82 : }
83 : }
84 :
85 0 : InfoBox aBox( pParent, ScGlobal::GetRscString( nGlobStrId ) );
86 0 : aBox.Execute();
87 0 : if (bFocus)
88 0 : pParent->GrabFocus();
89 0 : }
90 :
91 642 : sal_Bool ScDocShell::IsEditable() const
92 : {
93 : // import into read-only document is possible - must be extended if other filters use api
94 : // #i108547# MSOOXML filter uses "IsChangeReadOnlyEnabled" property
95 :
96 642 : return !IsReadOnly() || aDocument.IsImportingXML() || aDocument.IsChangeReadOnlyEnabled();
97 : }
98 :
99 2 : void ScDocShell::DBAreaDeleted( SCTAB nTab, SCCOL nX1, SCROW nY1, SCCOL nX2, SCROW /* nY2 */ )
100 : {
101 2 : ScDocShellModificator aModificator( *this );
102 2 : aDocument.RemoveFlagsTab( nX1, nY1, nX2, nY1, nTab, SC_MF_AUTO );
103 2 : PostPaint( nX1, nY1, nTab, nX2, nY1, nTab, PAINT_GRID );
104 : // No SetDocumentModified, as the unnamed database range might have to be restored later.
105 : // The UNO hint is broadcast directly instead, to keep UNO objects in valid state.
106 2 : aDocument.BroadcastUno( SfxSimpleHint( SFX_HINT_DATACHANGED ) );
107 2 : }
108 :
109 20 : ScDBData* ScDocShell::GetDBData( const ScRange& rMarked, ScGetDBMode eMode, ScGetDBSelection eSel )
110 : {
111 20 : SCCOL nCol = rMarked.aStart.Col();
112 20 : SCROW nRow = rMarked.aStart.Row();
113 20 : SCTAB nTab = rMarked.aStart.Tab();
114 :
115 20 : SCCOL nStartCol = nCol;
116 20 : SCROW nStartRow = nRow;
117 20 : SCTAB nStartTab = nTab;
118 20 : SCCOL nEndCol = rMarked.aEnd.Col();
119 20 : SCROW nEndRow = rMarked.aEnd.Row();
120 20 : SCTAB nEndTab = rMarked.aEnd.Tab();
121 : // Nicht einfach GetDBAtCursor: Der zusammenhaengende Datenbereich
122 : // fuer "unbenannt" (GetDataArea) kann neben dem Cursor legen, also muss auch ein
123 : // benannter DB-Bereich dort gesucht werden.
124 20 : ScDBCollection* pColl = aDocument.GetDBCollection();
125 20 : ScDBData* pData = aDocument.GetDBAtArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow );
126 20 : if (!pData && pColl)
127 13 : pData = pColl->GetDBNearCursor(nCol, nRow, nTab );
128 :
129 40 : sal_Bool bSelected = ( eSel == SC_DBSEL_FORCE_MARK ||
130 20 : (rMarked.aStart != rMarked.aEnd && eSel != SC_DBSEL_ROW_DOWN) );
131 20 : bool bOnlyDown = (!bSelected && eSel == SC_DBSEL_ROW_DOWN && rMarked.aStart.Row() == rMarked.aEnd.Row());
132 :
133 20 : sal_Bool bUseThis = false;
134 20 : if (pData)
135 : {
136 : // Bereich nehmen, wenn nichts anderes markiert
137 :
138 : SCTAB nDummy;
139 : SCCOL nOldCol1;
140 : SCROW nOldRow1;
141 : SCCOL nOldCol2;
142 : SCROW nOldRow2;
143 10 : pData->GetArea( nDummy, nOldCol1,nOldRow1, nOldCol2,nOldRow2 );
144 10 : sal_Bool bIsNoName = ( pData->GetName() == STR_DB_LOCAL_NONAME );
145 :
146 10 : if (!bSelected)
147 : {
148 0 : bUseThis = sal_True;
149 0 : if ( bIsNoName && eMode == SC_DB_MAKE )
150 : {
151 : // If nothing marked or only one row marked, adapt
152 : // "unbenannt"/"unnamed" to contiguous area.
153 0 : nStartCol = nCol;
154 0 : nStartRow = nRow;
155 0 : if (bOnlyDown)
156 : {
157 0 : nEndCol = rMarked.aEnd.Col();
158 0 : nEndRow = rMarked.aEnd.Row();
159 : }
160 : else
161 : {
162 0 : nEndCol = nStartCol;
163 0 : nEndRow = nStartRow;
164 : }
165 0 : aDocument.GetDataArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow, false, bOnlyDown );
166 0 : if ( nOldCol1 != nStartCol || nOldCol2 != nEndCol || nOldRow1 != nStartRow )
167 0 : bUseThis = false; // passt gar nicht
168 0 : else if ( nOldRow2 != nEndRow )
169 : {
170 : // Bereich auf neue End-Zeile erweitern
171 0 : pData->SetArea( nTab, nOldCol1,nOldRow1, nOldCol2,nEndRow );
172 : }
173 : }
174 : }
175 : else
176 : {
177 20 : if ( nOldCol1 == nStartCol && nOldRow1 == nStartRow &&
178 19 : nOldCol2 == nEndCol && nOldRow2 == nEndRow ) // genau markiert?
179 7 : bUseThis = sal_True;
180 : else
181 3 : bUseThis = false; // immer Markierung nehmen (Bug 11964)
182 : }
183 :
184 : // fuer Import nie "unbenannt" nehmen
185 :
186 10 : if ( bUseThis && eMode == SC_DB_IMPORT && bIsNoName )
187 0 : bUseThis = false;
188 : }
189 :
190 20 : if ( bUseThis )
191 : {
192 7 : pData->GetArea( nStartTab, nStartCol,nStartRow, nEndCol,nEndRow );
193 7 : nEndTab = nStartTab;
194 : }
195 13 : else if ( eMode == SC_DB_OLD )
196 : {
197 5 : pData = NULL; // nichts gefunden
198 5 : nStartCol = nEndCol = nCol;
199 5 : nStartRow = nEndRow = nRow;
200 5 : nStartTab = nEndTab = nTab;
201 : }
202 : else
203 : {
204 8 : if ( !bSelected )
205 : { // zusammenhaengender Bereich
206 0 : nStartCol = nCol;
207 0 : nStartRow = nRow;
208 0 : if (bOnlyDown)
209 : {
210 0 : nEndCol = rMarked.aEnd.Col();
211 0 : nEndRow = rMarked.aEnd.Row();
212 : }
213 : else
214 : {
215 0 : nEndCol = nStartCol;
216 0 : nEndRow = nStartRow;
217 : }
218 0 : aDocument.GetDataArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow, false, bOnlyDown );
219 : }
220 :
221 8 : sal_Bool bHasHeader = aDocument.HasColHeader( nStartCol,nStartRow, nEndCol,nEndRow, nTab );
222 :
223 8 : ScDBData* pNoNameData = aDocument.GetAnonymousDBData(nTab);
224 8 : if ( eMode != SC_DB_IMPORT && pNoNameData)
225 : {
226 :
227 2 : if ( !pOldAutoDBRange )
228 : {
229 : // store the old unnamed database range with its settings for undo
230 : // (store at the first change, get the state before all changes)
231 2 : pOldAutoDBRange = new ScDBData( *pNoNameData );
232 : }
233 :
234 : SCCOL nOldX1; // alten Bereich sauber wegnehmen
235 : SCROW nOldY1; //! (UNDO ???)
236 : SCCOL nOldX2;
237 : SCROW nOldY2;
238 : SCTAB nOldTab;
239 2 : pNoNameData->GetArea( nOldTab, nOldX1, nOldY1, nOldX2, nOldY2 );
240 2 : DBAreaDeleted( nOldTab, nOldX1, nOldY1, nOldX2, nOldY2 );
241 :
242 2 : pNoNameData->SetSortParam( ScSortParam() ); // Parameter zuruecksetzen
243 2 : pNoNameData->SetQueryParam( ScQueryParam() );
244 2 : pNoNameData->SetSubTotalParam( ScSubTotalParam() );
245 :
246 2 : pNoNameData->SetArea( nTab, nStartCol,nStartRow, nEndCol,nEndRow ); // neu setzen
247 2 : pNoNameData->SetByRow( sal_True );
248 2 : pNoNameData->SetHeader( bHasHeader );
249 2 : pNoNameData->SetAutoFilter( false );
250 : }
251 : else
252 : {
253 6 : ScDBCollection* pUndoColl = NULL;
254 :
255 6 : String aNewName;
256 6 : if (eMode==SC_DB_IMPORT)
257 : {
258 0 : aDocument.CompileDBFormula( sal_True ); // CreateFormulaString
259 0 : pUndoColl = new ScDBCollection( *pColl ); // Undo fuer Import1-Bereich
260 :
261 0 : String aImport = ScGlobal::GetRscString( STR_DBNAME_IMPORT );
262 0 : long nCount = 0;
263 0 : const ScDBData* pDummy = NULL;
264 0 : ScDBCollection::NamedDBs& rDBs = pColl->getNamedDBs();
265 0 : do
266 : {
267 0 : ++nCount;
268 0 : aNewName = aImport;
269 0 : aNewName += OUString::number( nCount );
270 0 : pDummy = rDBs.findByUpperName(ScGlobal::pCharClass->uppercase(aNewName));
271 : }
272 : while (pDummy);
273 : pNoNameData = new ScDBData( aNewName, nTab,
274 : nStartCol,nStartRow, nEndCol,nEndRow,
275 0 : sal_True, bHasHeader );
276 0 : rDBs.insert(pNoNameData);
277 : }
278 : else
279 : {
280 6 : aNewName = OUString(STR_DB_LOCAL_NONAME);
281 : pNoNameData = new ScDBData(aNewName , nTab,
282 : nStartCol,nStartRow, nEndCol,nEndRow,
283 6 : sal_True, bHasHeader );
284 6 : aDocument.SetAnonymousDBData(nTab, pNoNameData);
285 : }
286 :
287 :
288 :
289 6 : if ( pUndoColl )
290 : {
291 0 : aDocument.CompileDBFormula( false ); // CompileFormulaString
292 :
293 0 : ScDBCollection* pRedoColl = new ScDBCollection( *pColl );
294 0 : GetUndoManager()->AddUndoAction( new ScUndoDBData( this, pUndoColl, pRedoColl ) );
295 : }
296 :
297 : // neuen Bereich am Sba anmelden nicht mehr noetig
298 :
299 : // "Import1" etc am Navigator bekanntmachen
300 6 : if (eMode==SC_DB_IMPORT)
301 0 : SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) );
302 : }
303 8 : pData = pNoNameData;
304 : }
305 :
306 20 : return pData;
307 : }
308 :
309 0 : ScDBData* ScDocShell::GetAnonymousDBData(const ScRange& rRange)
310 : {
311 : bool bHasHeader = aDocument.HasColHeader(
312 0 : rRange.aStart.Col(), rRange.aStart.Row(), rRange.aEnd.Col(), rRange.aEnd.Row(), rRange.aStart.Tab());
313 :
314 0 : ScDBCollection* pColl = aDocument.GetDBCollection();
315 0 : if (!pColl)
316 0 : return NULL;
317 :
318 0 : ScDBData* pData = pColl->getAnonDBs().getByRange(rRange);
319 0 : if (!pData)
320 0 : return NULL;
321 :
322 0 : pData->SetHeader(bHasHeader);
323 0 : return pData;
324 : }
325 :
326 10 : ScDBData* ScDocShell::GetOldAutoDBRange()
327 : {
328 10 : ScDBData* pRet = pOldAutoDBRange;
329 10 : pOldAutoDBRange = NULL;
330 10 : return pRet; // has to be deleted by caller!
331 : }
332 :
333 0 : void ScDocShell::CancelAutoDBRange()
334 : {
335 : // called when dialog is cancelled
336 : //moggi:TODO
337 0 : if ( pOldAutoDBRange )
338 : {
339 0 : SCTAB nTab = GetCurTab();
340 0 : ScDBData* pDBData = aDocument.GetAnonymousDBData(nTab);
341 0 : if ( pDBData )
342 : {
343 : SCCOL nRangeX1;
344 : SCROW nRangeY1;
345 : SCCOL nRangeX2;
346 : SCROW nRangeY2;
347 : SCTAB nRangeTab;
348 0 : pDBData->GetArea( nRangeTab, nRangeX1, nRangeY1, nRangeX2, nRangeY2 );
349 0 : DBAreaDeleted( nRangeTab, nRangeX1, nRangeY1, nRangeX2, nRangeY2 );
350 :
351 0 : *pDBData = *pOldAutoDBRange; // restore old settings
352 :
353 0 : if ( pOldAutoDBRange->HasAutoFilter() )
354 : {
355 : // restore AutoFilter buttons
356 0 : pOldAutoDBRange->GetArea( nRangeTab, nRangeX1, nRangeY1, nRangeX2, nRangeY2 );
357 0 : aDocument.ApplyFlagsTab( nRangeX1, nRangeY1, nRangeX2, nRangeY1, nRangeTab, SC_MF_AUTO );
358 0 : PostPaint( nRangeX1, nRangeY1, nRangeTab, nRangeX2, nRangeY1, nRangeTab, PAINT_GRID );
359 : }
360 : }
361 :
362 0 : delete pOldAutoDBRange;
363 0 : pOldAutoDBRange = NULL;
364 : }
365 0 : }
366 :
367 :
368 : // Hoehen anpassen
369 : //! mit docfunc zusammenfassen
370 :
371 29 : sal_Bool ScDocShell::AdjustRowHeight( SCROW nStartRow, SCROW nEndRow, SCTAB nTab )
372 : {
373 29 : ScSizeDeviceProvider aProv(this);
374 29 : Fraction aZoom(1,1);
375 : sal_Bool bChange = aDocument.SetOptimalHeight( nStartRow,nEndRow, nTab, 0, aProv.GetDevice(),
376 29 : aProv.GetPPTX(),aProv.GetPPTY(), aZoom,aZoom, false );
377 29 : if (bChange)
378 17 : PostPaint( 0,nStartRow,nTab, MAXCOL,MAXROW,nTab, PAINT_GRID|PAINT_LEFT );
379 :
380 29 : return bChange;
381 : }
382 :
383 0 : void ScDocShell::UpdateAllRowHeights( const ScMarkData* pTabMark )
384 : {
385 : // update automatic row heights
386 :
387 0 : ScSizeDeviceProvider aProv(this);
388 0 : Fraction aZoom(1,1);
389 0 : aDocument.UpdateAllRowHeights( aProv.GetDevice(), aProv.GetPPTX(), aProv.GetPPTY(), aZoom, aZoom, pTabMark );
390 0 : }
391 :
392 4118 : void ScDocShell::UpdatePendingRowHeights( SCTAB nUpdateTab, bool bBefore )
393 : {
394 4118 : sal_Bool bIsUndoEnabled = aDocument.IsUndoEnabled();
395 4118 : aDocument.EnableUndo( false );
396 4118 : aDocument.LockStreamValid( true ); // ignore draw page size (but not formula results)
397 4118 : if ( bBefore ) // check all sheets up to nUpdateTab
398 : {
399 18 : SCTAB nTabCount = aDocument.GetTableCount();
400 18 : if ( nUpdateTab >= nTabCount )
401 0 : nUpdateTab = nTabCount-1; // nUpdateTab is inclusive
402 :
403 18 : ScMarkData aUpdateSheets;
404 : SCTAB nTab;
405 36 : for (nTab=0; nTab<=nUpdateTab; ++nTab)
406 18 : if ( aDocument.IsPendingRowHeights( nTab ) )
407 0 : aUpdateSheets.SelectTable( nTab, sal_True );
408 :
409 18 : if (aUpdateSheets.GetSelectCount())
410 0 : UpdateAllRowHeights(&aUpdateSheets); // update with a single progress bar
411 :
412 36 : for (nTab=0; nTab<=nUpdateTab; ++nTab)
413 18 : if ( aUpdateSheets.GetTableSelect( nTab ) )
414 : {
415 0 : aDocument.UpdatePageBreaks( nTab );
416 0 : aDocument.SetPendingRowHeights( nTab, false );
417 18 : }
418 : }
419 : else // only nUpdateTab
420 : {
421 4100 : if ( aDocument.IsPendingRowHeights( nUpdateTab ) )
422 : {
423 0 : AdjustRowHeight( 0, MAXROW, nUpdateTab );
424 0 : aDocument.UpdatePageBreaks( nUpdateTab );
425 0 : aDocument.SetPendingRowHeights( nUpdateTab, false );
426 : }
427 : }
428 4118 : aDocument.LockStreamValid( false );
429 4118 : aDocument.EnableUndo( bIsUndoEnabled );
430 4118 : }
431 :
432 0 : void ScDocShell::RefreshPivotTables( const ScRange& rSource )
433 : {
434 0 : ScDPCollection* pColl = aDocument.GetDPCollection();
435 0 : if (!pColl)
436 0 : return;
437 :
438 0 : ScDBDocFunc aFunc(*this);
439 0 : for (size_t i = 0, n = pColl->GetCount(); i < n; ++i)
440 : {
441 0 : ScDPObject* pOld = (*pColl)[i];
442 0 : if (!pOld)
443 0 : continue;
444 :
445 0 : const ScSheetSourceDesc* pSheetDesc = pOld->GetSheetDesc();
446 0 : if (pSheetDesc && pSheetDesc->GetSourceRange().Intersects(rSource))
447 0 : aFunc.UpdatePivotTable(*pOld, true, false);
448 0 : }
449 : }
450 :
451 0 : static OUString lcl_GetAreaName( ScDocument* pDoc, ScArea* pArea )
452 : {
453 0 : OUString aName;
454 0 : sal_Bool bOk = false;
455 : ScDBData* pData = pDoc->GetDBAtArea( pArea->nTab, pArea->nColStart, pArea->nRowStart,
456 0 : pArea->nColEnd, pArea->nRowEnd );
457 0 : if (pData)
458 : {
459 0 : aName = pData->GetName();
460 0 : bOk = sal_True;
461 : }
462 :
463 0 : if (!bOk)
464 0 : pDoc->GetName( pArea->nTab, aName );
465 :
466 0 : return aName;
467 : }
468 :
469 1 : void ScDocShell::DoConsolidate( const ScConsolidateParam& rParam, sal_Bool bRecord )
470 : {
471 1 : ScConsData aData;
472 :
473 : sal_uInt16 nPos;
474 1 : SCCOL nColSize = 0;
475 1 : SCROW nRowSize = 0;
476 1 : sal_Bool bErr = false;
477 1 : for (nPos=0; nPos<rParam.nDataAreaCount; nPos++)
478 : {
479 0 : ScArea* pArea = rParam.ppDataAreas[nPos];
480 0 : nColSize = std::max( nColSize, SCCOL( pArea->nColEnd - pArea->nColStart + 1 ) );
481 0 : nRowSize = std::max( nRowSize, SCROW( pArea->nRowEnd - pArea->nRowStart + 1 ) );
482 :
483 : // Test, ob Quelldaten verschoben wuerden
484 0 : if (rParam.bReferenceData)
485 0 : if (pArea->nTab == rParam.nTab && pArea->nRowEnd >= rParam.nRow)
486 0 : bErr = sal_True;
487 : }
488 :
489 1 : if (bErr)
490 : {
491 : InfoBox aBox( GetActiveDialogParent(),
492 0 : ScGlobal::GetRscString( STR_CONSOLIDATE_ERR1 ) );
493 0 : aBox.Execute();
494 1 : return;
495 : }
496 :
497 : // ausfuehren
498 :
499 2 : WaitObject aWait( GetActiveDialogParent() );
500 2 : ScDocShellModificator aModificator( *this );
501 :
502 1 : ScRange aOldDest;
503 1 : ScDBData* pDestData = aDocument.GetDBAtCursor( rParam.nCol, rParam.nRow, rParam.nTab, true );
504 1 : if (pDestData)
505 0 : pDestData->GetArea(aOldDest);
506 :
507 1 : aData.SetSize( nColSize, nRowSize );
508 1 : aData.SetFlags( rParam.eFunction, rParam.bByCol, rParam.bByRow, rParam.bReferenceData );
509 1 : if ( rParam.bByCol || rParam.bByRow )
510 0 : for (nPos=0; nPos<rParam.nDataAreaCount; nPos++)
511 : {
512 0 : ScArea* pArea = rParam.ppDataAreas[nPos];
513 : aData.AddFields( &aDocument, pArea->nTab, pArea->nColStart, pArea->nRowStart,
514 0 : pArea->nColEnd, pArea->nRowEnd );
515 : }
516 1 : aData.DoneFields();
517 1 : for (nPos=0; nPos<rParam.nDataAreaCount; nPos++)
518 : {
519 0 : ScArea* pArea = rParam.ppDataAreas[nPos];
520 : aData.AddData( &aDocument, pArea->nTab, pArea->nColStart, pArea->nRowStart,
521 0 : pArea->nColEnd, pArea->nRowEnd );
522 0 : aData.AddName( lcl_GetAreaName(&aDocument,pArea) );
523 : }
524 :
525 1 : aData.GetSize( nColSize, nRowSize );
526 1 : if (bRecord && nColSize > 0 && nRowSize > 0)
527 : {
528 0 : ScDBData* pUndoData = pDestData ? new ScDBData(*pDestData) : NULL;
529 :
530 0 : SCTAB nDestTab = rParam.nTab;
531 : ScArea aDestArea( rParam.nTab, rParam.nCol, rParam.nRow,
532 0 : rParam.nCol+nColSize-1, rParam.nRow+nRowSize-1 );
533 0 : if (rParam.bByCol) ++aDestArea.nColEnd;
534 0 : if (rParam.bByRow) ++aDestArea.nRowEnd;
535 :
536 0 : if (rParam.bReferenceData)
537 : {
538 0 : SCTAB nTabCount = aDocument.GetTableCount();
539 0 : SCROW nInsertCount = aData.GetInsertCount();
540 :
541 : // alte Outlines
542 0 : ScOutlineTable* pTable = aDocument.GetOutlineTable( nDestTab );
543 0 : ScOutlineTable* pUndoTab = pTable ? new ScOutlineTable( *pTable ) : NULL;
544 :
545 0 : ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
546 0 : pUndoDoc->InitUndo( &aDocument, 0, nTabCount-1, false, sal_True );
547 :
548 : // Zeilenstatus
549 : aDocument.CopyToDocument( 0,0,nDestTab, MAXCOL,MAXROW,nDestTab,
550 0 : IDF_NONE, false, pUndoDoc );
551 :
552 : // alle Formeln
553 : aDocument.CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1,
554 0 : IDF_FORMULA, false, pUndoDoc );
555 :
556 : // komplette Ausgangszeilen
557 : aDocument.CopyToDocument( 0,aDestArea.nRowStart,nDestTab,
558 : MAXCOL,aDestArea.nRowEnd,nDestTab,
559 0 : IDF_ALL, false, pUndoDoc );
560 :
561 : // alten Ausgabebereich
562 0 : if (pDestData)
563 0 : aDocument.CopyToDocument( aOldDest, IDF_ALL, false, pUndoDoc );
564 :
565 0 : GetUndoManager()->AddUndoAction(
566 : new ScUndoConsolidate( this, aDestArea, rParam, pUndoDoc,
567 0 : sal_True, nInsertCount, pUndoTab, pUndoData ) );
568 : }
569 : else
570 : {
571 0 : ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
572 0 : pUndoDoc->InitUndo( &aDocument, aDestArea.nTab, aDestArea.nTab );
573 :
574 : aDocument.CopyToDocument( aDestArea.nColStart, aDestArea.nRowStart, aDestArea.nTab,
575 : aDestArea.nColEnd, aDestArea.nRowEnd, aDestArea.nTab,
576 0 : IDF_ALL, false, pUndoDoc );
577 :
578 : // alten Ausgabebereich
579 0 : if (pDestData)
580 0 : aDocument.CopyToDocument( aOldDest, IDF_ALL, false, pUndoDoc );
581 :
582 0 : GetUndoManager()->AddUndoAction(
583 : new ScUndoConsolidate( this, aDestArea, rParam, pUndoDoc,
584 0 : false, 0, NULL, pUndoData ) );
585 : }
586 : }
587 :
588 1 : if (pDestData) // Zielbereich loeschen / anpassen
589 : {
590 0 : aDocument.DeleteAreaTab(aOldDest, IDF_CONTENTS);
591 : pDestData->SetArea( rParam.nTab, rParam.nCol, rParam.nRow,
592 0 : rParam.nCol + nColSize - 1, rParam.nRow + nRowSize - 1 );
593 0 : pDestData->SetHeader( rParam.bByRow );
594 : }
595 :
596 1 : aData.OutputToDocument( &aDocument, rParam.nCol, rParam.nRow, rParam.nTab );
597 :
598 1 : SCCOL nPaintStartCol = rParam.nCol;
599 1 : SCROW nPaintStartRow = rParam.nRow;
600 1 : SCCOL nPaintEndCol = nPaintStartCol + nColSize - 1;
601 1 : SCROW nPaintEndRow = nPaintStartRow + nRowSize - 1;
602 1 : sal_uInt16 nPaintFlags = PAINT_GRID;
603 1 : if (rParam.bByCol)
604 0 : ++nPaintEndRow;
605 1 : if (rParam.bByRow)
606 0 : ++nPaintEndCol;
607 1 : if (rParam.bReferenceData)
608 : {
609 0 : nPaintStartCol = 0;
610 0 : nPaintEndCol = MAXCOL;
611 0 : nPaintEndRow = MAXROW;
612 0 : nPaintFlags |= PAINT_LEFT | PAINT_SIZE;
613 : }
614 1 : if (pDestData)
615 : {
616 0 : if ( aOldDest.aEnd.Col() > nPaintEndCol )
617 0 : nPaintEndCol = aOldDest.aEnd.Col();
618 0 : if ( aOldDest.aEnd.Row() > nPaintEndRow )
619 0 : nPaintEndRow = aOldDest.aEnd.Row();
620 : }
621 : PostPaint( nPaintStartCol, nPaintStartRow, rParam.nTab,
622 1 : nPaintEndCol, nPaintEndRow, rParam.nTab, nPaintFlags );
623 2 : aModificator.SetDocumentModified();
624 : }
625 :
626 0 : void ScDocShell::UseScenario( SCTAB nTab, const String& rName, sal_Bool bRecord )
627 : {
628 0 : if (!aDocument.IsScenario(nTab))
629 : {
630 0 : SCTAB nTabCount = aDocument.GetTableCount();
631 0 : SCTAB nSrcTab = SCTAB_MAX;
632 0 : SCTAB nEndTab = nTab;
633 0 : OUString aCompare;
634 0 : while ( nEndTab+1 < nTabCount && aDocument.IsScenario(nEndTab+1) )
635 : {
636 0 : ++nEndTab;
637 0 : if (nSrcTab > MAXTAB) // noch auf der Suche nach dem Szenario?
638 : {
639 0 : aDocument.GetName( nEndTab, aCompare );
640 0 : if (aCompare.equals(rName))
641 0 : nSrcTab = nEndTab; // gefunden
642 : }
643 : }
644 0 : if (ValidTab(nSrcTab))
645 : {
646 0 : if ( aDocument.TestCopyScenario( nSrcTab, nTab ) ) // Zellschutz testen
647 : {
648 0 : ScDocShellModificator aModificator( *this );
649 0 : ScMarkData aScenMark;
650 0 : aDocument.MarkScenario( nSrcTab, nTab, aScenMark );
651 0 : ScRange aMultiRange;
652 0 : aScenMark.GetMultiMarkArea( aMultiRange );
653 0 : SCCOL nStartCol = aMultiRange.aStart.Col();
654 0 : SCROW nStartRow = aMultiRange.aStart.Row();
655 0 : SCCOL nEndCol = aMultiRange.aEnd.Col();
656 0 : SCROW nEndRow = aMultiRange.aEnd.Row();
657 :
658 0 : if (bRecord)
659 : {
660 0 : ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
661 0 : pUndoDoc->InitUndo( &aDocument, nTab,nEndTab ); // auch alle Szenarien
662 : // angezeigte Tabelle:
663 : aDocument.CopyToDocument( nStartCol,nStartRow,nTab,
664 0 : nEndCol,nEndRow,nTab, IDF_ALL,sal_True, pUndoDoc, &aScenMark );
665 : // Szenarien
666 0 : for (SCTAB i=nTab+1; i<=nEndTab; i++)
667 : {
668 0 : pUndoDoc->SetScenario( i, sal_True );
669 0 : OUString aComment;
670 0 : Color aColor;
671 : sal_uInt16 nScenFlags;
672 0 : aDocument.GetScenarioData( i, aComment, aColor, nScenFlags );
673 0 : pUndoDoc->SetScenarioData( i, aComment, aColor, nScenFlags );
674 0 : sal_Bool bActive = aDocument.IsActiveScenario( i );
675 0 : pUndoDoc->SetActiveScenario( i, bActive );
676 : // Bei Zurueckkopier-Szenarios auch Inhalte
677 0 : if ( nScenFlags & SC_SCENARIO_TWOWAY )
678 : aDocument.CopyToDocument( 0,0,i, MAXCOL,MAXROW,i,
679 0 : IDF_ALL,false, pUndoDoc );
680 0 : }
681 :
682 0 : GetUndoManager()->AddUndoAction(
683 : new ScUndoUseScenario( this, aScenMark,
684 : ScArea( nTab,nStartCol,nStartRow,nEndCol,nEndRow ),
685 0 : pUndoDoc, rName ) );
686 : }
687 :
688 0 : aDocument.CopyScenario( nSrcTab, nTab );
689 0 : aDocument.SetDirty();
690 :
691 : // alles painten, weil in anderen Bereichen das aktive Szenario
692 : // geaendert sein kann
693 : //! nur, wenn sichtbare Rahmen vorhanden?
694 0 : PostPaint( 0,0,nTab, MAXCOL,MAXROW,nTab, PAINT_GRID );
695 0 : aModificator.SetDocumentModified();
696 : }
697 : else
698 : {
699 : InfoBox aBox(GetActiveDialogParent(),
700 0 : ScGlobal::GetRscString( STR_PROTECTIONERR ) );
701 0 : aBox.Execute();
702 : }
703 : }
704 : else
705 : {
706 : InfoBox aBox(GetActiveDialogParent(),
707 0 : ScGlobal::GetRscString( STR_SCENARIO_NOTFOUND ) );
708 0 : aBox.Execute();
709 0 : }
710 : }
711 : else
712 : {
713 : OSL_FAIL( "UseScenario auf Szenario-Blatt" );
714 : }
715 0 : }
716 :
717 0 : void ScDocShell::ModifyScenario( SCTAB nTab, const String& rName, const String& rComment,
718 : const Color& rColor, sal_uInt16 nFlags )
719 : {
720 : // Undo
721 0 : OUString aOldName;
722 0 : aDocument.GetName( nTab, aOldName );
723 0 : OUString aOldComment;
724 0 : Color aOldColor;
725 : sal_uInt16 nOldFlags;
726 0 : aDocument.GetScenarioData( nTab, aOldComment, aOldColor, nOldFlags );
727 0 : GetUndoManager()->AddUndoAction(
728 : new ScUndoScenarioFlags( this, nTab,
729 : aOldName, rName, aOldComment, rComment,
730 0 : aOldColor, rColor, nOldFlags, nFlags ) );
731 :
732 : // ausfuehren
733 0 : ScDocShellModificator aModificator( *this );
734 0 : aDocument.RenameTab( nTab, rName );
735 0 : aDocument.SetScenarioData( nTab, rComment, rColor, nFlags );
736 0 : PostPaintGridAll();
737 0 : aModificator.SetDocumentModified();
738 :
739 0 : if (!aOldName.equals(rName))
740 0 : SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
741 :
742 0 : SfxBindings* pBindings = GetViewBindings();
743 0 : if (pBindings)
744 0 : pBindings->Invalidate( SID_SELECT_SCENARIO );
745 0 : }
746 :
747 1 : SCTAB ScDocShell::MakeScenario( SCTAB nTab, const String& rName, const String& rComment,
748 : const Color& rColor, sal_uInt16 nFlags,
749 : ScMarkData& rMark, sal_Bool bRecord )
750 : {
751 1 : rMark.MarkToMulti();
752 1 : if (rMark.IsMultiMarked())
753 : {
754 1 : SCTAB nNewTab = nTab + 1;
755 2 : while (aDocument.IsScenario(nNewTab))
756 0 : ++nNewTab;
757 :
758 1 : sal_Bool bCopyAll = ( (nFlags & SC_SCENARIO_COPYALL) != 0 );
759 1 : const ScMarkData* pCopyMark = NULL;
760 1 : if (!bCopyAll)
761 1 : pCopyMark = &rMark;
762 :
763 1 : ScDocShellModificator aModificator( *this );
764 :
765 1 : if (bRecord)
766 1 : aDocument.BeginDrawUndo(); // drawing layer must do its own undo actions
767 :
768 1 : if (aDocument.CopyTab( nTab, nNewTab, pCopyMark ))
769 : {
770 1 : if (bRecord)
771 : {
772 1 : GetUndoManager()->AddUndoAction(
773 : new ScUndoMakeScenario( this, nTab, nNewTab,
774 1 : rName, rComment, rColor, nFlags, rMark ));
775 : }
776 :
777 1 : aDocument.RenameTab( nNewTab, rName, false ); // ohne Formel-Update
778 1 : aDocument.SetScenario( nNewTab, sal_True );
779 1 : aDocument.SetScenarioData( nNewTab, rComment, rColor, nFlags );
780 :
781 1 : ScMarkData aDestMark = rMark;
782 1 : aDestMark.SelectOneTable( nNewTab );
783 :
784 : //! auf Filter / Buttons / Merging testen !
785 :
786 2 : ScPatternAttr aProtPattern( aDocument.GetPool() );
787 1 : aProtPattern.GetItemSet().Put( ScProtectionAttr( sal_True ) );
788 1 : aDocument.ApplyPatternAreaTab( 0,0, MAXCOL,MAXROW, nNewTab, aProtPattern );
789 :
790 2 : ScPatternAttr aPattern( aDocument.GetPool() );
791 1 : aPattern.GetItemSet().Put( ScMergeFlagAttr( SC_MF_SCENARIO ) );
792 1 : aPattern.GetItemSet().Put( ScProtectionAttr( sal_True ) );
793 1 : aDocument.ApplySelectionPattern( aPattern, aDestMark );
794 :
795 1 : if (!bCopyAll)
796 1 : aDocument.SetVisible( nNewTab, false );
797 :
798 : // dies ist dann das aktive Szenario
799 1 : aDocument.CopyScenario( nNewTab, nTab, sal_True ); // sal_True - nicht aus Szenario kopieren
800 :
801 1 : if (nFlags & SC_SCENARIO_SHOWFRAME)
802 1 : PostPaint( 0,0,nTab, MAXCOL,MAXROW,nTab, PAINT_GRID ); // Rahmen painten
803 1 : PostPaintExtras(); // Tabellenreiter
804 1 : aModificator.SetDocumentModified();
805 :
806 1 : SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
807 :
808 2 : return nNewTab;
809 0 : }
810 : }
811 0 : return nTab;
812 : }
813 :
814 1 : sal_uLong ScDocShell::TransferTab( ScDocShell& rSrcDocShell, SCTAB nSrcPos,
815 : SCTAB nDestPos, sal_Bool bInsertNew,
816 : sal_Bool bNotifyAndPaint )
817 : {
818 1 : ScDocument* pSrcDoc = rSrcDocShell.GetDocument();
819 :
820 : // set the transferred area to the copyparam to make adjusting formulas possible
821 1 : ScClipParam aParam;
822 1 : ScRange aRange(0, 0, nSrcPos, MAXCOL, MAXROW, nSrcPos);
823 1 : aParam.maRanges.Append(aRange);
824 1 : pSrcDoc->SetClipParam(aParam);
825 :
826 : sal_uLong nErrVal = aDocument.TransferTab( pSrcDoc, nSrcPos, nDestPos,
827 1 : bInsertNew ); // no insert
828 :
829 : // TransferTab doesn't copy drawing objects with bInsertNew=FALSE
830 1 : if ( nErrVal > 0 && !bInsertNew)
831 0 : aDocument.TransferDrawPage( pSrcDoc, nSrcPos, nDestPos );
832 :
833 1 : if(nErrVal>0 && pSrcDoc->IsScenario( nSrcPos ))
834 : {
835 0 : OUString aComment;
836 0 : Color aColor;
837 : sal_uInt16 nFlags;
838 :
839 0 : pSrcDoc->GetScenarioData( nSrcPos, aComment,aColor, nFlags);
840 0 : aDocument.SetScenario(nDestPos,true);
841 0 : aDocument.SetScenarioData(nDestPos,aComment,aColor,nFlags);
842 0 : sal_Bool bActive = pSrcDoc->IsActiveScenario(nSrcPos);
843 0 : aDocument.SetActiveScenario(nDestPos, bActive );
844 :
845 0 : sal_Bool bVisible=pSrcDoc->IsVisible(nSrcPos);
846 0 : aDocument.SetVisible(nDestPos,bVisible );
847 :
848 : }
849 :
850 1 : if ( nErrVal > 0 && pSrcDoc->IsTabProtected( nSrcPos ) )
851 0 : aDocument.SetTabProtection(nDestPos, pSrcDoc->GetTabProtection(nSrcPos));
852 1 : if ( bNotifyAndPaint )
853 : {
854 1 : Broadcast( ScTablesHint( SC_TAB_INSERTED, nDestPos ) );
855 1 : PostPaintExtras();
856 1 : PostPaintGridAll();
857 : }
858 1 : return nErrVal;
859 : }
860 :
861 0 : sal_Bool ScDocShell::MoveTable( SCTAB nSrcTab, SCTAB nDestTab, sal_Bool bCopy, sal_Bool bRecord )
862 : {
863 0 : ScDocShellModificator aModificator( *this );
864 :
865 : // #i92477# be consistent with ScDocFunc::InsertTable: any index past the last sheet means "append"
866 : // #i101139# nDestTab must be the target position, not APPEND (for CopyTabProtection etc.)
867 0 : if ( nDestTab >= aDocument.GetTableCount() )
868 0 : nDestTab = aDocument.GetTableCount();
869 :
870 0 : if (bCopy)
871 : {
872 0 : if (bRecord)
873 0 : aDocument.BeginDrawUndo(); // drawing layer must do its own undo actions
874 :
875 0 : OUString sSrcCodeName;
876 0 : aDocument.GetCodeName( nSrcTab, sSrcCodeName );
877 0 : if (!aDocument.CopyTab( nSrcTab, nDestTab ))
878 : {
879 : //! EndDrawUndo?
880 0 : return false;
881 : }
882 : else
883 : {
884 0 : SCTAB nAdjSource = nSrcTab;
885 0 : if ( nDestTab <= nSrcTab )
886 0 : ++nAdjSource; // new position of source table after CopyTab
887 :
888 0 : if ( aDocument.IsTabProtected( nAdjSource ) )
889 0 : aDocument.CopyTabProtection(nAdjSource, nDestTab);
890 :
891 0 : if (bRecord)
892 : {
893 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
894 0 : auto_ptr< vector<SCTAB> > pSrcList(new vector<SCTAB>(1, nSrcTab));
895 0 : auto_ptr< vector<SCTAB> > pDestList(new vector<SCTAB>(1, nDestTab));
896 : SAL_WNODEPRECATED_DECLARATIONS_POP
897 0 : GetUndoManager()->AddUndoAction(
898 0 : new ScUndoCopyTab(this, pSrcList.release(), pDestList.release()));
899 : }
900 :
901 0 : sal_Bool bVbaEnabled = aDocument.IsInVBAMode();
902 0 : if ( bVbaEnabled )
903 : {
904 0 : String aLibName( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
905 0 : Reference< XLibraryContainer > xLibContainer = GetBasicContainer();
906 0 : Reference< XVBACompatibility > xVBACompat( xLibContainer, UNO_QUERY );
907 :
908 0 : if ( xVBACompat.is() )
909 : {
910 0 : aLibName = xVBACompat->getProjectName();
911 : }
912 :
913 0 : SCTAB nTabToUse = nDestTab;
914 0 : if ( nDestTab == SC_TAB_APPEND )
915 0 : nTabToUse = aDocument.GetMaxTableNumber() - 1;
916 0 : OUString sCodeName;
917 0 : OUString sSource;
918 : try
919 : {
920 0 : Reference< XNameContainer > xLib;
921 0 : if( xLibContainer.is() )
922 : {
923 0 : com::sun::star::uno::Any aLibAny = xLibContainer->getByName( aLibName );
924 0 : aLibAny >>= xLib;
925 : }
926 0 : if( xLib.is() )
927 : {
928 0 : xLib->getByName( sSrcCodeName ) >>= sSource;
929 0 : }
930 : }
931 0 : catch ( const com::sun::star::uno::Exception& )
932 : {
933 : }
934 0 : VBA_InsertModule( aDocument, nTabToUse, sCodeName, sSource );
935 : }
936 : }
937 0 : Broadcast( ScTablesHint( SC_TAB_COPIED, nSrcTab, nDestTab ) );
938 : }
939 : else
940 : {
941 0 : if ( aDocument.GetChangeTrack() )
942 0 : return false;
943 :
944 0 : if ( nSrcTab<nDestTab && nDestTab!=SC_TAB_APPEND )
945 0 : nDestTab--;
946 :
947 0 : if ( nSrcTab == nDestTab )
948 : {
949 : //! allow only for api calls?
950 0 : return sal_True; // nothing to do, but valid
951 : }
952 :
953 0 : ScProgress* pProgress = new ScProgress(this, ScGlobal::GetRscString(STR_UNDO_MOVE_TAB),
954 0 : aDocument.GetCodeCount());
955 0 : bool bDone = aDocument.MoveTab( nSrcTab, nDestTab, pProgress );
956 0 : delete pProgress;
957 0 : if (!bDone)
958 : {
959 0 : return false;
960 : }
961 0 : else if (bRecord)
962 : {
963 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
964 0 : auto_ptr< vector<SCTAB> > pSrcList(new vector<SCTAB>(1, nSrcTab));
965 0 : auto_ptr< vector<SCTAB> > pDestList(new vector<SCTAB>(1, nDestTab));
966 : SAL_WNODEPRECATED_DECLARATIONS_POP
967 0 : GetUndoManager()->AddUndoAction(
968 0 : new ScUndoMoveTab(this, pSrcList.release(), pDestList.release()));
969 : }
970 :
971 0 : Broadcast( ScTablesHint( SC_TAB_MOVED, nSrcTab, nDestTab ) );
972 : }
973 :
974 0 : PostPaintGridAll();
975 0 : PostPaintExtras();
976 0 : aModificator.SetDocumentModified();
977 0 : SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
978 :
979 0 : return sal_True;
980 : }
981 :
982 :
983 0 : IMPL_LINK( ScDocShell, RefreshDBDataHdl, ScRefreshTimer*, pRefreshTimer )
984 : {
985 0 : ScDBDocFunc aFunc(*this);
986 :
987 0 : sal_Bool bContinue = sal_True;
988 0 : ScDBData* pDBData = static_cast<ScDBData*>(pRefreshTimer);
989 0 : ScImportParam aImportParam;
990 0 : pDBData->GetImportParam( aImportParam );
991 0 : if (aImportParam.bImport && !pDBData->HasImportSelection())
992 : {
993 0 : ScRange aRange;
994 0 : pDBData->GetArea( aRange );
995 0 : bContinue = aFunc.DoImport( aRange.aStart.Tab(), aImportParam, NULL, true, false ); //! Api-Flag as parameter
996 : // internal operations (sort, query, subtotal) only if no error
997 0 : if (bContinue)
998 : {
999 0 : aFunc.RepeatDB( pDBData->GetName(), sal_True, sal_True );
1000 0 : RefreshPivotTables(aRange);
1001 : }
1002 : }
1003 :
1004 0 : return bContinue != 0;
1005 93 : }
1006 :
1007 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|