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