xref: /aoo4110/main/sc/source/ui/docshell/docsh5.cxx (revision b1cdbd2c)
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 
ErrorMessage(sal_uInt16 nGlobStrId)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 
IsEditable() const96 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 
DBAreaDeleted(SCTAB nTab,SCCOL nX1,SCROW nY1,SCCOL nX2,SCROW)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 
lcl_GetDBNearCursor(const ScDBCollection * pColl,const SCCOL nCol,const SCROW nRow,const SCTAB nTab)115 ScDBData* lcl_GetDBNearCursor(
116     const ScDBCollection* pColl,
117     const SCCOL nCol,
118     const SCROW nRow,
119     const SCTAB nTab )
120 {
121     //!	nach document/dbcolect verschieben
122 
123     if (!pColl)
124         return NULL;
125 
126     ScDBData* pInternalDBData = NULL;
127     ScDBData* pNearData = NULL;
128     sal_uInt16 nCount = pColl->GetCount();
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
137              && nCol+1 >= nStartCol
138              && nCol <= nEndCol+1
139              && nRow+1 >= nStartRow
140              && nRow <= nEndRow+1 )
141         {
142             if ( pDB->IsInternalUnnamed()
143                  || pDB->IsInternalForAutoFilter() )
144             {
145                 pInternalDBData = pDB;
146             }
147             else if ( nCol < nStartCol || nCol > nEndCol || nRow < nStartRow || nRow > nEndRow )
148             {
149                 if ( !pNearData )
150                     pNearData = pDB;	// ersten angrenzenden Bereich merken
151             }
152             else
153                 return pDB;				// nicht "unbenannt" und Cursor steht wirklich drin
154         }
155     }
156     if (pNearData)
157         return pNearData;				// angrenzender, wenn nichts direkt getroffen
158     return pInternalDBData;
159 }
160 
GetDBData(const ScRange & rMarked,ScGetDBMode eMode,ScGetDBSelection eSel)161 ScDBData* ScDocShell::GetDBData( const ScRange& rMarked, ScGetDBMode eMode, ScGetDBSelection eSel )
162 {
163     const SCCOL nCol = rMarked.aStart.Col();
164     const SCROW nRow = rMarked.aStart.Row();
165     const SCTAB nTab = rMarked.aStart.Tab();
166 
167     SCCOL nStartCol = nCol;
168     SCROW nStartRow = nRow;
169     SCCOL nEndCol = rMarked.aEnd.Col();
170     SCROW nEndRow = rMarked.aEnd.Row();
171 
172     ScDBData* pFoundDBData = aDocument.GetDBAtArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow );
173     if ( pFoundDBData == NULL )
174     {
175         pFoundDBData = lcl_GetDBNearCursor( aDocument.GetDBCollection(), nCol, nRow, nTab );
176     }
177 
178     const bool bSelected =
179         ( eSel == SC_DBSEL_FORCE_MARK
180           || ( rMarked.aStart != rMarked.aEnd
181                && eSel != SC_DBSEL_ROW_DOWN ) );
182     const bool bOnlyDown = ( !bSelected
183                              && eSel == SC_DBSEL_ROW_DOWN
184                              && rMarked.aStart.Row() == rMarked.aEnd.Row());
185 
186     bool bUseFoundDBData = false;
187     if ( pFoundDBData )
188     {
189         // check, if found database range can be used
190         SCTAB nDummy;
191         SCCOL nOldCol1;
192         SCROW nOldRow1;
193         SCCOL nOldCol2;
194         SCROW nOldRow2;
195         pFoundDBData->GetArea( nDummy, nOldCol1, nOldRow1, nOldCol2, nOldRow2 );
196 
197         const bool bIsUnnamedOne = pFoundDBData->IsInternalUnnamed();
198         const bool bIsInternalForAutoFilter = pFoundDBData->IsInternalForAutoFilter();
199         if ( !bSelected )
200         {
201             bUseFoundDBData = true;
202             if ( ( bIsUnnamedOne || bIsInternalForAutoFilter )
203                  && ( eMode == SC_DB_MAKE || eMode == SC_DB_MAKE_AUTOFILTER ) )
204             {
205                 // If nothing marked or only one row marked, adapt found database range to contiguous area.
206                 nStartCol = nCol;
207                 nStartRow = nRow;
208                 if ( bOnlyDown )
209                 {
210                     nEndCol = rMarked.aEnd.Col();
211                     nEndRow = rMarked.aEnd.Row();
212                 }
213                 else
214                 {
215                     nEndCol = nStartCol;
216                     nEndRow = nStartRow;
217                 }
218                 aDocument.GetDataArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow, sal_False, bOnlyDown );
219                 if ( nOldCol1 != nStartCol || nOldCol2 != nEndCol || nOldRow1 != nStartRow )
220                 {
221                     bUseFoundDBData = false;
222                 }
223                 else if ( nOldRow2 != nEndRow )
224                 {
225                     // adapt found internal database range to new end row
226                     pFoundDBData->SetArea( nTab, nOldCol1,nOldRow1, nOldCol2,nEndRow );
227                 }
228             }
229         }
230         else
231         {
232             if ( nOldCol1 == nStartCol
233                  && nOldRow1 == nStartRow
234                  && nOldCol2 == nEndCol
235                  && nOldRow2 == nEndRow )
236             {
237                 bUseFoundDBData = true;
238             }
239             else
240             {
241                 bUseFoundDBData = false;
242             }
243         }
244 
245         // adapt internal unnamed database range to an auto filter one
246         // otherwise the auto filter is lost when the internal unnamed one is changed/reused/deleted
247         if ( bUseFoundDBData
248              && eMode == SC_DB_MAKE_AUTOFILTER
249              && bIsUnnamedOne )
250         {
251             pFoundDBData->SetName( aDocument.GetDBCollection()->GetNewDefaultDBName() );
252         }
253 
254         // no internal database range for Import
255         if ( bUseFoundDBData
256              && eMode == SC_DB_IMPORT
257              && ( bIsUnnamedOne || bIsInternalForAutoFilter ) )
258         {
259             bUseFoundDBData = false;
260         }
261     }
262 
263     if ( bUseFoundDBData )
264     {
265         return pFoundDBData;
266     }
267 
268     if ( eMode == SC_DB_OLD )
269     {
270         // no existing database range found
271         return NULL;
272     }
273     else // eMode == SC_DB_MAKE||SC_DB_IMPORT||SC_DB_MAKE_AUTOFILTER
274     {
275         if ( !bSelected )
276         {
277             nStartCol = nCol;
278             nStartRow = nRow;
279             if (bOnlyDown)
280             {
281                 nEndCol = rMarked.aEnd.Col();
282                 nEndRow = rMarked.aEnd.Row();
283             }
284             else
285             {
286                 nEndCol = nStartCol;
287                 nEndRow = nStartRow;
288             }
289             aDocument.GetDataArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow, sal_False, bOnlyDown );
290         }
291 
292         const sal_Bool bHasHeader = aDocument.HasColHeader( nStartCol, nStartRow, nEndCol, nEndRow, nTab );
293 
294         ScDBData* pDBData = NULL;
295         sal_uInt16 nUnnamedDBIndex;
296         ScDBCollection* pColl = aDocument.GetDBCollection();
297         if ( eMode == SC_DB_MAKE &&
298              pColl->SearchName( ScGlobal::GetRscString( STR_DB_NONAME ), nUnnamedDBIndex ) )
299         {
300             // adapt existing unnamed database range
301             pDBData = (*pColl)[nUnnamedDBIndex];
302 
303             if ( !pOldAutoDBRange )
304             {
305                 // store the old unnamed database range with its settings for undo
306                 // (store at the first change, get the state before all changes)
307                 pOldAutoDBRange = new ScDBData( *pDBData );
308             }
309 
310             SCCOL nOldX1;
311             SCROW nOldY1;
312             SCCOL nOldX2;
313             SCROW nOldY2;
314             SCTAB nOldTab;
315             pDBData->GetArea( nOldTab, nOldX1, nOldY1, nOldX2, nOldY2 );
316             DBAreaDeleted( nOldTab, nOldX1, nOldY1, nOldX2, nOldY2 );
317 
318             pDBData->SetSortParam( ScSortParam() );
319             pDBData->SetQueryParam( ScQueryParam() );
320             pDBData->SetSubTotalParam( ScSubTotalParam() );
321 
322             pDBData->SetArea( nTab, nStartCol,nStartRow, nEndCol,nEndRow );
323             pDBData->SetByRow( sal_True );
324             pDBData->SetHeader( bHasHeader );
325             pDBData->SetAutoFilter( sal_False );
326         }
327         else
328         {
329             ScDBCollection* pUndoColl = NULL;
330 
331             String aNewName;
332             switch ( eMode )
333             {
334             case SC_DB_IMPORT:
335             {
336                 aDocument.CompileDBFormula( sal_True );
337                 pUndoColl = new ScDBCollection( *pColl );
338 
339                 String aImport = ScGlobal::GetRscString( STR_DBNAME_IMPORT );
340                 long nCount = 0;
341                 sal_uInt16 nDummy;
342                 do
343                 {
344                     ++nCount;
345                     aNewName = aImport;
346                     aNewName += String::CreateFromInt32( nCount );
347                 }
348                 while (pColl->SearchName( aNewName, nDummy ));
349             }
350             break;
351 
352             case SC_DB_MAKE_AUTOFILTER:
353             {
354                 aDocument.CompileDBFormula( sal_True );
355                 pUndoColl = new ScDBCollection( *pColl );
356 
357                 aNewName = pColl->GetNewDefaultDBName();
358             }
359             break;
360 
361             case SC_DB_MAKE:
362             {
363                 aNewName = ScGlobal::GetRscString( STR_DB_NONAME );
364             }
365             break;
366 
367             default:
368                 DBG_ERROR( "<ScDocShell::GetDBData(..)> - unexcepted <eMode>" );
369                 break;
370             }
371 
372             pDBData = new ScDBData(
373                 aNewName, nTab, nStartCol, nStartRow, nEndCol, nEndRow, sal_True, bHasHeader );
374             pColl->Insert( pDBData );
375 
376             if ( pUndoColl )
377             {
378                 aDocument.CompileDBFormula( sal_False );
379 
380                 ScDBCollection* pRedoColl = new ScDBCollection( *pColl );
381                 GetUndoManager()->AddUndoAction( new ScUndoDBData( this, pUndoColl, pRedoColl ) );
382             }
383 
384             // notify Navigator about database range "Import[X]"
385             if ( eMode==SC_DB_IMPORT )
386             {
387                 SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) );
388             }
389         }
390 
391         return pDBData;
392     }
393 
394     return NULL; // never reached
395 }
396 
397 
GetOldAutoDBRange()398 ScDBData* ScDocShell::GetOldAutoDBRange()
399 {
400     ScDBData* pRet = pOldAutoDBRange;
401     pOldAutoDBRange = NULL;
402     return pRet;                    // has to be deleted by caller!
403 }
404 
CancelAutoDBRange()405 void ScDocShell::CancelAutoDBRange()
406 {
407     // called when dialog is cancelled
408     if ( pOldAutoDBRange )
409     {
410         sal_uInt16 nNoNameIndex;
411         ScDBCollection* pColl = aDocument.GetDBCollection();
412         if ( pColl->SearchName( ScGlobal::GetRscString( STR_DB_NONAME ), nNoNameIndex ) )
413         {
414             ScDBData* pNoNameData = (*pColl)[nNoNameIndex];
415 
416             SCCOL nRangeX1;
417             SCROW nRangeY1;
418             SCCOL nRangeX2;
419             SCROW nRangeY2;
420             SCTAB nRangeTab;
421             pNoNameData->GetArea( nRangeTab, nRangeX1, nRangeY1, nRangeX2, nRangeY2 );
422             DBAreaDeleted( nRangeTab, nRangeX1, nRangeY1, nRangeX2, nRangeY2 );
423 
424             *pNoNameData = *pOldAutoDBRange;    // restore old settings
425 
426             if ( pOldAutoDBRange->HasAutoFilter() )
427             {
428                 // restore AutoFilter buttons
429                 pOldAutoDBRange->GetArea( nRangeTab, nRangeX1, nRangeY1, nRangeX2, nRangeY2 );
430                 aDocument.ApplyFlagsTab( nRangeX1, nRangeY1, nRangeX2, nRangeY1, nRangeTab, SC_MF_AUTO );
431                 PostPaint( nRangeX1, nRangeY1, nRangeTab, nRangeX2, nRangeY1, nRangeTab, PAINT_GRID );
432             }
433         }
434 
435         delete pOldAutoDBRange;
436         pOldAutoDBRange = NULL;
437     }
438 }
439 
440 
441 		//	Hoehen anpassen
442 		//!	mit docfunc zusammenfassen
443 
AdjustRowHeight(SCROW nStartRow,SCROW nEndRow,SCTAB nTab)444 sal_Bool ScDocShell::AdjustRowHeight( SCROW nStartRow, SCROW nEndRow, SCTAB nTab )
445 {
446 	ScSizeDeviceProvider aProv(this);
447 	Fraction aZoom(1,1);
448 	sal_Bool bChange = aDocument.SetOptimalHeight( nStartRow,nEndRow, nTab, 0, aProv.GetDevice(),
449 												aProv.GetPPTX(),aProv.GetPPTY(), aZoom,aZoom, sal_False );
450 	if (bChange)
451 		PostPaint( 0,nStartRow,nTab, MAXCOL,MAXROW,nTab, PAINT_GRID|PAINT_LEFT );
452 
453 	return bChange;
454 }
455 
UpdateAllRowHeights(const ScMarkData * pTabMark)456 void ScDocShell::UpdateAllRowHeights( const ScMarkData* pTabMark )
457 {
458 	// update automatic row heights
459 
460 	ScSizeDeviceProvider aProv(this);
461 	Fraction aZoom(1,1);
462     aDocument.UpdateAllRowHeights( aProv.GetDevice(), aProv.GetPPTX(), aProv.GetPPTY(), aZoom, aZoom, pTabMark );
463 }
464 
UpdatePendingRowHeights(SCTAB nUpdateTab,bool bBefore)465 void ScDocShell::UpdatePendingRowHeights( SCTAB nUpdateTab, bool bBefore )
466 {
467     sal_Bool bIsUndoEnabled = aDocument.IsUndoEnabled();
468     aDocument.EnableUndo( sal_False );
469     aDocument.LockStreamValid( true );      // ignore draw page size (but not formula results)
470     if ( bBefore )          // check all sheets up to nUpdateTab
471     {
472         SCTAB nTabCount = aDocument.GetTableCount();
473         if ( nUpdateTab >= nTabCount )
474             nUpdateTab = nTabCount-1;     // nUpdateTab is inclusive
475 
476         ScMarkData aUpdateSheets;
477         SCTAB nTab;
478         for (nTab=0; nTab<=nUpdateTab; ++nTab)
479             if ( aDocument.IsPendingRowHeights( nTab ) )
480                 aUpdateSheets.SelectTable( nTab, sal_True );
481 
482         if (aUpdateSheets.GetSelectCount())
483             UpdateAllRowHeights(&aUpdateSheets);        // update with a single progress bar
484 
485         for (nTab=0; nTab<=nUpdateTab; ++nTab)
486             if ( aUpdateSheets.GetTableSelect( nTab ) )
487             {
488                 aDocument.UpdatePageBreaks( nTab );
489                 aDocument.SetPendingRowHeights( nTab, sal_False );
490             }
491     }
492     else                    // only nUpdateTab
493     {
494         if ( aDocument.IsPendingRowHeights( nUpdateTab ) )
495         {
496             AdjustRowHeight( 0, MAXROW, nUpdateTab );
497             aDocument.UpdatePageBreaks( nUpdateTab );
498             aDocument.SetPendingRowHeights( nUpdateTab, sal_False );
499         }
500     }
501     aDocument.LockStreamValid( false );
502     aDocument.EnableUndo( bIsUndoEnabled );
503 }
504 
RefreshPivotTables(const ScRange & rSource)505 void ScDocShell::RefreshPivotTables( const ScRange& rSource )
506 {
507 	//!	rename to RefreshDataPilotTables?
508 
509 	ScDPCollection* pColl = aDocument.GetDPCollection();
510 	if ( pColl )
511 	{
512 		//	DataPilotUpdate doesn't modify the collection order like PivotUpdate did,
513 		//	so a simple loop can be used.
514 
515 		sal_uInt16 nCount = pColl->GetCount();
516 		for ( sal_uInt16 i=0; i<nCount; i++ )
517 		{
518 			ScDPObject* pOld = (*pColl)[i];
519 			if ( pOld )
520 			{
521 				const ScSheetSourceDesc* pSheetDesc = pOld->GetSheetDesc();
522 				if ( pSheetDesc && pSheetDesc->aSourceRange.Intersects( rSource ) )
523 				{
524 					ScDPObject* pNew = new ScDPObject( *pOld );
525 					ScDBDocFunc aFunc( *this );
526 					aFunc.DataPilotUpdate( pOld, pNew, sal_True, sal_False );
527 					delete pNew;	// DataPilotUpdate copies settings from "new" object
528 				}
529 			}
530 		}
531 	}
532 }
533 
lcl_GetAreaName(ScDocument * pDoc,ScArea * pArea)534 String lcl_GetAreaName( ScDocument* pDoc, ScArea* pArea )
535 {
536     String aName;
537     bool bOk = false;
538     ScDBData* pData = pDoc->GetDBAtArea( pArea->nTab, pArea->nColStart, pArea->nRowStart, pArea->nColEnd, pArea->nRowEnd );
539     if (pData)
540     {
541         pData->GetName( aName );
542         if ( !pData->IsInternalUnnamed() )
543             bOk = true;
544     }
545 
546     if (!bOk)
547     {
548         pDoc->GetName( pArea->nTab, aName );
549     }
550 
551     return aName;
552 }
553 
DoConsolidate(const ScConsolidateParam & rParam,sal_Bool bRecord)554 void ScDocShell::DoConsolidate( const ScConsolidateParam& rParam, sal_Bool bRecord )
555 {
556 	ScConsData aData;
557 
558 	sal_uInt16 nPos;
559 	SCCOL nColSize = 0;
560 	SCROW nRowSize = 0;
561 	sal_Bool bErr = sal_False;
562 	for (nPos=0; nPos<rParam.nDataAreaCount; nPos++)
563 	{
564 		ScArea* pArea = rParam.ppDataAreas[nPos];
565 		nColSize = Max( nColSize, SCCOL( pArea->nColEnd - pArea->nColStart + 1 ) );
566 		nRowSize = Max( nRowSize, SCROW( pArea->nRowEnd - pArea->nRowStart + 1 ) );
567 
568 										// Test, ob Quelldaten verschoben wuerden
569 		if (rParam.bReferenceData)
570 			if (pArea->nTab == rParam.nTab && pArea->nRowEnd >= rParam.nRow)
571 				bErr = sal_True;
572 	}
573 
574 	if (bErr)
575 	{
576 		InfoBox aBox( GetActiveDialogParent(),
577 				ScGlobal::GetRscString( STR_CONSOLIDATE_ERR1 ) );
578 		aBox.Execute();
579 		return;
580 	}
581 
582 	//		ausfuehren
583 
584 	WaitObject aWait( GetActiveDialogParent() );
585 	ScDocShellModificator aModificator( *this );
586 
587 	ScRange aOldDest;
588 	ScDBData* pDestData = aDocument.GetDBAtCursor( rParam.nCol, rParam.nRow, rParam.nTab, sal_True );
589 	if (pDestData)
590 		pDestData->GetArea(aOldDest);
591 
592 	aData.SetSize( nColSize, nRowSize );
593 	aData.SetFlags( rParam.eFunction, rParam.bByCol, rParam.bByRow, rParam.bReferenceData );
594 	if ( rParam.bByCol || rParam.bByRow )
595 		for (nPos=0; nPos<rParam.nDataAreaCount; nPos++)
596 		{
597 			ScArea* pArea = rParam.ppDataAreas[nPos];
598 			aData.AddFields( &aDocument, pArea->nTab, pArea->nColStart, pArea->nRowStart,
599 														pArea->nColEnd, pArea->nRowEnd );
600 		}
601 	aData.DoneFields();
602 	for (nPos=0; nPos<rParam.nDataAreaCount; nPos++)
603 	{
604 		ScArea* pArea = rParam.ppDataAreas[nPos];
605 		aData.AddData( &aDocument, pArea->nTab, pArea->nColStart, pArea->nRowStart,
606 													pArea->nColEnd, pArea->nRowEnd );
607 		aData.AddName( lcl_GetAreaName(&aDocument,pArea) );
608 	}
609 
610 	aData.GetSize( nColSize, nRowSize );
611 	if (bRecord && nColSize > 0 && nRowSize > 0)
612 	{
613 		ScDBData* pUndoData = pDestData ? new ScDBData(*pDestData) : NULL;
614 
615 		SCTAB nDestTab = rParam.nTab;
616 		ScArea aDestArea( rParam.nTab, rParam.nCol, rParam.nRow,
617 							rParam.nCol+nColSize-1, rParam.nRow+nRowSize-1 );
618 		if (rParam.bByCol) ++aDestArea.nColEnd;
619 		if (rParam.bByRow) ++aDestArea.nRowEnd;
620 
621 		if (rParam.bReferenceData)
622 		{
623 			SCTAB nTabCount = aDocument.GetTableCount();
624 			SCROW nInsertCount = aData.GetInsertCount();
625 
626 			// alte Outlines
627 			ScOutlineTable* pTable = aDocument.GetOutlineTable( nDestTab );
628 			ScOutlineTable* pUndoTab = pTable ? new ScOutlineTable( *pTable ) : NULL;
629 
630 			ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
631 			pUndoDoc->InitUndo( &aDocument, 0, nTabCount-1, sal_False, sal_True );
632 
633 			// Zeilenstatus
634 			aDocument.CopyToDocument( 0,0,nDestTab, MAXCOL,MAXROW,nDestTab,
635 									IDF_NONE, sal_False, pUndoDoc );
636 
637 			// alle Formeln
638 			aDocument.CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1,
639 										IDF_FORMULA, sal_False, pUndoDoc );
640 
641 			// komplette Ausgangszeilen
642 			aDocument.CopyToDocument( 0,aDestArea.nRowStart,nDestTab,
643 									MAXCOL,aDestArea.nRowEnd,nDestTab,
644 									IDF_ALL, sal_False, pUndoDoc );
645 
646 			// alten Ausgabebereich
647 			if (pDestData)
648 				aDocument.CopyToDocument( aOldDest, IDF_ALL, sal_False, pUndoDoc );
649 
650 			GetUndoManager()->AddUndoAction(
651 					new ScUndoConsolidate( this, aDestArea, rParam, pUndoDoc,
652 											sal_True, nInsertCount, pUndoTab, pUndoData ) );
653 		}
654 		else
655 		{
656 			ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
657 			pUndoDoc->InitUndo( &aDocument, aDestArea.nTab, aDestArea.nTab );
658 
659 			aDocument.CopyToDocument( aDestArea.nColStart, aDestArea.nRowStart, aDestArea.nTab,
660 									aDestArea.nColEnd, aDestArea.nRowEnd, aDestArea.nTab,
661 									IDF_ALL, sal_False, pUndoDoc );
662 
663 			// alten Ausgabebereich
664 			if (pDestData)
665 				aDocument.CopyToDocument( aOldDest, IDF_ALL, sal_False, pUndoDoc );
666 
667 			GetUndoManager()->AddUndoAction(
668 					new ScUndoConsolidate( this, aDestArea, rParam, pUndoDoc,
669 											sal_False, 0, NULL, pUndoData ) );
670 		}
671 	}
672 
673 	if (pDestData)										// Zielbereich loeschen / anpassen
674 	{
675 		aDocument.DeleteAreaTab(aOldDest, IDF_CONTENTS);
676 		pDestData->SetArea( rParam.nTab, rParam.nCol, rParam.nRow,
677 							rParam.nCol + nColSize - 1, rParam.nRow + nRowSize - 1 );
678 		pDestData->SetHeader( rParam.bByRow );
679 	}
680 
681 	aData.OutputToDocument( &aDocument, rParam.nCol, rParam.nRow, rParam.nTab );
682 
683 	SCCOL nPaintStartCol = rParam.nCol;
684 	SCROW nPaintStartRow = rParam.nRow;
685 	SCCOL nPaintEndCol = nPaintStartCol + nColSize - 1;
686 	SCROW nPaintEndRow = nPaintStartRow + nRowSize - 1;
687 	sal_uInt16 nPaintFlags = PAINT_GRID;
688 	if (rParam.bByCol)
689 		++nPaintEndRow;
690 	if (rParam.bByRow)
691 		++nPaintEndCol;
692 	if (rParam.bReferenceData)
693 	{
694 		nPaintStartCol = 0;
695 		nPaintEndCol = MAXCOL;
696 		nPaintEndRow = MAXROW;
697 		nPaintFlags |= PAINT_LEFT | PAINT_SIZE;
698 	}
699 	if (pDestData)
700 	{
701 		if ( aOldDest.aEnd.Col() > nPaintEndCol )
702 			nPaintEndCol = aOldDest.aEnd.Col();
703 		if ( aOldDest.aEnd.Row() > nPaintEndRow )
704 			nPaintEndRow = aOldDest.aEnd.Row();
705 	}
706 	PostPaint( nPaintStartCol, nPaintStartRow, rParam.nTab,
707 				nPaintEndCol, nPaintEndRow, rParam.nTab, nPaintFlags );
708 	aModificator.SetDocumentModified();
709 }
710 
UseScenario(SCTAB nTab,const String & rName,sal_Bool bRecord)711 void ScDocShell::UseScenario( SCTAB nTab, const String& rName, sal_Bool bRecord )
712 {
713 	if (!aDocument.IsScenario(nTab))
714 	{
715 		SCTAB	nTabCount = aDocument.GetTableCount();
716 		SCTAB	nSrcTab = SCTAB_MAX;
717 		SCTAB	nEndTab = nTab;
718 		String aCompare;
719 		while ( nEndTab+1 < nTabCount && aDocument.IsScenario(nEndTab+1) )
720 		{
721 			++nEndTab;
722 			if (nSrcTab > MAXTAB)			// noch auf der Suche nach dem Szenario?
723 			{
724 				aDocument.GetName( nEndTab, aCompare );
725 				if (aCompare == rName)
726 					nSrcTab = nEndTab;		// gefunden
727 			}
728 		}
729 		if (ValidTab(nSrcTab))
730 		{
731 			if ( aDocument.TestCopyScenario( nSrcTab, nTab ) )			// Zellschutz testen
732 			{
733 				ScDocShellModificator aModificator( *this );
734 				ScMarkData aScenMark;
735 				aDocument.MarkScenario( nSrcTab, nTab, aScenMark );
736 				ScRange aMultiRange;
737 				aScenMark.GetMultiMarkArea( aMultiRange );
738 				SCCOL nStartCol = aMultiRange.aStart.Col();
739 				SCROW nStartRow = aMultiRange.aStart.Row();
740 				SCCOL nEndCol = aMultiRange.aEnd.Col();
741 				SCROW nEndRow = aMultiRange.aEnd.Row();
742 
743 				if (bRecord)
744 				{
745 					ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
746 					pUndoDoc->InitUndo( &aDocument, nTab,nEndTab );				// auch alle Szenarien
747 					//	angezeigte Tabelle:
748 					aDocument.CopyToDocument( nStartCol,nStartRow,nTab,
749 									nEndCol,nEndRow,nTab, IDF_ALL,sal_True, pUndoDoc, &aScenMark );
750 					//	Szenarien
751 					for (SCTAB i=nTab+1; i<=nEndTab; i++)
752 					{
753 						pUndoDoc->SetScenario( i, sal_True );
754 						String aComment;
755 						Color  aColor;
756 						sal_uInt16 nScenFlags;
757 						aDocument.GetScenarioData( i, aComment, aColor, nScenFlags );
758 						pUndoDoc->SetScenarioData( i, aComment, aColor, nScenFlags );
759 						sal_Bool bActive = aDocument.IsActiveScenario( i );
760 						pUndoDoc->SetActiveScenario( i, bActive );
761 						//	Bei Zurueckkopier-Szenarios auch Inhalte
762 						if ( nScenFlags & SC_SCENARIO_TWOWAY )
763 							aDocument.CopyToDocument( 0,0,i, MAXCOL,MAXROW,i,
764 														IDF_ALL,sal_False, pUndoDoc );
765 					}
766 
767 					GetUndoManager()->AddUndoAction(
768 						new ScUndoUseScenario( this, aScenMark,
769 										ScArea( nTab,nStartCol,nStartRow,nEndCol,nEndRow ),
770 										pUndoDoc, rName ) );
771 				}
772 
773 				aDocument.CopyScenario( nSrcTab, nTab );
774 				aDocument.SetDirty();
775 
776 				//	alles painten, weil in anderen Bereichen das aktive Szenario
777 				//	geaendert sein kann
778 				//!	nur, wenn sichtbare Rahmen vorhanden?
779 				PostPaint( 0,0,nTab, MAXCOL,MAXROW,nTab, PAINT_GRID );
780 				aModificator.SetDocumentModified();
781 			}
782 			else
783 			{
784 				InfoBox aBox(GetActiveDialogParent(),
785 					ScGlobal::GetRscString( STR_PROTECTIONERR ) );
786 				aBox.Execute();
787 			}
788 		}
789 		else
790 		{
791 			InfoBox aBox(GetActiveDialogParent(),
792 				ScGlobal::GetRscString( STR_SCENARIO_NOTFOUND ) );
793 			aBox.Execute();
794 		}
795 	}
796 	else
797 	{
798 		DBG_ERROR( "UseScenario auf Szenario-Blatt" );
799 	}
800 }
801 
ModifyScenario(SCTAB nTab,const String & rName,const String & rComment,const Color & rColor,sal_uInt16 nFlags)802 void ScDocShell::ModifyScenario( SCTAB nTab, const String& rName, const String& rComment,
803 									const Color& rColor, sal_uInt16 nFlags )
804 {
805 	//	Undo
806 	String aOldName;
807 	aDocument.GetName( nTab, aOldName );
808 	String aOldComment;
809 	Color aOldColor;
810 	sal_uInt16 nOldFlags;
811 	aDocument.GetScenarioData( nTab, aOldComment, aOldColor, nOldFlags );
812 	GetUndoManager()->AddUndoAction(
813 		new ScUndoScenarioFlags( this, nTab,
814 				aOldName, rName, aOldComment, rComment,
815 				aOldColor, rColor, nOldFlags, nFlags ) );
816 
817 	//	ausfuehren
818 	ScDocShellModificator aModificator( *this );
819 	aDocument.RenameTab( nTab, rName );
820 	aDocument.SetScenarioData( nTab, rComment, rColor, nFlags );
821 	PostPaintGridAll();
822 	aModificator.SetDocumentModified();
823 
824 	if ( rName != aOldName )
825 		SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
826 
827 	SfxBindings* pBindings = GetViewBindings();
828 	if (pBindings)
829 		pBindings->Invalidate( SID_SELECT_SCENARIO );
830 }
831 
MakeScenario(SCTAB nTab,const String & rName,const String & rComment,const Color & rColor,sal_uInt16 nFlags,ScMarkData & rMark,sal_Bool bRecord)832 SCTAB ScDocShell::MakeScenario( SCTAB nTab, const String& rName, const String& rComment,
833 									const Color& rColor, sal_uInt16 nFlags,
834 									ScMarkData& rMark, sal_Bool bRecord )
835 {
836 	rMark.MarkToMulti();
837 	if (rMark.IsMultiMarked())
838 	{
839 		SCTAB nNewTab = nTab + 1;
840 		while (aDocument.IsScenario(nNewTab))
841 			++nNewTab;
842 
843 		sal_Bool bCopyAll = ( (nFlags & SC_SCENARIO_COPYALL) != 0 );
844 		const ScMarkData* pCopyMark = NULL;
845 		if (!bCopyAll)
846 			pCopyMark = &rMark;
847 
848 		ScDocShellModificator aModificator( *this );
849 
850         if (bRecord)
851             aDocument.BeginDrawUndo();      // drawing layer must do its own undo actions
852 
853 		if (aDocument.CopyTab( nTab, nNewTab, pCopyMark ))
854 		{
855 			if (bRecord)
856 			{
857 				GetUndoManager()->AddUndoAction(
858                         new ScUndoMakeScenario( this, nTab, nNewTab,
859 												rName, rComment, rColor, nFlags, rMark ));
860 			}
861 
862 			aDocument.RenameTab( nNewTab, rName, sal_False );			// ohne Formel-Update
863 			aDocument.SetScenario( nNewTab, sal_True );
864 			aDocument.SetScenarioData( nNewTab, rComment, rColor, nFlags );
865 
866 			ScMarkData aDestMark = rMark;
867 			aDestMark.SelectOneTable( nNewTab );
868 
869 			//!		auf Filter / Buttons / Merging testen !
870 
871 			ScPatternAttr aProtPattern( aDocument.GetPool() );
872 			aProtPattern.GetItemSet().Put( ScProtectionAttr( sal_True ) );
873 			aDocument.ApplyPatternAreaTab( 0,0, MAXCOL,MAXROW, nNewTab, aProtPattern );
874 
875 			ScPatternAttr aPattern( aDocument.GetPool() );
876 			aPattern.GetItemSet().Put( ScMergeFlagAttr( SC_MF_SCENARIO ) );
877 			aPattern.GetItemSet().Put( ScProtectionAttr( sal_True ) );
878 			aDocument.ApplySelectionPattern( aPattern, aDestMark );
879 
880 			if (!bCopyAll)
881 				aDocument.SetVisible( nNewTab, sal_False );
882 
883 			//	dies ist dann das aktive Szenario
884 			aDocument.CopyScenario( nNewTab, nTab, sal_True );	// sal_True - nicht aus Szenario kopieren
885 
886 			if (nFlags & SC_SCENARIO_SHOWFRAME)
887 				PostPaint( 0,0,nTab, MAXCOL,MAXROW,nTab, PAINT_GRID );	// Rahmen painten
888 			PostPaintExtras();											// Tabellenreiter
889 			aModificator.SetDocumentModified();
890 
891 			SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
892 
893 			return nNewTab;
894 		}
895 	}
896 	return nTab;
897 }
898 
MoveTable(SCTAB nSrcTab,SCTAB nDestTab,sal_Bool bCopy,sal_Bool bRecord)899 sal_Bool ScDocShell::MoveTable( SCTAB nSrcTab, SCTAB nDestTab, sal_Bool bCopy, sal_Bool bRecord )
900 {
901 	ScDocShellModificator aModificator( *this );
902 
903     // #i92477# be consistent with ScDocFunc::InsertTable: any index past the last sheet means "append"
904     // #i101139# nDestTab must be the target position, not APPEND (for CopyTabProtection etc.)
905     if ( nDestTab >= aDocument.GetTableCount() )
906         nDestTab = aDocument.GetTableCount();
907 
908 	if (bCopy)
909 	{
910 		if (bRecord)
911 			aDocument.BeginDrawUndo();			// drawing layer must do its own undo actions
912 
913         String sSrcCodeName;
914         aDocument.GetCodeName( nSrcTab, sSrcCodeName );
915 		if (!aDocument.CopyTab( nSrcTab, nDestTab ))
916 		{
917 			//!	EndDrawUndo?
918 			return sal_False;
919 		}
920 		else
921 		{
922 			SCTAB nAdjSource = nSrcTab;
923 			if ( nDestTab <= nSrcTab )
924 				++nAdjSource;				// new position of source table after CopyTab
925 
926 			if ( aDocument.IsTabProtected( nAdjSource ) )
927                 aDocument.CopyTabProtection(nAdjSource, nDestTab);
928 
929 			if (bRecord)
930 			{
931 				SvShorts aSrcList;
932 				SvShorts aDestList;
933 				aSrcList.push_front(nSrcTab);
934 				aDestList.push_front(nDestTab);
935 				GetUndoManager()->AddUndoAction(
936 						new ScUndoCopyTab( this, aSrcList, aDestList ) );
937 			}
938 
939 			sal_Bool bVbaEnabled = aDocument.IsInVBAMode();
940                         if ( bVbaEnabled )
941                         {
942 			    StarBASIC* pStarBASIC = GetBasic();
943                             String aLibName( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
944                             if ( GetBasicManager()->GetName().Len() > 0 )
945                             {
946                                 aLibName = GetBasicManager()->GetName();
947                                 pStarBASIC = GetBasicManager()->GetLib( aLibName );
948                             }
949                             SCTAB nTabToUse = nDestTab;
950                             if ( nDestTab == SC_TAB_APPEND )
951                                 nTabToUse = aDocument.GetMaxTableNumber() - 1;
952                             String sCodeName;
953                             String sSource;
954                             com::sun::star::uno::Reference< com::sun::star::script::XLibraryContainer > xLibContainer = GetBasicContainer();
955                             com::sun::star::uno::Reference< com::sun::star::container::XNameContainer > xLib;
956                             if( xLibContainer.is() )
957                             {
958                                 com::sun::star::uno::Any aLibAny = xLibContainer->getByName( aLibName );
959                                 aLibAny >>= xLib;
960                             }
961                             if( xLib.is() )
962                             {
963                                 rtl::OUString sRTLSource;
964                                 xLib->getByName( sSrcCodeName ) >>= sRTLSource;
965                                 sSource = sRTLSource;
966                             }
967                             VBA_InsertModule( aDocument, nTabToUse, sCodeName, sSource );
968                         }
969                 }
970 		Broadcast( ScTablesHint( SC_TAB_COPIED, nSrcTab, nDestTab ) );
971 	}
972 	else
973 	{
974 		if ( aDocument.GetChangeTrack() )
975 			return sal_False;
976 
977 		if ( nSrcTab<nDestTab && nDestTab!=SC_TAB_APPEND )
978 			nDestTab--;
979 
980 		if ( nSrcTab == nDestTab )
981 		{
982 			//!	allow only for api calls?
983 			return sal_True;	// nothing to do, but valid
984 		}
985 
986 		if (!aDocument.MoveTab( nSrcTab, nDestTab ))
987 			return sal_False;
988 		else if (bRecord)
989 		{
990 			SvShorts aSrcList;
991 			SvShorts aDestList;
992 			aSrcList.push_front(nSrcTab);
993 			aDestList.push_front(nDestTab);
994 			GetUndoManager()->AddUndoAction(
995 					new ScUndoMoveTab( this, aSrcList, aDestList ) );
996 		}
997 
998 		Broadcast( ScTablesHint( SC_TAB_MOVED, nSrcTab, nDestTab ) );
999 	}
1000 
1001 	PostPaintGridAll();
1002 	PostPaintExtras();
1003 	aModificator.SetDocumentModified();
1004 	SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED ) );
1005 
1006 	return sal_True;
1007 }
1008 
1009 
IMPL_LINK(ScDocShell,RefreshDBDataHdl,ScRefreshTimer *,pRefreshTimer)1010 IMPL_LINK( ScDocShell, RefreshDBDataHdl, ScRefreshTimer*, pRefreshTimer )
1011 {
1012 	ScDBDocFunc aFunc(*this);
1013 
1014 	sal_Bool bContinue = sal_True;
1015     ScDBData* pDBData = static_cast<ScDBData*>(pRefreshTimer);
1016 	ScImportParam aImportParam;
1017 	pDBData->GetImportParam( aImportParam );
1018 	if (aImportParam.bImport && !pDBData->HasImportSelection())
1019 	{
1020 		ScRange aRange;
1021 		pDBData->GetArea( aRange );
1022         bContinue = aFunc.DoImport( aRange.aStart.Tab(), aImportParam, NULL, sal_True, sal_False ); //! Api-Flag as parameter
1023 		// internal operations (sort, query, subtotal) only if no error
1024 		if (bContinue)
1025 		{
1026 			aFunc.RepeatDB( pDBData->GetName(), sal_True, sal_True );
1027 			RefreshPivotTables(aRange);
1028 		}
1029 	}
1030 
1031 	return bContinue != 0;
1032 }
1033 
1034