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