xref: /aoo42x/main/sc/source/ui/docshell/dbdocfun.cxx (revision 22407013)
1b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b3f79822SAndrew Rist  * distributed with this work for additional information
6b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b3f79822SAndrew Rist  *
11b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b3f79822SAndrew Rist  *
13b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17b3f79822SAndrew Rist  * specific language governing permissions and limitations
18b3f79822SAndrew Rist  * under the License.
19b3f79822SAndrew Rist  *
20b3f79822SAndrew Rist  *************************************************************/
21b3f79822SAndrew Rist 
22b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir // INCLUDE ---------------------------------------------------------------
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sfx2/app.hxx>
32cdf0e10cSrcweir #include <vcl/msgbox.hxx>
33cdf0e10cSrcweir #include <vcl/waitobj.hxx>
34cdf0e10cSrcweir #include <svx/dataaccessdescriptor.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include "dbdocfun.hxx"
39cdf0e10cSrcweir #include "sc.hrc"
40cdf0e10cSrcweir #include "dbcolect.hxx"
41cdf0e10cSrcweir #include "undodat.hxx"
42cdf0e10cSrcweir #include "docsh.hxx"
43cdf0e10cSrcweir #include "docfunc.hxx"
44cdf0e10cSrcweir #include "globstr.hrc"
45cdf0e10cSrcweir #include "tabvwsh.hxx"
46cdf0e10cSrcweir #include "patattr.hxx"
47cdf0e10cSrcweir #include "rangenam.hxx"
48cdf0e10cSrcweir #include "olinetab.hxx"
49cdf0e10cSrcweir #include "dpobject.hxx"
50cdf0e10cSrcweir #include "dociter.hxx"		// for lcl_EmptyExcept
51cdf0e10cSrcweir #include "cell.hxx"			// for lcl_EmptyExcept
52cdf0e10cSrcweir #include "editable.hxx"
53cdf0e10cSrcweir #include "attrib.hxx"
54cdf0e10cSrcweir #include "drwlayer.hxx"
55cdf0e10cSrcweir #include "dpshttab.hxx"
56cdf0e10cSrcweir #include "hints.hxx"
57cdf0e10cSrcweir 
58cdf0e10cSrcweir using namespace ::com::sun::star;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir // -----------------------------------------------------------------
61cdf0e10cSrcweir 
AddDBRange(const String & rName,const ScRange & rRange,sal_Bool)62cdf0e10cSrcweir sal_Bool ScDBDocFunc::AddDBRange( const String& rName, const ScRange& rRange, sal_Bool /* bApi */ )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	ScDocShellModificator aModificator( rDocShell );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
68cdf0e10cSrcweir 	ScDBCollection* pDocColl = pDoc->GetDBCollection();
69cdf0e10cSrcweir 	sal_Bool bUndo (pDoc->IsUndoEnabled());
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	ScDBCollection* pUndoColl = NULL;
72cdf0e10cSrcweir 	if (bUndo)
73cdf0e10cSrcweir 		pUndoColl = new ScDBCollection( *pDocColl );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	ScDBData* pNew = new ScDBData( rName, rRange.aStart.Tab(),
76cdf0e10cSrcweir 									rRange.aStart.Col(), rRange.aStart.Row(),
77cdf0e10cSrcweir 									rRange.aEnd.Col(), rRange.aEnd.Row() );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     // #i55926# While loading XML, formula cells only have a single string token,
80cdf0e10cSrcweir     // so CompileDBFormula would never find any name (index) tokens, and would
81cdf0e10cSrcweir     // unnecessarily loop through all cells.
82cdf0e10cSrcweir     sal_Bool bCompile = !pDoc->IsImportingXML();
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     if ( bCompile )
85cdf0e10cSrcweir         pDoc->CompileDBFormula( sal_True );     // CreateFormulaString
86cdf0e10cSrcweir 	sal_Bool bOk = pDocColl->Insert( pNew );
87cdf0e10cSrcweir     if ( bCompile )
88cdf0e10cSrcweir         pDoc->CompileDBFormula( sal_False );    // CompileFormulaString
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	if (!bOk)
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		delete pNew;
93cdf0e10cSrcweir 		delete pUndoColl;
94cdf0e10cSrcweir 		return sal_False;
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	if (bUndo)
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir 		ScDBCollection* pRedoColl = new ScDBCollection( *pDocColl );
100cdf0e10cSrcweir 		rDocShell.GetUndoManager()->AddUndoAction(
101cdf0e10cSrcweir 						new ScUndoDBData( &rDocShell, pUndoColl, pRedoColl ) );
102cdf0e10cSrcweir 	}
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	aModificator.SetDocumentModified();
105cdf0e10cSrcweir 	SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) );
106cdf0e10cSrcweir 	return sal_True;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
DeleteDBRange(const String & rName,sal_Bool)109cdf0e10cSrcweir sal_Bool ScDBDocFunc::DeleteDBRange( const String& rName, sal_Bool /* bApi */ )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir 	sal_Bool bDone = sal_False;
112cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
113cdf0e10cSrcweir 	ScDBCollection* pDocColl = pDoc->GetDBCollection();
114cdf0e10cSrcweir 	sal_Bool bUndo (pDoc->IsUndoEnabled());
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	sal_uInt16 nPos = 0;
117cdf0e10cSrcweir 	if (pDocColl->SearchName( rName, nPos ))
118cdf0e10cSrcweir 	{
119cdf0e10cSrcweir 		ScDocShellModificator aModificator( rDocShell );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		ScDBCollection* pUndoColl = NULL;
122cdf0e10cSrcweir 		if (bUndo)
123cdf0e10cSrcweir 			pUndoColl = new ScDBCollection( *pDocColl );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		pDoc->CompileDBFormula( sal_True );		// CreateFormulaString
126cdf0e10cSrcweir 		pDocColl->AtFree( nPos );
127cdf0e10cSrcweir 		pDoc->CompileDBFormula( sal_False );	// CompileFormulaString
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 		if (bUndo)
130cdf0e10cSrcweir 		{
131cdf0e10cSrcweir 			ScDBCollection* pRedoColl = new ScDBCollection( *pDocColl );
132cdf0e10cSrcweir 			rDocShell.GetUndoManager()->AddUndoAction(
133cdf0e10cSrcweir 							new ScUndoDBData( &rDocShell, pUndoColl, pRedoColl ) );
134cdf0e10cSrcweir 		}
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		aModificator.SetDocumentModified();
137cdf0e10cSrcweir 		SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) );
138cdf0e10cSrcweir 		bDone = sal_True;
139cdf0e10cSrcweir 	}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	return bDone;
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
RenameDBRange(const String & rOld,const String & rNew,sal_Bool)144cdf0e10cSrcweir sal_Bool ScDBDocFunc::RenameDBRange( const String& rOld, const String& rNew, sal_Bool /* bApi */ )
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	sal_Bool bDone = sal_False;
147cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
148cdf0e10cSrcweir 	ScDBCollection* pDocColl = pDoc->GetDBCollection();
149cdf0e10cSrcweir 	sal_Bool bUndo (pDoc->IsUndoEnabled());
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	sal_uInt16 nPos = 0;
152cdf0e10cSrcweir 	sal_uInt16 nDummy = 0;
153cdf0e10cSrcweir 	if ( pDocColl->SearchName( rOld, nPos ) &&
154cdf0e10cSrcweir 		 !pDocColl->SearchName( rNew, nDummy ) )
155cdf0e10cSrcweir 	{
156cdf0e10cSrcweir 		ScDocShellModificator aModificator( rDocShell );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 		ScDBData* pData = (*pDocColl)[nPos];
159cdf0e10cSrcweir 		ScDBData* pNewData = new ScDBData(*pData);
160cdf0e10cSrcweir 		pNewData->SetName(rNew);
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 		ScDBCollection* pUndoColl = new ScDBCollection( *pDocColl );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 		pDoc->CompileDBFormula( sal_True );				// CreateFormulaString
165cdf0e10cSrcweir 		pDocColl->AtFree( nPos );
166cdf0e10cSrcweir 		sal_Bool bInserted = pDocColl->Insert( pNewData );
167cdf0e10cSrcweir 		if (!bInserted)								// Fehler -> alten Zustand wiederherstellen
168cdf0e10cSrcweir 		{
169cdf0e10cSrcweir 			delete pNewData;
170cdf0e10cSrcweir 			pDoc->SetDBCollection( pUndoColl );		// gehoert dann dem Dokument
171cdf0e10cSrcweir 		}
172cdf0e10cSrcweir 		pDoc->CompileDBFormula( sal_False );			// CompileFormulaString
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		if (bInserted)								// Einfuegen hat geklappt
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir 			if (bUndo)
177cdf0e10cSrcweir 			{
178cdf0e10cSrcweir 				ScDBCollection* pRedoColl = new ScDBCollection( *pDocColl );
179cdf0e10cSrcweir 				rDocShell.GetUndoManager()->AddUndoAction(
180cdf0e10cSrcweir 								new ScUndoDBData( &rDocShell, pUndoColl, pRedoColl ) );
181cdf0e10cSrcweir 			}
182cdf0e10cSrcweir 			else
183cdf0e10cSrcweir 				delete pUndoColl;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 			aModificator.SetDocumentModified();
186cdf0e10cSrcweir 			SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) );
187cdf0e10cSrcweir 			bDone = sal_True;
188cdf0e10cSrcweir 		}
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	return bDone;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
ModifyDBData(const ScDBData & rNewData,sal_Bool)194cdf0e10cSrcweir sal_Bool ScDBDocFunc::ModifyDBData( const ScDBData& rNewData, sal_Bool /* bApi */ )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	sal_Bool bDone = sal_False;
197cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
198cdf0e10cSrcweir 	ScDBCollection* pDocColl = pDoc->GetDBCollection();
199cdf0e10cSrcweir 	sal_Bool bUndo (pDoc->IsUndoEnabled());
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	sal_uInt16 nPos = 0;
202cdf0e10cSrcweir 	if (pDocColl->SearchName( rNewData.GetName(), nPos ))
203cdf0e10cSrcweir 	{
204cdf0e10cSrcweir 		ScDocShellModificator aModificator( rDocShell );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 		ScDBData* pData = (*pDocColl)[nPos];
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		ScRange aOldRange, aNewRange;
209cdf0e10cSrcweir 		pData->GetArea(aOldRange);
210cdf0e10cSrcweir 		rNewData.GetArea(aNewRange);
211cdf0e10cSrcweir 		sal_Bool bAreaChanged = ( aOldRange != aNewRange );		// dann muss neu compiliert werden
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 		ScDBCollection* pUndoColl = NULL;
214cdf0e10cSrcweir 		if (bUndo)
215cdf0e10cSrcweir 			pUndoColl = new ScDBCollection( *pDocColl );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 		*pData = rNewData;
218cdf0e10cSrcweir 		if (bAreaChanged)
219cdf0e10cSrcweir 			pDoc->CompileDBFormula();
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 		if (bUndo)
222cdf0e10cSrcweir 		{
223cdf0e10cSrcweir 			ScDBCollection* pRedoColl = new ScDBCollection( *pDocColl );
224cdf0e10cSrcweir 			rDocShell.GetUndoManager()->AddUndoAction(
225cdf0e10cSrcweir 							new ScUndoDBData( &rDocShell, pUndoColl, pRedoColl ) );
226cdf0e10cSrcweir 		}
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 		aModificator.SetDocumentModified();
229cdf0e10cSrcweir 		bDone = sal_True;
230cdf0e10cSrcweir 	}
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 	return bDone;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir // -----------------------------------------------------------------
236cdf0e10cSrcweir 
RepeatDB(const String & rDBName,sal_Bool bRecord,sal_Bool bApi)237cdf0e10cSrcweir sal_Bool ScDBDocFunc::RepeatDB( const String& rDBName, sal_Bool bRecord, sal_Bool bApi )
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	//!	auch fuer ScDBFunc::RepeatDB benutzen!
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 	sal_Bool bDone = sal_False;
242cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
243cdf0e10cSrcweir 	if (bRecord && !pDoc->IsUndoEnabled())
244cdf0e10cSrcweir 		bRecord = sal_False;
245cdf0e10cSrcweir 	ScDBCollection*	pColl = pDoc->GetDBCollection();
246cdf0e10cSrcweir 	sal_uInt16 nIndex;
247cdf0e10cSrcweir 	if ( pColl && pColl->SearchName( rDBName, nIndex ) )
248cdf0e10cSrcweir 	{
249cdf0e10cSrcweir 		ScDBData* pDBData = (*pColl)[nIndex];
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 		ScQueryParam aQueryParam;
252cdf0e10cSrcweir 		pDBData->GetQueryParam( aQueryParam );
253cdf0e10cSrcweir 		sal_Bool bQuery = aQueryParam.GetEntry(0).bDoQuery;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 		ScSortParam aSortParam;
256cdf0e10cSrcweir 		pDBData->GetSortParam( aSortParam );
257cdf0e10cSrcweir 		sal_Bool bSort = aSortParam.bDoSort[0];
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 		ScSubTotalParam aSubTotalParam;
260cdf0e10cSrcweir 		pDBData->GetSubTotalParam( aSubTotalParam );
261cdf0e10cSrcweir 		sal_Bool bSubTotal = aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 		if ( bQuery || bSort || bSubTotal )
264cdf0e10cSrcweir 		{
265cdf0e10cSrcweir 			sal_Bool bQuerySize = sal_False;
266cdf0e10cSrcweir 			ScRange aOldQuery;
267cdf0e10cSrcweir 			ScRange aNewQuery;
268cdf0e10cSrcweir 			if (bQuery && !aQueryParam.bInplace)
269cdf0e10cSrcweir 			{
270cdf0e10cSrcweir 				ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
271cdf0e10cSrcweir 														aQueryParam.nDestTab, sal_True );
272cdf0e10cSrcweir 				if (pDest && pDest->IsDoSize())
273cdf0e10cSrcweir 				{
274cdf0e10cSrcweir 					pDest->GetArea( aOldQuery );
275cdf0e10cSrcweir 					bQuerySize = sal_True;
276cdf0e10cSrcweir 				}
277cdf0e10cSrcweir 			}
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 			SCTAB nTab;
280cdf0e10cSrcweir 			SCCOL nStartCol;
281cdf0e10cSrcweir 			SCROW nStartRow;
282cdf0e10cSrcweir 			SCCOL nEndCol;
283cdf0e10cSrcweir 			SCROW nEndRow;
284cdf0e10cSrcweir 			pDBData->GetArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow );
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 			//!		Undo nur benoetigte Daten ?
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 			ScDocument* pUndoDoc = NULL;
289cdf0e10cSrcweir 			ScOutlineTable* pUndoTab = NULL;
290cdf0e10cSrcweir 			ScRangeName* pUndoRange = NULL;
291cdf0e10cSrcweir 			ScDBCollection* pUndoDB = NULL;
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 			if (bRecord)
294cdf0e10cSrcweir 			{
295cdf0e10cSrcweir 				SCTAB nTabCount = pDoc->GetTableCount();
296cdf0e10cSrcweir 				pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
297cdf0e10cSrcweir 				ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
298cdf0e10cSrcweir 				if (pTable)
299cdf0e10cSrcweir 				{
300cdf0e10cSrcweir 					pUndoTab = new ScOutlineTable( *pTable );
301cdf0e10cSrcweir 
302cdf0e10cSrcweir                     // column/row state
303cdf0e10cSrcweir 					SCCOLROW nOutStartCol, nOutEndCol;
304cdf0e10cSrcweir 					SCCOLROW nOutStartRow, nOutEndRow;
305cdf0e10cSrcweir 					pTable->GetColArray()->GetRange( nOutStartCol, nOutEndCol );
306cdf0e10cSrcweir 					pTable->GetRowArray()->GetRange( nOutStartRow, nOutEndRow );
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 					pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_True, sal_True );
309cdf0e10cSrcweir                     pDoc->CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0,
310cdf0e10cSrcweir                             nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab,
311cdf0e10cSrcweir                             IDF_NONE, sal_False, pUndoDoc );
312cdf0e10cSrcweir                     pDoc->CopyToDocument( 0, static_cast<SCROW>(nOutStartRow),
313cdf0e10cSrcweir                             nTab, MAXCOL, static_cast<SCROW>(nOutEndRow), nTab,
314cdf0e10cSrcweir                             IDF_NONE, sal_False, pUndoDoc );
315cdf0e10cSrcweir 				}
316cdf0e10cSrcweir 				else
317cdf0e10cSrcweir 					pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_False, sal_True );
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 				//	Datenbereich sichern - incl. Filter-Ergebnis
320cdf0e10cSrcweir 				pDoc->CopyToDocument( 0,nStartRow,nTab, MAXCOL,nEndRow,nTab, IDF_ALL, sal_False, pUndoDoc );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 				//	alle Formeln wegen Referenzen
323cdf0e10cSrcweir 				pDoc->CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1, IDF_FORMULA, sal_False, pUndoDoc );
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 				//	DB- und andere Bereiche
326cdf0e10cSrcweir 				ScRangeName* pDocRange = pDoc->GetRangeName();
327cdf0e10cSrcweir 				if (pDocRange->GetCount())
328cdf0e10cSrcweir 					pUndoRange = new ScRangeName( *pDocRange );
329cdf0e10cSrcweir 				ScDBCollection* pDocDB = pDoc->GetDBCollection();
330cdf0e10cSrcweir 				if (pDocDB->GetCount())
331cdf0e10cSrcweir 					pUndoDB = new ScDBCollection( *pDocDB );
332cdf0e10cSrcweir 			}
333cdf0e10cSrcweir 
334cdf0e10cSrcweir 			if (bSort && bSubTotal)
335cdf0e10cSrcweir 			{
336cdf0e10cSrcweir 				//	Sortieren ohne SubTotals
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 				aSubTotalParam.bRemoveOnly = sal_True;		// wird unten wieder zurueckgesetzt
339cdf0e10cSrcweir 				DoSubTotals( nTab, aSubTotalParam, NULL, sal_False, bApi );
340cdf0e10cSrcweir 			}
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 			if (bSort)
343cdf0e10cSrcweir 			{
344cdf0e10cSrcweir 				pDBData->GetSortParam( aSortParam );			// Bereich kann sich geaendert haben
345cdf0e10cSrcweir 				Sort( nTab, aSortParam, sal_False, sal_False, bApi );
346cdf0e10cSrcweir 			}
347cdf0e10cSrcweir 			if (bQuery)
348cdf0e10cSrcweir 			{
349cdf0e10cSrcweir 				pDBData->GetQueryParam( aQueryParam );			// Bereich kann sich geaendert haben
350cdf0e10cSrcweir 				ScRange aAdvSource;
351cdf0e10cSrcweir 				if (pDBData->GetAdvancedQuerySource(aAdvSource))
352cdf0e10cSrcweir 					Query( nTab, aQueryParam, &aAdvSource, sal_False, bApi );
353cdf0e10cSrcweir 				else
354cdf0e10cSrcweir 					Query( nTab, aQueryParam, NULL, sal_False, bApi );
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 				//	bei nicht-inplace kann die Tabelle umgestellt worden sein
357cdf0e10cSrcweir //				if ( !aQueryParam.bInplace && aQueryParam.nDestTab != nTab )
358cdf0e10cSrcweir //					SetTabNo( nTab );
359cdf0e10cSrcweir 			}
360cdf0e10cSrcweir 			if (bSubTotal)
361cdf0e10cSrcweir 			{
362cdf0e10cSrcweir 				pDBData->GetSubTotalParam( aSubTotalParam );	// Bereich kann sich geaendert haben
363cdf0e10cSrcweir 				aSubTotalParam.bRemoveOnly = sal_False;
364cdf0e10cSrcweir 				DoSubTotals( nTab, aSubTotalParam, NULL, sal_False, bApi );
365cdf0e10cSrcweir 			}
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 			if (bRecord)
368cdf0e10cSrcweir 			{
369cdf0e10cSrcweir                 SCTAB nDummyTab;
370cdf0e10cSrcweir                 SCCOL nDummyCol;
371cdf0e10cSrcweir                 SCROW nDummyRow;
372cdf0e10cSrcweir 				SCROW nNewEndRow;
373cdf0e10cSrcweir 				pDBData->GetArea( nDummyTab, nDummyCol,nDummyRow, nDummyCol,nNewEndRow );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 				const ScRange* pOld = NULL;
376cdf0e10cSrcweir 				const ScRange* pNew = NULL;
377cdf0e10cSrcweir 				if (bQuerySize)
378cdf0e10cSrcweir 				{
379cdf0e10cSrcweir 					ScDBData* pDest = pDoc->GetDBAtCursor( aQueryParam.nDestCol, aQueryParam.nDestRow,
380cdf0e10cSrcweir 															aQueryParam.nDestTab, sal_True );
381cdf0e10cSrcweir 					if (pDest)
382cdf0e10cSrcweir 					{
383cdf0e10cSrcweir 						pDest->GetArea( aNewQuery );
384cdf0e10cSrcweir 						pOld = &aOldQuery;
385cdf0e10cSrcweir 						pNew = &aNewQuery;
386cdf0e10cSrcweir 					}
387cdf0e10cSrcweir 				}
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 				rDocShell.GetUndoManager()->AddUndoAction(
390cdf0e10cSrcweir 					new ScUndoRepeatDB( &rDocShell, nTab,
391cdf0e10cSrcweir 											nStartCol, nStartRow, nEndCol, nEndRow,
392cdf0e10cSrcweir 											nNewEndRow,
393cdf0e10cSrcweir 											//nCurX, nCurY,
394cdf0e10cSrcweir 											nStartCol, nStartRow,
395cdf0e10cSrcweir 											pUndoDoc, pUndoTab,
396cdf0e10cSrcweir 											pUndoRange, pUndoDB,
397cdf0e10cSrcweir 											pOld, pNew ) );
398cdf0e10cSrcweir 			}
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 			rDocShell.PostPaint( 0,0,nTab, MAXCOL,MAXROW,nTab,
401cdf0e10cSrcweir 									PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE );
402cdf0e10cSrcweir 			bDone = sal_True;
403cdf0e10cSrcweir 		}
404cdf0e10cSrcweir 		else if (!bApi)		// "Keine Operationen auszufuehren"
405cdf0e10cSrcweir 			rDocShell.ErrorMessage(STR_MSSG_REPEATDB_0);
406cdf0e10cSrcweir 	}
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 	return bDone;
409cdf0e10cSrcweir }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir // -----------------------------------------------------------------
412cdf0e10cSrcweir 
Sort(SCTAB nTab,const ScSortParam & rSortParam,sal_Bool bRecord,sal_Bool bPaint,sal_Bool bApi)413cdf0e10cSrcweir sal_Bool ScDBDocFunc::Sort( SCTAB nTab, const ScSortParam& rSortParam,
414cdf0e10cSrcweir 							sal_Bool bRecord, sal_Bool bPaint, sal_Bool bApi )
415cdf0e10cSrcweir {
416cdf0e10cSrcweir 	ScDocShellModificator aModificator( rDocShell );
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
419cdf0e10cSrcweir 	if (bRecord && !pDoc->IsUndoEnabled())
420cdf0e10cSrcweir 		bRecord = sal_False;
421cdf0e10cSrcweir 	SCTAB nSrcTab = nTab;
422cdf0e10cSrcweir     ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 	ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rSortParam.nCol1, rSortParam.nRow1,
425cdf0e10cSrcweir 													rSortParam.nCol2, rSortParam.nRow2 );
426cdf0e10cSrcweir 	if (!pDBData)
427cdf0e10cSrcweir 	{
428cdf0e10cSrcweir 		DBG_ERROR( "Sort: keine DBData" );
429cdf0e10cSrcweir 		return sal_False;
430cdf0e10cSrcweir 	}
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 	ScDBData* pDestData = NULL;
433cdf0e10cSrcweir 	ScRange aOldDest;
434cdf0e10cSrcweir 	sal_Bool bCopy = !rSortParam.bInplace;
435cdf0e10cSrcweir 	if ( bCopy && rSortParam.nDestCol == rSortParam.nCol1 &&
436cdf0e10cSrcweir 				  rSortParam.nDestRow == rSortParam.nRow1 && rSortParam.nDestTab == nTab )
437cdf0e10cSrcweir 		bCopy = sal_False;
438cdf0e10cSrcweir 	ScSortParam aLocalParam( rSortParam );
439cdf0e10cSrcweir 	if ( bCopy )
440cdf0e10cSrcweir 	{
441cdf0e10cSrcweir 		aLocalParam.MoveToDest();
442cdf0e10cSrcweir         if ( !ValidColRow( aLocalParam.nCol2, aLocalParam.nRow2 ) )
443cdf0e10cSrcweir         {
444cdf0e10cSrcweir             if (!bApi)
445cdf0e10cSrcweir                 rDocShell.ErrorMessage(STR_PASTE_FULL);
446cdf0e10cSrcweir             return sal_False;
447cdf0e10cSrcweir         }
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 		nTab = rSortParam.nDestTab;
450cdf0e10cSrcweir 		pDestData = pDoc->GetDBAtCursor( rSortParam.nDestCol, rSortParam.nDestRow,
451cdf0e10cSrcweir 											rSortParam.nDestTab, sal_True );
452cdf0e10cSrcweir 		if (pDestData)
453cdf0e10cSrcweir 			pDestData->GetArea(aOldDest);
454cdf0e10cSrcweir 	}
455cdf0e10cSrcweir 
456cdf0e10cSrcweir 	ScEditableTester aTester( pDoc, nTab, aLocalParam.nCol1,aLocalParam.nRow1,
457cdf0e10cSrcweir 										aLocalParam.nCol2,aLocalParam.nRow2 );
458cdf0e10cSrcweir 	if (!aTester.IsEditable())
459cdf0e10cSrcweir 	{
460cdf0e10cSrcweir 		if (!bApi)
461cdf0e10cSrcweir 			rDocShell.ErrorMessage(aTester.GetMessageId());
462cdf0e10cSrcweir 		return sal_False;
463cdf0e10cSrcweir 	}
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 	if ( aLocalParam.bIncludePattern && pDoc->HasAttrib(
466cdf0e10cSrcweir 										aLocalParam.nCol1, aLocalParam.nRow1, nTab,
467cdf0e10cSrcweir 										aLocalParam.nCol2, aLocalParam.nRow2, nTab,
468cdf0e10cSrcweir 										HASATTR_MERGED | HASATTR_OVERLAPPED ) )
469cdf0e10cSrcweir 	{
470cdf0e10cSrcweir 		//	Merge-Attribute wuerden beim Sortieren durcheinanderkommen
471cdf0e10cSrcweir 		if (!bApi)
472cdf0e10cSrcweir 			rDocShell.ErrorMessage(STR_SORT_ERR_MERGED);
473cdf0e10cSrcweir 		return sal_False;
474cdf0e10cSrcweir 	}
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 	//		ausfuehren
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 	WaitObject aWait( rDocShell.GetActiveDialogParent() );
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 	sal_Bool bRepeatQuery = sal_False;							// bestehenden Filter wiederholen?
482cdf0e10cSrcweir 	ScQueryParam aQueryParam;
483cdf0e10cSrcweir 	pDBData->GetQueryParam( aQueryParam );
484cdf0e10cSrcweir 	if ( aQueryParam.GetEntry(0).bDoQuery )
485cdf0e10cSrcweir 		bRepeatQuery = sal_True;
486cdf0e10cSrcweir 
487cdf0e10cSrcweir 	if (bRepeatQuery && bCopy)
488cdf0e10cSrcweir 	{
489cdf0e10cSrcweir 		if ( aQueryParam.bInplace ||
490cdf0e10cSrcweir 				aQueryParam.nDestCol != rSortParam.nDestCol ||
491cdf0e10cSrcweir 				aQueryParam.nDestRow != rSortParam.nDestRow ||
492cdf0e10cSrcweir 				aQueryParam.nDestTab != rSortParam.nDestTab )		// Query auf selben Zielbereich?
493cdf0e10cSrcweir 			bRepeatQuery = sal_False;
494cdf0e10cSrcweir 	}
495cdf0e10cSrcweir 
496cdf0e10cSrcweir     ScUndoSort* pUndoAction = 0;
497cdf0e10cSrcweir     if ( bRecord )
498cdf0e10cSrcweir     {
499cdf0e10cSrcweir         //  Referenzen ausserhalb des Bereichs werden nicht veraendert !
500cdf0e10cSrcweir 
501cdf0e10cSrcweir         ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
502cdf0e10cSrcweir         //  Zeilenhoehen immer (wegen automatischer Anpassung)
503cdf0e10cSrcweir         //! auf ScBlockUndo umstellen
504cdf0e10cSrcweir         pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_False, sal_True );
505cdf0e10cSrcweir 
506cdf0e10cSrcweir         /*  #i59745# Do not copy note captions to undo document. All existing
507cdf0e10cSrcweir             caption objects will be repositioned while sorting which is tracked
508cdf0e10cSrcweir             in drawing undo. When undo is executed, the old positions will be
509cdf0e10cSrcweir             restored, and the cells with the old notes (which still refer to the
510cdf0e10cSrcweir             existing captions) will be copied back into the source document. */
511cdf0e10cSrcweir         pDoc->CopyToDocument( aLocalParam.nCol1, aLocalParam.nRow1, nTab,
512cdf0e10cSrcweir                                 aLocalParam.nCol2, aLocalParam.nRow2, nTab,
513cdf0e10cSrcweir                                 IDF_ALL|IDF_NOCAPTIONS, sal_False, pUndoDoc );
514cdf0e10cSrcweir 
515cdf0e10cSrcweir         const ScRange* pR = 0;
516cdf0e10cSrcweir         if (pDestData)
517cdf0e10cSrcweir         {
518cdf0e10cSrcweir             /*  #i59745# Do not copy note captions from destination range to
519cdf0e10cSrcweir                 undo document. All existing caption objects will be removed
520cdf0e10cSrcweir                 which is tracked in drawing undo. When undo is executed, the
521cdf0e10cSrcweir                 caption objects are reinserted with drawing undo, and the cells
522cdf0e10cSrcweir                 with the old notes (which still refer to the existing captions)
523cdf0e10cSrcweir                 will be copied back into the source document. */
524cdf0e10cSrcweir             pDoc->CopyToDocument( aOldDest, IDF_ALL|IDF_NOCAPTIONS, sal_False, pUndoDoc );
525cdf0e10cSrcweir             pR = &aOldDest;
526cdf0e10cSrcweir         }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 		//	Zeilenhoehen immer (wegen automatischer Anpassung)
529cdf0e10cSrcweir 		//!	auf ScBlockUndo umstellen
530cdf0e10cSrcweir //        if (bRepeatQuery)
531cdf0e10cSrcweir             pDoc->CopyToDocument( 0, aLocalParam.nRow1, nTab, MAXCOL, aLocalParam.nRow2, nTab,
532cdf0e10cSrcweir                                     IDF_NONE, sal_False, pUndoDoc );
533cdf0e10cSrcweir 
534cdf0e10cSrcweir         ScDBCollection* pUndoDB = NULL;
535cdf0e10cSrcweir         ScDBCollection* pDocDB = pDoc->GetDBCollection();
536cdf0e10cSrcweir         if (pDocDB->GetCount())
537cdf0e10cSrcweir             pUndoDB = new ScDBCollection( *pDocDB );
538cdf0e10cSrcweir 
539cdf0e10cSrcweir         pUndoAction = new ScUndoSort( &rDocShell, nTab, rSortParam, bRepeatQuery, pUndoDoc, pUndoDB, pR );
540cdf0e10cSrcweir         rDocShell.GetUndoManager()->AddUndoAction( pUndoAction );
541cdf0e10cSrcweir 
542cdf0e10cSrcweir         // #i59745# collect all drawing undo actions affecting cell note captions
543cdf0e10cSrcweir         if( pDrawLayer )
544a840a559SArmin Le Grand             pDrawLayer->BeginCalcUndo(false);
545cdf0e10cSrcweir     }
546cdf0e10cSrcweir 
547cdf0e10cSrcweir 	if ( bCopy )
548cdf0e10cSrcweir 	{
549cdf0e10cSrcweir 		if (pDestData)
550cdf0e10cSrcweir 			pDoc->DeleteAreaTab(aOldDest, IDF_CONTENTS);			// Zielbereich vorher loeschen
551cdf0e10cSrcweir 
552cdf0e10cSrcweir 		ScRange aSource( rSortParam.nCol1,rSortParam.nRow1,nSrcTab,
553cdf0e10cSrcweir 							rSortParam.nCol2,rSortParam.nRow2,nSrcTab );
554cdf0e10cSrcweir 		ScAddress aDest( rSortParam.nDestCol, rSortParam.nDestRow, rSortParam.nDestTab );
555cdf0e10cSrcweir 
556cdf0e10cSrcweir 		rDocShell.GetDocFunc().MoveBlock( aSource, aDest, sal_False, sal_False, sal_False, sal_True );
557cdf0e10cSrcweir 	}
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 	// #105780# don't call ScDocument::Sort with an empty SortParam (may be empty here if bCopy is set)
560cdf0e10cSrcweir 	if ( aLocalParam.bDoSort[0] )
561cdf0e10cSrcweir 		pDoc->Sort( nTab, aLocalParam, bRepeatQuery );
562cdf0e10cSrcweir 
563cdf0e10cSrcweir 	sal_Bool bSave = sal_True;
564cdf0e10cSrcweir 	if (bCopy)
565cdf0e10cSrcweir 	{
566cdf0e10cSrcweir 		ScSortParam aOldSortParam;
567cdf0e10cSrcweir 		pDBData->GetSortParam( aOldSortParam );
568cdf0e10cSrcweir 		if ( aOldSortParam.bDoSort[0] && aOldSortParam.bInplace )	// Inplace-Sortierung gemerkt?
569cdf0e10cSrcweir 		{
570cdf0e10cSrcweir 			bSave = sal_False;
571cdf0e10cSrcweir 			aOldSortParam.nDestCol = rSortParam.nDestCol;
572cdf0e10cSrcweir 			aOldSortParam.nDestRow = rSortParam.nDestRow;
573cdf0e10cSrcweir 			aOldSortParam.nDestTab = rSortParam.nDestTab;
574cdf0e10cSrcweir 			pDBData->SetSortParam( aOldSortParam ); 				// dann nur DestPos merken
575cdf0e10cSrcweir 		}
576cdf0e10cSrcweir 	}
577cdf0e10cSrcweir 	if (bSave)												// Parameter merken
578cdf0e10cSrcweir 	{
579cdf0e10cSrcweir 		pDBData->SetSortParam( rSortParam );
580cdf0e10cSrcweir 		pDBData->SetHeader( rSortParam.bHasHeader );		//! ???
581cdf0e10cSrcweir 		pDBData->SetByRow( rSortParam.bByRow );				//! ???
582cdf0e10cSrcweir 	}
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 	if (bCopy)											// neuen DB-Bereich merken
585cdf0e10cSrcweir 	{
586cdf0e10cSrcweir 		//	Tabelle umschalten von aussen (View)
587cdf0e10cSrcweir 		//!	SetCursor ??!?!
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 		ScRange aDestPos( aLocalParam.nCol1, aLocalParam.nRow1, nTab,
590cdf0e10cSrcweir 							aLocalParam.nCol2, aLocalParam.nRow2, nTab );
591cdf0e10cSrcweir 		ScDBData* pNewData;
592cdf0e10cSrcweir 		if (pDestData)
593cdf0e10cSrcweir 			pNewData = pDestData;				// Bereich vorhanden -> anpassen
594cdf0e10cSrcweir 		else									// Bereich ab Cursor/Markierung wird angelegt
595*22407013SOliver-Rainer Wittmann 			pNewData = rDocShell.GetDBData(aDestPos, SC_DB_MAKE, SC_DBSEL_FORCE_MARK );
596cdf0e10cSrcweir 		if (pNewData)
597cdf0e10cSrcweir 		{
598cdf0e10cSrcweir 			pNewData->SetArea( nTab,
599cdf0e10cSrcweir 								aLocalParam.nCol1,aLocalParam.nRow1,
600cdf0e10cSrcweir 								aLocalParam.nCol2,aLocalParam.nRow2 );
601cdf0e10cSrcweir 			pNewData->SetSortParam( aLocalParam );
602cdf0e10cSrcweir 			pNewData->SetHeader( aLocalParam.bHasHeader );		//! ???
603cdf0e10cSrcweir 			pNewData->SetByRow( aLocalParam.bByRow );
604cdf0e10cSrcweir 		}
605cdf0e10cSrcweir 		else
606cdf0e10cSrcweir 		{
607cdf0e10cSrcweir 			DBG_ERROR("Zielbereich nicht da");
608cdf0e10cSrcweir 		}
609cdf0e10cSrcweir 	}
610cdf0e10cSrcweir 
611cdf0e10cSrcweir 	ScRange aDirtyRange( aLocalParam.nCol1, aLocalParam.nRow1, nTab,
612cdf0e10cSrcweir 		aLocalParam.nCol2, aLocalParam.nRow2, nTab );
613cdf0e10cSrcweir 	pDoc->SetDirty( aDirtyRange );
614cdf0e10cSrcweir 
615cdf0e10cSrcweir 	if (bPaint)
616cdf0e10cSrcweir 	{
617cdf0e10cSrcweir 		sal_uInt16 nPaint = PAINT_GRID;
618cdf0e10cSrcweir 		SCCOL nStartX = aLocalParam.nCol1;
619cdf0e10cSrcweir 		SCROW nStartY = aLocalParam.nRow1;
620cdf0e10cSrcweir 		SCCOL nEndX = aLocalParam.nCol2;
621cdf0e10cSrcweir 		SCROW nEndY = aLocalParam.nRow2;
622cdf0e10cSrcweir 		if ( bRepeatQuery )
623cdf0e10cSrcweir 		{
624cdf0e10cSrcweir 			nPaint |= PAINT_LEFT;
625cdf0e10cSrcweir 			nStartX = 0;
626cdf0e10cSrcweir 			nEndX = MAXCOL;
627cdf0e10cSrcweir 		}
628cdf0e10cSrcweir 		if (pDestData)
629cdf0e10cSrcweir 		{
630cdf0e10cSrcweir 			if ( nEndX < aOldDest.aEnd.Col() )
631cdf0e10cSrcweir 				nEndX = aOldDest.aEnd.Col();
632cdf0e10cSrcweir 			if ( nEndY < aOldDest.aEnd.Row() )
633cdf0e10cSrcweir 				nEndY = aOldDest.aEnd.Row();
634cdf0e10cSrcweir 		}
635cdf0e10cSrcweir 		rDocShell.PostPaint( nStartX, nStartY, nTab, nEndX, nEndY, nTab, nPaint );
636cdf0e10cSrcweir 	}
637cdf0e10cSrcweir 
638cdf0e10cSrcweir     //  AdjustRowHeight( aLocalParam.nRow1, aLocalParam.nRow2, bPaint );
639cdf0e10cSrcweir 	rDocShell.AdjustRowHeight( aLocalParam.nRow1, aLocalParam.nRow2, nTab );
640cdf0e10cSrcweir 
641cdf0e10cSrcweir     // #i59745# set collected drawing undo actions at sorting undo action
642cdf0e10cSrcweir     if( pUndoAction && pDrawLayer )
643cdf0e10cSrcweir         pUndoAction->SetDrawUndoAction( pDrawLayer->GetCalcUndo() );
644cdf0e10cSrcweir 
645cdf0e10cSrcweir 	aModificator.SetDocumentModified();
646cdf0e10cSrcweir 
647cdf0e10cSrcweir 	return sal_True;
648cdf0e10cSrcweir }
649cdf0e10cSrcweir 
650cdf0e10cSrcweir // -----------------------------------------------------------------
651cdf0e10cSrcweir 
Query(SCTAB nTab,const ScQueryParam & rQueryParam,const ScRange * pAdvSource,sal_Bool bRecord,sal_Bool bApi)652cdf0e10cSrcweir sal_Bool ScDBDocFunc::Query( SCTAB nTab, const ScQueryParam& rQueryParam,
653cdf0e10cSrcweir 						const ScRange* pAdvSource, sal_Bool bRecord, sal_Bool bApi )
654cdf0e10cSrcweir {
655cdf0e10cSrcweir 	ScDocShellModificator aModificator( rDocShell );
656cdf0e10cSrcweir 
657cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
658cdf0e10cSrcweir 	if (bRecord && !pDoc->IsUndoEnabled())
659cdf0e10cSrcweir 		bRecord = sal_False;
660cdf0e10cSrcweir 	ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rQueryParam.nCol1, rQueryParam.nRow1,
661cdf0e10cSrcweir 													rQueryParam.nCol2, rQueryParam.nRow2 );
662cdf0e10cSrcweir 	if (!pDBData)
663cdf0e10cSrcweir 	{
664cdf0e10cSrcweir 		DBG_ERROR( "Query: keine DBData" );
665cdf0e10cSrcweir 		return sal_False;
666cdf0e10cSrcweir 	}
667cdf0e10cSrcweir 
668cdf0e10cSrcweir 	//	Wechsel von Inplace auf nicht-Inplace, dann erst Inplace aufheben:
669cdf0e10cSrcweir 	//	(nur, wenn im Dialog "Persistent" ausgewaehlt ist)
670cdf0e10cSrcweir 
671cdf0e10cSrcweir 	if ( !rQueryParam.bInplace && pDBData->HasQueryParam() && rQueryParam.bDestPers )
672cdf0e10cSrcweir 	{
673cdf0e10cSrcweir 		ScQueryParam aOldQuery;
674cdf0e10cSrcweir 		pDBData->GetQueryParam(aOldQuery);
675cdf0e10cSrcweir 		if (aOldQuery.bInplace)
676cdf0e10cSrcweir 		{
677cdf0e10cSrcweir 			//	alte Filterung aufheben
678cdf0e10cSrcweir 
679cdf0e10cSrcweir 			SCSIZE nEC = aOldQuery.GetEntryCount();
680cdf0e10cSrcweir 			for (SCSIZE i=0; i<nEC; i++)
681cdf0e10cSrcweir 				aOldQuery.GetEntry(i).bDoQuery = sal_False;
682cdf0e10cSrcweir 			aOldQuery.bDuplicate = sal_True;
683cdf0e10cSrcweir 			Query( nTab, aOldQuery, NULL, bRecord, bApi );
684cdf0e10cSrcweir 		}
685cdf0e10cSrcweir 	}
686cdf0e10cSrcweir 
687cdf0e10cSrcweir 	ScQueryParam aLocalParam( rQueryParam );		// fuer Paint / Zielbereich
688cdf0e10cSrcweir 	sal_Bool bCopy = !rQueryParam.bInplace; 			// kopiert wird in Table::Query
689cdf0e10cSrcweir 	ScDBData* pDestData = NULL;						// Bereich, in den kopiert wird
690cdf0e10cSrcweir 	sal_Bool bDoSize = sal_False;							// Zielgroesse anpassen (einf./loeschen)
691cdf0e10cSrcweir 	SCCOL nFormulaCols = 0;						// nur bei bDoSize
692cdf0e10cSrcweir 	sal_Bool bKeepFmt = sal_False;
693cdf0e10cSrcweir 	ScRange aOldDest;
694cdf0e10cSrcweir 	ScRange aDestTotal;
695cdf0e10cSrcweir 	if ( bCopy && rQueryParam.nDestCol == rQueryParam.nCol1 &&
696cdf0e10cSrcweir 				  rQueryParam.nDestRow == rQueryParam.nRow1 && rQueryParam.nDestTab == nTab )
697cdf0e10cSrcweir 		bCopy = sal_False;
698cdf0e10cSrcweir 	SCTAB nDestTab = nTab;
699cdf0e10cSrcweir 	if ( bCopy )
700cdf0e10cSrcweir 	{
701cdf0e10cSrcweir 		aLocalParam.MoveToDest();
702cdf0e10cSrcweir 		nDestTab = rQueryParam.nDestTab;
703cdf0e10cSrcweir         if ( !ValidColRow( aLocalParam.nCol2, aLocalParam.nRow2 ) )
704cdf0e10cSrcweir         {
705cdf0e10cSrcweir             if (!bApi)
706cdf0e10cSrcweir                 rDocShell.ErrorMessage(STR_PASTE_FULL);
707cdf0e10cSrcweir             return sal_False;
708cdf0e10cSrcweir         }
709cdf0e10cSrcweir 
710cdf0e10cSrcweir 		ScEditableTester aTester( pDoc, nDestTab, aLocalParam.nCol1,aLocalParam.nRow1,
711cdf0e10cSrcweir 												aLocalParam.nCol2,aLocalParam.nRow2);
712cdf0e10cSrcweir 		if (!aTester.IsEditable())
713cdf0e10cSrcweir 		{
714cdf0e10cSrcweir 			if (!bApi)
715cdf0e10cSrcweir 				rDocShell.ErrorMessage(aTester.GetMessageId());
716cdf0e10cSrcweir 			return sal_False;
717cdf0e10cSrcweir 		}
718cdf0e10cSrcweir 
719cdf0e10cSrcweir 		pDestData = pDoc->GetDBAtCursor( rQueryParam.nDestCol, rQueryParam.nDestRow,
720cdf0e10cSrcweir 											rQueryParam.nDestTab, sal_True );
721cdf0e10cSrcweir 		if (pDestData)
722cdf0e10cSrcweir 		{
723cdf0e10cSrcweir 			pDestData->GetArea( aOldDest );
724cdf0e10cSrcweir 			aDestTotal=ScRange( rQueryParam.nDestCol,
725cdf0e10cSrcweir 								rQueryParam.nDestRow,
726cdf0e10cSrcweir 								nDestTab,
727cdf0e10cSrcweir 								rQueryParam.nDestCol + rQueryParam.nCol2 - rQueryParam.nCol1,
728cdf0e10cSrcweir 								rQueryParam.nDestRow + rQueryParam.nRow2 - rQueryParam.nRow1,
729cdf0e10cSrcweir 								nDestTab );
730cdf0e10cSrcweir 
731cdf0e10cSrcweir 			bDoSize = pDestData->IsDoSize();
732cdf0e10cSrcweir 			//	Test, ob Formeln aufgefuellt werden muessen (nFormulaCols):
733cdf0e10cSrcweir 			if ( bDoSize && aOldDest.aEnd.Col() == aDestTotal.aEnd.Col() )
734cdf0e10cSrcweir 			{
735cdf0e10cSrcweir 				SCCOL nTestCol = aOldDest.aEnd.Col() + 1;		// neben dem Bereich
736cdf0e10cSrcweir 				SCROW nTestRow = rQueryParam.nDestRow +
737cdf0e10cSrcweir 									( aLocalParam.bHasHeader ? 1 : 0 );
738cdf0e10cSrcweir 				while ( nTestCol <= MAXCOL &&
739cdf0e10cSrcweir 						pDoc->GetCellType(ScAddress( nTestCol, nTestRow, nTab )) == CELLTYPE_FORMULA )
740cdf0e10cSrcweir 					++nTestCol, ++nFormulaCols;
741cdf0e10cSrcweir 			}
742cdf0e10cSrcweir 
743cdf0e10cSrcweir 			bKeepFmt = pDestData->IsKeepFmt();
744cdf0e10cSrcweir 			if ( bDoSize && !pDoc->CanFitBlock( aOldDest, aDestTotal ) )
745cdf0e10cSrcweir 			{
746cdf0e10cSrcweir 				if (!bApi)
747cdf0e10cSrcweir 					rDocShell.ErrorMessage(STR_MSSG_DOSUBTOTALS_2);		// kann keine Zeilen einfuegen
748cdf0e10cSrcweir 				return sal_False;
749cdf0e10cSrcweir 			}
750cdf0e10cSrcweir 		}
751cdf0e10cSrcweir 	}
752cdf0e10cSrcweir 
753cdf0e10cSrcweir 	//		ausfuehren
754cdf0e10cSrcweir 
755cdf0e10cSrcweir 	WaitObject aWait( rDocShell.GetActiveDialogParent() );
756cdf0e10cSrcweir 
757cdf0e10cSrcweir 	sal_Bool bKeepSub = sal_False;							// bestehende Teilergebnisse wiederholen?
758cdf0e10cSrcweir 	ScSubTotalParam aSubTotalParam;
759cdf0e10cSrcweir 	if (rQueryParam.GetEntry(0).bDoQuery)			// nicht beim Aufheben
760cdf0e10cSrcweir 	{
761cdf0e10cSrcweir 		pDBData->GetSubTotalParam( aSubTotalParam );	// Teilergebnisse vorhanden?
762cdf0e10cSrcweir 
763cdf0e10cSrcweir 		if ( aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly )
764cdf0e10cSrcweir 			bKeepSub = sal_True;
765cdf0e10cSrcweir 	}
766cdf0e10cSrcweir 
767cdf0e10cSrcweir     ScDocument* pUndoDoc = NULL;
768cdf0e10cSrcweir     ScDBCollection* pUndoDB = NULL;
769cdf0e10cSrcweir     const ScRange* pOld = NULL;
770cdf0e10cSrcweir 
771cdf0e10cSrcweir 	if ( bRecord )
772cdf0e10cSrcweir 	{
773cdf0e10cSrcweir 		pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
774cdf0e10cSrcweir 		if (bCopy)
775cdf0e10cSrcweir 		{
776cdf0e10cSrcweir 			pUndoDoc->InitUndo( pDoc, nDestTab, nDestTab, sal_False, sal_True );
777cdf0e10cSrcweir 			pDoc->CopyToDocument( aLocalParam.nCol1, aLocalParam.nRow1, nDestTab,
778cdf0e10cSrcweir 									aLocalParam.nCol2, aLocalParam.nRow2, nDestTab,
779cdf0e10cSrcweir 									IDF_ALL, sal_False, pUndoDoc );
780cdf0e10cSrcweir 			//	Attribute sichern, falls beim Filtern mitkopiert
781cdf0e10cSrcweir 
782cdf0e10cSrcweir 			if (pDestData)
783cdf0e10cSrcweir 			{
784cdf0e10cSrcweir 				pDoc->CopyToDocument( aOldDest, IDF_ALL, sal_False, pUndoDoc );
785cdf0e10cSrcweir 				pOld = &aOldDest;
786cdf0e10cSrcweir 			}
787cdf0e10cSrcweir 		}
788cdf0e10cSrcweir 		else
789cdf0e10cSrcweir 		{
790cdf0e10cSrcweir 			pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_False, sal_True );
791cdf0e10cSrcweir 			pDoc->CopyToDocument( 0, rQueryParam.nRow1, nTab, MAXCOL, rQueryParam.nRow2, nTab,
792cdf0e10cSrcweir 										IDF_NONE, sal_False, pUndoDoc );
793cdf0e10cSrcweir 		}
794cdf0e10cSrcweir 
795cdf0e10cSrcweir 		ScDBCollection* pDocDB = pDoc->GetDBCollection();
796cdf0e10cSrcweir 		if (pDocDB->GetCount())
797cdf0e10cSrcweir 			pUndoDB = new ScDBCollection( *pDocDB );
798cdf0e10cSrcweir 
799cdf0e10cSrcweir         pDoc->BeginDrawUndo();
800cdf0e10cSrcweir 	}
801cdf0e10cSrcweir 
802cdf0e10cSrcweir 	ScDocument* pAttribDoc = NULL;
803cdf0e10cSrcweir 	ScRange aAttribRange;
804cdf0e10cSrcweir 	if (pDestData)										// Zielbereich loeschen
805cdf0e10cSrcweir 	{
806cdf0e10cSrcweir 		if ( bKeepFmt )
807cdf0e10cSrcweir 		{
808cdf0e10cSrcweir 			//	kleinere der End-Spalten, Header+1 Zeile
809cdf0e10cSrcweir 			aAttribRange = aOldDest;
810cdf0e10cSrcweir 			if ( aAttribRange.aEnd.Col() > aDestTotal.aEnd.Col() )
811cdf0e10cSrcweir 				aAttribRange.aEnd.SetCol( aDestTotal.aEnd.Col() );
812cdf0e10cSrcweir 			aAttribRange.aEnd.SetRow( aAttribRange.aStart.Row() +
813cdf0e10cSrcweir 										( aLocalParam.bHasHeader ? 1 : 0 ) );
814cdf0e10cSrcweir 
815cdf0e10cSrcweir 			//	auch fuer aufgefuellte Formeln
816cdf0e10cSrcweir 			aAttribRange.aEnd.SetCol( aAttribRange.aEnd.Col() + nFormulaCols );
817cdf0e10cSrcweir 
818cdf0e10cSrcweir 			pAttribDoc = new ScDocument( SCDOCMODE_UNDO );
819cdf0e10cSrcweir 			pAttribDoc->InitUndo( pDoc, nDestTab, nDestTab, sal_False, sal_True );
820cdf0e10cSrcweir 			pDoc->CopyToDocument( aAttribRange, IDF_ATTRIB, sal_False, pAttribDoc );
821cdf0e10cSrcweir 		}
822cdf0e10cSrcweir 
823cdf0e10cSrcweir 		if ( bDoSize )
824cdf0e10cSrcweir 			pDoc->FitBlock( aOldDest, aDestTotal );
825cdf0e10cSrcweir 		else
826cdf0e10cSrcweir 			pDoc->DeleteAreaTab(aOldDest, IDF_ALL);			// einfach loeschen
827cdf0e10cSrcweir 	}
828cdf0e10cSrcweir 
829cdf0e10cSrcweir 	//	Filtern am Dokument ausfuehren
830cdf0e10cSrcweir 	SCSIZE nCount = pDoc->Query( nTab, rQueryParam, bKeepSub );
831cdf0e10cSrcweir 	if (bCopy)
832cdf0e10cSrcweir 	{
833cdf0e10cSrcweir 		aLocalParam.nRow2 = aLocalParam.nRow1 + nCount;
834cdf0e10cSrcweir 		if (!aLocalParam.bHasHeader && nCount > 0)
835cdf0e10cSrcweir 			--aLocalParam.nRow2;
836cdf0e10cSrcweir 
837cdf0e10cSrcweir 		if ( bDoSize )
838cdf0e10cSrcweir 		{
839cdf0e10cSrcweir 			//	auf wirklichen Ergebnis-Bereich anpassen
840cdf0e10cSrcweir 			//	(das hier ist immer eine Verkleinerung)
841cdf0e10cSrcweir 
842cdf0e10cSrcweir 			ScRange aNewDest( aLocalParam.nCol1, aLocalParam.nRow1, nDestTab,
843cdf0e10cSrcweir 								aLocalParam.nCol2, aLocalParam.nRow2, nDestTab );
844cdf0e10cSrcweir 			pDoc->FitBlock( aDestTotal, aNewDest, sal_False );		// sal_False - nicht loeschen
845cdf0e10cSrcweir 
846cdf0e10cSrcweir 			if ( nFormulaCols > 0 )
847cdf0e10cSrcweir 			{
848cdf0e10cSrcweir 				//	Formeln ausfuellen
849cdf0e10cSrcweir 				//!	Undo (Query und Repeat) !!!
850cdf0e10cSrcweir 
851cdf0e10cSrcweir 				ScRange aNewForm( aLocalParam.nCol2+1, aLocalParam.nRow1, nDestTab,
852cdf0e10cSrcweir 								  aLocalParam.nCol2+nFormulaCols, aLocalParam.nRow2, nDestTab );
853cdf0e10cSrcweir 				ScRange aOldForm = aNewForm;
854cdf0e10cSrcweir 				aOldForm.aEnd.SetRow( aOldDest.aEnd.Row() );
855cdf0e10cSrcweir 				pDoc->FitBlock( aOldForm, aNewForm, sal_False );
856cdf0e10cSrcweir 
857cdf0e10cSrcweir 				ScMarkData aMark;
858cdf0e10cSrcweir 				aMark.SelectOneTable(nDestTab);
859cdf0e10cSrcweir 				SCROW nFStartY = aLocalParam.nRow1 + ( aLocalParam.bHasHeader ? 1 : 0 );
860cdf0e10cSrcweir 				pDoc->Fill( aLocalParam.nCol2+1, nFStartY,
861cdf0e10cSrcweir 							aLocalParam.nCol2+nFormulaCols, nFStartY, aMark,
862cdf0e10cSrcweir 							aLocalParam.nRow2 - nFStartY,
863cdf0e10cSrcweir 							FILL_TO_BOTTOM, FILL_SIMPLE );
864cdf0e10cSrcweir 			}
865cdf0e10cSrcweir 		}
866cdf0e10cSrcweir 
867cdf0e10cSrcweir 		if ( pAttribDoc )		// gemerkte Attribute zurueckkopieren
868cdf0e10cSrcweir 		{
869cdf0e10cSrcweir 			//	Header
870cdf0e10cSrcweir 			if (aLocalParam.bHasHeader)
871cdf0e10cSrcweir 			{
872cdf0e10cSrcweir 				ScRange aHdrRange = aAttribRange;
873cdf0e10cSrcweir 				aHdrRange.aEnd.SetRow( aHdrRange.aStart.Row() );
874cdf0e10cSrcweir 				pAttribDoc->CopyToDocument( aHdrRange, IDF_ATTRIB, sal_False, pDoc );
875cdf0e10cSrcweir 			}
876cdf0e10cSrcweir 
877cdf0e10cSrcweir 			//	Daten
878cdf0e10cSrcweir 			SCCOL nAttrEndCol = aAttribRange.aEnd.Col();
879cdf0e10cSrcweir 			SCROW nAttrRow = aAttribRange.aStart.Row() + ( aLocalParam.bHasHeader ? 1 : 0 );
880cdf0e10cSrcweir 			for (SCCOL nCol = aAttribRange.aStart.Col(); nCol<=nAttrEndCol; nCol++)
881cdf0e10cSrcweir 			{
882cdf0e10cSrcweir 				const ScPatternAttr* pSrcPattern = pAttribDoc->GetPattern(
883cdf0e10cSrcweir 													nCol, nAttrRow, nDestTab );
884cdf0e10cSrcweir 				DBG_ASSERT(pSrcPattern,"Pattern ist 0");
885cdf0e10cSrcweir 				if (pSrcPattern)
886cdf0e10cSrcweir 					pDoc->ApplyPatternAreaTab( nCol, nAttrRow, nCol, aLocalParam.nRow2,
887cdf0e10cSrcweir 													nDestTab, *pSrcPattern );
888cdf0e10cSrcweir 				const ScStyleSheet* pStyle = pSrcPattern->GetStyleSheet();
889cdf0e10cSrcweir 				if (pStyle)
890cdf0e10cSrcweir 					pDoc->ApplyStyleAreaTab( nCol, nAttrRow, nCol, aLocalParam.nRow2,
891cdf0e10cSrcweir 													nDestTab, *pStyle );
892cdf0e10cSrcweir 			}
893cdf0e10cSrcweir 
894cdf0e10cSrcweir 			delete pAttribDoc;
895cdf0e10cSrcweir 		}
896cdf0e10cSrcweir 	}
897cdf0e10cSrcweir 
898cdf0e10cSrcweir 	//	speichern: Inplace immer, sonst je nach Einstellung
899cdf0e10cSrcweir 	//			   alter Inplace-Filter ist ggf. schon aufgehoben
900cdf0e10cSrcweir 
901cdf0e10cSrcweir 	sal_Bool bSave = rQueryParam.bInplace || rQueryParam.bDestPers;
902cdf0e10cSrcweir 	if (bSave)													// merken
903cdf0e10cSrcweir 	{
904cdf0e10cSrcweir 		pDBData->SetQueryParam( rQueryParam );
905cdf0e10cSrcweir 		pDBData->SetHeader( rQueryParam.bHasHeader );		//! ???
906cdf0e10cSrcweir 		pDBData->SetAdvancedQuerySource( pAdvSource );		// after SetQueryParam
907cdf0e10cSrcweir 	}
908cdf0e10cSrcweir 
909cdf0e10cSrcweir 	if (bCopy)												// neuen DB-Bereich merken
910cdf0e10cSrcweir 	{
911cdf0e10cSrcweir 		//	selektieren wird hinterher von aussen (dbfunc)
912cdf0e10cSrcweir 		//	momentan ueber DB-Bereich an der Zielposition, darum muss dort
913cdf0e10cSrcweir 		//	auf jeden Fall ein Bereich angelegt werden.
914cdf0e10cSrcweir 
915cdf0e10cSrcweir 		ScDBData* pNewData;
916cdf0e10cSrcweir 		if (pDestData)
917cdf0e10cSrcweir 			pNewData = pDestData;				// Bereich vorhanden -> anpassen (immer!)
918cdf0e10cSrcweir 		else									// Bereich anlegen
919cdf0e10cSrcweir 			pNewData = rDocShell.GetDBData(
920cdf0e10cSrcweir 							ScRange( aLocalParam.nCol1, aLocalParam.nRow1, nDestTab,
921cdf0e10cSrcweir 									 aLocalParam.nCol2, aLocalParam.nRow2, nDestTab ),
922*22407013SOliver-Rainer Wittmann 							SC_DB_MAKE, SC_DBSEL_FORCE_MARK );
923cdf0e10cSrcweir 
924cdf0e10cSrcweir 		if (pNewData)
925cdf0e10cSrcweir 		{
926cdf0e10cSrcweir 			pNewData->SetArea( nDestTab, aLocalParam.nCol1, aLocalParam.nRow1,
927cdf0e10cSrcweir 											aLocalParam.nCol2, aLocalParam.nRow2 );
928cdf0e10cSrcweir 
929cdf0e10cSrcweir 			//	Query-Param wird am Ziel nicht mehr eingestellt, fuehrt nur zu Verwirrung
930cdf0e10cSrcweir 			//	und Verwechslung mit dem Query-Param am Quellbereich (#37187#)
931cdf0e10cSrcweir 		}
932cdf0e10cSrcweir 		else
933cdf0e10cSrcweir 		{
934cdf0e10cSrcweir 			DBG_ERROR("Zielbereich nicht da");
935cdf0e10cSrcweir 		}
936cdf0e10cSrcweir 	}
937cdf0e10cSrcweir 
938cdf0e10cSrcweir 	if (!bCopy)
939cdf0e10cSrcweir     {
940cdf0e10cSrcweir         pDoc->InvalidatePageBreaks(nTab);
941cdf0e10cSrcweir 		pDoc->UpdatePageBreaks( nTab );
942cdf0e10cSrcweir     }
943cdf0e10cSrcweir 
944cdf0e10cSrcweir     // #i23299# because of Subtotal functions, the whole rows must be set dirty
945cdf0e10cSrcweir 	ScRange aDirtyRange( 0 , aLocalParam.nRow1, nDestTab,
946cdf0e10cSrcweir 		MAXCOL, aLocalParam.nRow2, nDestTab );
947cdf0e10cSrcweir 	pDoc->SetDirty( aDirtyRange );
948cdf0e10cSrcweir 
949cdf0e10cSrcweir     if ( bRecord )
950cdf0e10cSrcweir     {
951cdf0e10cSrcweir         // create undo action after executing, because of drawing layer undo
952cdf0e10cSrcweir 		rDocShell.GetUndoManager()->AddUndoAction(
953cdf0e10cSrcweir 					new ScUndoQuery( &rDocShell, nTab, rQueryParam, pUndoDoc, pUndoDB,
954cdf0e10cSrcweir 										pOld, bDoSize, pAdvSource ) );
955cdf0e10cSrcweir     }
956cdf0e10cSrcweir 
957cdf0e10cSrcweir 
958cdf0e10cSrcweir 	if (bCopy)
959cdf0e10cSrcweir 	{
960cdf0e10cSrcweir 		SCCOL nEndX = aLocalParam.nCol2;
961cdf0e10cSrcweir 		SCROW nEndY = aLocalParam.nRow2;
962cdf0e10cSrcweir 		if (pDestData)
963cdf0e10cSrcweir 		{
964cdf0e10cSrcweir 			if ( aOldDest.aEnd.Col() > nEndX )
965cdf0e10cSrcweir 				nEndX = aOldDest.aEnd.Col();
966cdf0e10cSrcweir 			if ( aOldDest.aEnd.Row() > nEndY )
967cdf0e10cSrcweir 				nEndY = aOldDest.aEnd.Row();
968cdf0e10cSrcweir 		}
969cdf0e10cSrcweir 		if (bDoSize)
970cdf0e10cSrcweir 			nEndY = MAXROW;
971cdf0e10cSrcweir 		rDocShell.PostPaint( aLocalParam.nCol1, aLocalParam.nRow1, nDestTab,
972cdf0e10cSrcweir 									nEndX, nEndY, nDestTab, PAINT_GRID );
973cdf0e10cSrcweir 	}
974cdf0e10cSrcweir 	else
975cdf0e10cSrcweir 		rDocShell.PostPaint( 0, rQueryParam.nRow1, nTab, MAXCOL, MAXROW, nTab,
976cdf0e10cSrcweir 												PAINT_GRID | PAINT_LEFT );
977cdf0e10cSrcweir 	aModificator.SetDocumentModified();
978cdf0e10cSrcweir 
979cdf0e10cSrcweir 	return sal_True;
980cdf0e10cSrcweir }
981cdf0e10cSrcweir 
982cdf0e10cSrcweir // -----------------------------------------------------------------
983cdf0e10cSrcweir 
DoSubTotals(SCTAB nTab,const ScSubTotalParam & rParam,const ScSortParam * pForceNewSort,sal_Bool bRecord,sal_Bool bApi)984cdf0e10cSrcweir sal_Bool ScDBDocFunc::DoSubTotals( SCTAB nTab, const ScSubTotalParam& rParam,
985cdf0e10cSrcweir 								const ScSortParam* pForceNewSort, sal_Bool bRecord, sal_Bool bApi )
986cdf0e10cSrcweir {
987cdf0e10cSrcweir 	//!	auch fuer ScDBFunc::DoSubTotals benutzen!
988cdf0e10cSrcweir 	//	dann bleibt aussen:
989cdf0e10cSrcweir 	//	- neuen Bereich (aus DBData) markieren
990cdf0e10cSrcweir 	//	- SelectionChanged (?)
991cdf0e10cSrcweir 
992cdf0e10cSrcweir 	sal_Bool bDo = !rParam.bRemoveOnly;							// sal_False = nur loeschen
993cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
994cdf0e10cSrcweir 
995cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
996cdf0e10cSrcweir 	if (bRecord && !pDoc->IsUndoEnabled())
997cdf0e10cSrcweir 		bRecord = sal_False;
998cdf0e10cSrcweir 	ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
999cdf0e10cSrcweir 												rParam.nCol2, rParam.nRow2 );
1000cdf0e10cSrcweir 	if (!pDBData)
1001cdf0e10cSrcweir 	{
1002cdf0e10cSrcweir 		DBG_ERROR( "SubTotals: keine DBData" );
1003cdf0e10cSrcweir 		return sal_False;
1004cdf0e10cSrcweir 	}
1005cdf0e10cSrcweir 
1006cdf0e10cSrcweir 	ScEditableTester aTester( pDoc, nTab, 0,rParam.nRow1+1, MAXCOL,MAXROW );
1007cdf0e10cSrcweir 	if (!aTester.IsEditable())
1008cdf0e10cSrcweir 	{
1009cdf0e10cSrcweir 		if (!bApi)
1010cdf0e10cSrcweir 			rDocShell.ErrorMessage(aTester.GetMessageId());
1011cdf0e10cSrcweir 		return sal_False;
1012cdf0e10cSrcweir 	}
1013cdf0e10cSrcweir 
1014cdf0e10cSrcweir 	if (pDoc->HasAttrib( rParam.nCol1, rParam.nRow1+1, nTab,
1015cdf0e10cSrcweir 						 rParam.nCol2, rParam.nRow2, nTab, HASATTR_MERGED | HASATTR_OVERLAPPED ))
1016cdf0e10cSrcweir 	{
1017cdf0e10cSrcweir 		if (!bApi)
1018cdf0e10cSrcweir 			rDocShell.ErrorMessage(STR_MSSG_INSERTCELLS_0);	// nicht in zusammengefasste einfuegen
1019cdf0e10cSrcweir 		return sal_False;
1020cdf0e10cSrcweir 	}
1021cdf0e10cSrcweir 
1022cdf0e10cSrcweir 	sal_Bool bOk = sal_True;
1023cdf0e10cSrcweir 	sal_Bool bDelete = sal_False;
1024cdf0e10cSrcweir 	if (rParam.bReplace)
1025cdf0e10cSrcweir 		if (pDoc->TestRemoveSubTotals( nTab, rParam ))
1026cdf0e10cSrcweir 		{
1027cdf0e10cSrcweir 			bDelete = sal_True;
1028cdf0e10cSrcweir 			bOk = ( MessBox( rDocShell.GetActiveDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
1029cdf0e10cSrcweir 				// "StarCalc" "Daten loeschen?"
1030cdf0e10cSrcweir 				ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ),
1031cdf0e10cSrcweir 				ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_1 ) ).Execute()
1032cdf0e10cSrcweir 				== RET_YES );
1033cdf0e10cSrcweir 		}
1034cdf0e10cSrcweir 
1035cdf0e10cSrcweir 	if (bOk)
1036cdf0e10cSrcweir 	{
1037cdf0e10cSrcweir 		WaitObject aWait( rDocShell.GetActiveDialogParent() );
1038cdf0e10cSrcweir 		ScDocShellModificator aModificator( rDocShell );
1039cdf0e10cSrcweir 
1040cdf0e10cSrcweir 		ScSubTotalParam aNewParam( rParam );		// Bereichsende wird veraendert
1041cdf0e10cSrcweir 		ScDocument*		pUndoDoc = NULL;
1042cdf0e10cSrcweir 		ScOutlineTable*	pUndoTab = NULL;
1043cdf0e10cSrcweir 		ScRangeName*	pUndoRange = NULL;
1044cdf0e10cSrcweir 		ScDBCollection* pUndoDB = NULL;
1045cdf0e10cSrcweir 		SCTAB 			nTabCount = 0;				// fuer Referenz-Undo
1046cdf0e10cSrcweir 
1047cdf0e10cSrcweir 		if (bRecord)										// alte Daten sichern
1048cdf0e10cSrcweir 		{
1049cdf0e10cSrcweir 			sal_Bool bOldFilter = bDo && rParam.bDoSort;
1050cdf0e10cSrcweir 
1051cdf0e10cSrcweir 			nTabCount = pDoc->GetTableCount();
1052cdf0e10cSrcweir 			pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
1053cdf0e10cSrcweir 			ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
1054cdf0e10cSrcweir 			if (pTable)
1055cdf0e10cSrcweir 			{
1056cdf0e10cSrcweir 				pUndoTab = new ScOutlineTable( *pTable );
1057cdf0e10cSrcweir 
1058cdf0e10cSrcweir                 // column/row state
1059cdf0e10cSrcweir                 SCCOLROW nOutStartCol, nOutEndCol;
1060cdf0e10cSrcweir                 SCCOLROW nOutStartRow, nOutEndRow;
1061cdf0e10cSrcweir 				pTable->GetColArray()->GetRange( nOutStartCol, nOutEndCol );
1062cdf0e10cSrcweir 				pTable->GetRowArray()->GetRange( nOutStartRow, nOutEndRow );
1063cdf0e10cSrcweir 
1064cdf0e10cSrcweir 				pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_True, sal_True );
1065cdf0e10cSrcweir 				pDoc->CopyToDocument( static_cast<SCCOL>(nOutStartCol), 0, nTab, static_cast<SCCOL>(nOutEndCol), MAXROW, nTab, IDF_NONE, sal_False, pUndoDoc );
1066cdf0e10cSrcweir 				pDoc->CopyToDocument( 0, nOutStartRow, nTab, MAXCOL, nOutEndRow, nTab, IDF_NONE, sal_False, pUndoDoc );
1067cdf0e10cSrcweir 			}
1068cdf0e10cSrcweir 			else
1069cdf0e10cSrcweir 				pUndoDoc->InitUndo( pDoc, nTab, nTab, sal_False, bOldFilter );
1070cdf0e10cSrcweir 
1071cdf0e10cSrcweir 			//	Datenbereich sichern - incl. Filter-Ergebnis
1072cdf0e10cSrcweir 			pDoc->CopyToDocument( 0,rParam.nRow1+1,nTab, MAXCOL,rParam.nRow2,nTab,
1073cdf0e10cSrcweir 									IDF_ALL, sal_False, pUndoDoc );
1074cdf0e10cSrcweir 
1075cdf0e10cSrcweir 			//	alle Formeln wegen Referenzen
1076cdf0e10cSrcweir 			pDoc->CopyToDocument( 0,0,0, MAXCOL,MAXROW,nTabCount-1,
1077cdf0e10cSrcweir 										IDF_FORMULA, sal_False, pUndoDoc );
1078cdf0e10cSrcweir 
1079cdf0e10cSrcweir 			//	DB- und andere Bereiche
1080cdf0e10cSrcweir 			ScRangeName* pDocRange = pDoc->GetRangeName();
1081cdf0e10cSrcweir 			if (pDocRange->GetCount())
1082cdf0e10cSrcweir 				pUndoRange = new ScRangeName( *pDocRange );
1083cdf0e10cSrcweir 			ScDBCollection* pDocDB = pDoc->GetDBCollection();
1084cdf0e10cSrcweir 			if (pDocDB->GetCount())
1085cdf0e10cSrcweir 				pUndoDB = new ScDBCollection( *pDocDB );
1086cdf0e10cSrcweir 		}
1087cdf0e10cSrcweir 
1088cdf0e10cSrcweir //		pDoc->SetOutlineTable( nTab, NULL );
1089cdf0e10cSrcweir 		ScOutlineTable*	pOut = pDoc->GetOutlineTable( nTab );
1090cdf0e10cSrcweir 		if (pOut)
1091cdf0e10cSrcweir 			pOut->GetRowArray()->RemoveAll();		// nur Zeilen-Outlines loeschen
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir 		if (rParam.bReplace)
1094cdf0e10cSrcweir 			pDoc->RemoveSubTotals( nTab, aNewParam );
1095cdf0e10cSrcweir 		sal_Bool bSuccess = sal_True;
1096cdf0e10cSrcweir 		if (bDo)
1097cdf0e10cSrcweir 		{
1098cdf0e10cSrcweir 			// Sortieren
1099cdf0e10cSrcweir 			if ( rParam.bDoSort || pForceNewSort )
1100cdf0e10cSrcweir 			{
1101cdf0e10cSrcweir 				pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
1102cdf0e10cSrcweir 
1103cdf0e10cSrcweir 				//	Teilergebnis-Felder vor die Sortierung setzen
1104cdf0e10cSrcweir 				//	(doppelte werden weggelassen, kann darum auch wieder aufgerufen werden)
1105cdf0e10cSrcweir 
1106cdf0e10cSrcweir 				ScSortParam aOldSort;
1107cdf0e10cSrcweir 				pDBData->GetSortParam( aOldSort );
1108cdf0e10cSrcweir 				ScSortParam aSortParam( aNewParam, pForceNewSort ? *pForceNewSort : aOldSort );
1109cdf0e10cSrcweir 				Sort( nTab, aSortParam, sal_False, sal_False, bApi );
1110cdf0e10cSrcweir 			}
1111cdf0e10cSrcweir 
1112cdf0e10cSrcweir 			bSuccess = pDoc->DoSubTotals( nTab, aNewParam );
1113cdf0e10cSrcweir 		}
1114cdf0e10cSrcweir 		ScRange aDirtyRange( aNewParam.nCol1, aNewParam.nRow1, nTab,
1115cdf0e10cSrcweir 			aNewParam.nCol2, aNewParam.nRow2, nTab );
1116cdf0e10cSrcweir 		pDoc->SetDirty( aDirtyRange );
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir 		if (bRecord)
1119cdf0e10cSrcweir 		{
1120cdf0e10cSrcweir //			ScDBData* pUndoDBData = pDBData ? new ScDBData( *pDBData ) : NULL;
1121cdf0e10cSrcweir 			rDocShell.GetUndoManager()->AddUndoAction(
1122cdf0e10cSrcweir 				new ScUndoSubTotals( &rDocShell, nTab,
1123cdf0e10cSrcweir 										rParam, aNewParam.nRow2,
1124cdf0e10cSrcweir 										pUndoDoc, pUndoTab, // pUndoDBData,
1125cdf0e10cSrcweir 										pUndoRange, pUndoDB ) );
1126cdf0e10cSrcweir 		}
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir 		if (!bSuccess)
1129cdf0e10cSrcweir 		{
1130cdf0e10cSrcweir 			// "Kann keine Zeilen einfuegen"
1131cdf0e10cSrcweir 			if (!bApi)
1132cdf0e10cSrcweir 				rDocShell.ErrorMessage(STR_MSSG_DOSUBTOTALS_2);
1133cdf0e10cSrcweir 		}
1134cdf0e10cSrcweir 
1135cdf0e10cSrcweir 													// merken
1136cdf0e10cSrcweir 		pDBData->SetSubTotalParam( aNewParam );
1137cdf0e10cSrcweir 		pDBData->SetArea( nTab, aNewParam.nCol1,aNewParam.nRow1, aNewParam.nCol2,aNewParam.nRow2 );
1138cdf0e10cSrcweir 		pDoc->CompileDBFormula();
1139cdf0e10cSrcweir 
1140cdf0e10cSrcweir 		rDocShell.PostPaint( 0,0,nTab, MAXCOL,MAXROW,nTab,
1141cdf0e10cSrcweir 												PAINT_GRID | PAINT_LEFT | PAINT_TOP | PAINT_SIZE );
1142cdf0e10cSrcweir 		aModificator.SetDocumentModified();
1143cdf0e10cSrcweir 
1144cdf0e10cSrcweir 		bRet = bSuccess;
1145cdf0e10cSrcweir 	}
1146cdf0e10cSrcweir 	return bRet;
1147cdf0e10cSrcweir }
1148cdf0e10cSrcweir 
1149cdf0e10cSrcweir //==================================================================
1150cdf0e10cSrcweir 
lcl_EmptyExcept(ScDocument * pDoc,const ScRange & rRange,const ScRange & rExcept)1151cdf0e10cSrcweir sal_Bool lcl_EmptyExcept( ScDocument* pDoc, const ScRange& rRange, const ScRange& rExcept )
1152cdf0e10cSrcweir {
1153cdf0e10cSrcweir 	ScCellIterator aIter( pDoc, rRange );
1154cdf0e10cSrcweir 	ScBaseCell* pCell = aIter.GetFirst();
1155cdf0e10cSrcweir 	while (pCell)
1156cdf0e10cSrcweir 	{
1157cdf0e10cSrcweir         if ( !pCell->IsBlank() )      // real content?
1158cdf0e10cSrcweir 		{
1159cdf0e10cSrcweir 			if ( !rExcept.In( ScAddress( aIter.GetCol(), aIter.GetRow(), aIter.GetTab() ) ) )
1160cdf0e10cSrcweir 				return sal_False;		// cell found
1161cdf0e10cSrcweir 		}
1162cdf0e10cSrcweir 		pCell = aIter.GetNext();
1163cdf0e10cSrcweir 	}
1164cdf0e10cSrcweir 
1165cdf0e10cSrcweir 	return sal_True;		// nothing found - empty
1166cdf0e10cSrcweir }
1167cdf0e10cSrcweir 
DataPilotUpdate(ScDPObject * pOldObj,const ScDPObject * pNewObj,sal_Bool bRecord,sal_Bool bApi,sal_Bool bAllowMove)1168cdf0e10cSrcweir sal_Bool ScDBDocFunc::DataPilotUpdate( ScDPObject* pOldObj, const ScDPObject* pNewObj,
1169cdf0e10cSrcweir 										sal_Bool bRecord, sal_Bool bApi, sal_Bool bAllowMove )
1170cdf0e10cSrcweir {
1171cdf0e10cSrcweir 	ScDocShellModificator aModificator( rDocShell );
1172cdf0e10cSrcweir 	WaitObject aWait( rDocShell.GetActiveDialogParent() );
1173cdf0e10cSrcweir 
1174cdf0e10cSrcweir 	sal_Bool bDone = sal_False;
1175cdf0e10cSrcweir 	sal_Bool bUndoSelf = sal_False;
1176cdf0e10cSrcweir 	sal_uInt16 nErrId = 0;
1177cdf0e10cSrcweir 
1178cdf0e10cSrcweir 	ScDocument* pOldUndoDoc = NULL;
1179cdf0e10cSrcweir 	ScDocument* pNewUndoDoc = NULL;
1180cdf0e10cSrcweir 	ScDPObject* pUndoDPObj = NULL;
1181cdf0e10cSrcweir 	if ( bRecord && pOldObj )
1182cdf0e10cSrcweir 		pUndoDPObj = new ScDPObject( *pOldObj );	// copy old settings for undo
1183cdf0e10cSrcweir 
1184cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
1185cdf0e10cSrcweir 	if (bRecord && !pDoc->IsUndoEnabled())
1186cdf0e10cSrcweir 		bRecord = sal_False;
1187cdf0e10cSrcweir 	if ( !rDocShell.IsEditable() || pDoc->GetChangeTrack() )
1188cdf0e10cSrcweir 	{
1189cdf0e10cSrcweir 		//	not recorded -> disallow
1190cdf0e10cSrcweir 		//!	different error messages?
1191cdf0e10cSrcweir 
1192cdf0e10cSrcweir 		nErrId = STR_PROTECTIONERR;
1193cdf0e10cSrcweir 	}
1194cdf0e10cSrcweir 	if ( pOldObj && !nErrId )
1195cdf0e10cSrcweir 	{
1196cdf0e10cSrcweir 		ScRange aOldOut = pOldObj->GetOutRange();
1197cdf0e10cSrcweir 		ScEditableTester aTester( pDoc, aOldOut );
1198cdf0e10cSrcweir 		if ( !aTester.IsEditable() )
1199cdf0e10cSrcweir 			nErrId = aTester.GetMessageId();
1200cdf0e10cSrcweir 	}
1201cdf0e10cSrcweir 	if ( pNewObj && !nErrId )
1202cdf0e10cSrcweir 	{
1203cdf0e10cSrcweir 		//	at least one cell at the output position must be editable
1204cdf0e10cSrcweir 		//	-> check in advance
1205cdf0e10cSrcweir 		//	(start of output range in pNewObj is valid)
1206cdf0e10cSrcweir 
1207cdf0e10cSrcweir 		ScRange aNewStart( pNewObj->GetOutRange().aStart );
1208cdf0e10cSrcweir 		ScEditableTester aTester( pDoc, aNewStart );
1209cdf0e10cSrcweir 		if ( !aTester.IsEditable() )
1210cdf0e10cSrcweir 			nErrId = aTester.GetMessageId();
1211cdf0e10cSrcweir 	}
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir 	ScDPObject* pDestObj = NULL;
1214cdf0e10cSrcweir 	if ( !nErrId )
1215cdf0e10cSrcweir 	{
1216cdf0e10cSrcweir 		if ( pOldObj && !pNewObj )
1217cdf0e10cSrcweir 		{
1218cdf0e10cSrcweir 			//	delete table
1219cdf0e10cSrcweir 
1220cdf0e10cSrcweir 			ScRange aRange = pOldObj->GetOutRange();
1221cdf0e10cSrcweir 			SCTAB nTab = aRange.aStart.Tab();
1222cdf0e10cSrcweir 
1223cdf0e10cSrcweir 			if ( bRecord )
1224cdf0e10cSrcweir 			{
1225cdf0e10cSrcweir 				pOldUndoDoc = new ScDocument( SCDOCMODE_UNDO );
1226cdf0e10cSrcweir 				pOldUndoDoc->InitUndo( pDoc, nTab, nTab );
1227cdf0e10cSrcweir 				pDoc->CopyToDocument( aRange, IDF_ALL, sal_False, pOldUndoDoc );
1228cdf0e10cSrcweir 			}
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir 			pDoc->DeleteAreaTab( aRange.aStart.Col(), aRange.aStart.Row(),
1231cdf0e10cSrcweir 								 aRange.aEnd.Col(),   aRange.aEnd.Row(),
1232cdf0e10cSrcweir 								 nTab, IDF_ALL );
1233cdf0e10cSrcweir 			pDoc->RemoveFlagsTab( aRange.aStart.Col(), aRange.aStart.Row(),
1234cdf0e10cSrcweir 								  aRange.aEnd.Col(),   aRange.aEnd.Row(),
1235cdf0e10cSrcweir 								  nTab, SC_MF_AUTO );
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir             pDoc->GetDPCollection()->FreeTable( pOldObj );  // object is deleted here
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir 			rDocShell.PostPaintGridAll();	//! only necessary parts
1240cdf0e10cSrcweir 			rDocShell.PostPaint( aRange.aStart.Col(), aRange.aStart.Row(), nTab,
1241cdf0e10cSrcweir 								 aRange.aEnd.Col(),   aRange.aEnd.Row(),   nTab,
1242cdf0e10cSrcweir 								 PAINT_GRID );
1243cdf0e10cSrcweir 			bDone = sal_True;
1244cdf0e10cSrcweir 		}
1245cdf0e10cSrcweir 		else if ( pNewObj )
1246cdf0e10cSrcweir 		{
1247cdf0e10cSrcweir 			if ( pOldObj )
1248cdf0e10cSrcweir 			{
1249cdf0e10cSrcweir 				if ( bRecord )
1250cdf0e10cSrcweir 				{
1251cdf0e10cSrcweir 					ScRange aRange = pOldObj->GetOutRange();
1252cdf0e10cSrcweir 					SCTAB nTab = aRange.aStart.Tab();
1253cdf0e10cSrcweir 					pOldUndoDoc = new ScDocument( SCDOCMODE_UNDO );
1254cdf0e10cSrcweir 					pOldUndoDoc->InitUndo( pDoc, nTab, nTab );
1255cdf0e10cSrcweir 					pDoc->CopyToDocument( aRange, IDF_ALL, sal_False, pOldUndoDoc );
1256cdf0e10cSrcweir 				}
1257cdf0e10cSrcweir 
1258cdf0e10cSrcweir 				if ( pNewObj == pOldObj )
1259cdf0e10cSrcweir 				{
1260cdf0e10cSrcweir 					//	refresh only - no settings modified
1261cdf0e10cSrcweir 				}
1262cdf0e10cSrcweir 				else
1263cdf0e10cSrcweir 				{
1264cdf0e10cSrcweir 					pNewObj->WriteSourceDataTo( *pOldObj );		// copy source data
1265cdf0e10cSrcweir 
1266cdf0e10cSrcweir 					ScDPSaveData* pData = pNewObj->GetSaveData();
1267cdf0e10cSrcweir 					DBG_ASSERT( pData, "no SaveData from living DPObject" );
1268cdf0e10cSrcweir 					if ( pData )
1269cdf0e10cSrcweir 						pOldObj->SetSaveData( *pData );		// copy SaveData
1270cdf0e10cSrcweir 				}
1271cdf0e10cSrcweir 
1272cdf0e10cSrcweir 				pDestObj = pOldObj;
1273cdf0e10cSrcweir 				pDestObj->SetAllowMove( bAllowMove );
1274cdf0e10cSrcweir 			}
1275cdf0e10cSrcweir 			else
1276cdf0e10cSrcweir 			{
1277cdf0e10cSrcweir 				//	output range must be set at pNewObj
1278cdf0e10cSrcweir 
1279cdf0e10cSrcweir 				pDestObj = new ScDPObject( *pNewObj );
1280cdf0e10cSrcweir 
1281cdf0e10cSrcweir                 // #i94570# When changing the output position in the dialog, a new table is created
1282cdf0e10cSrcweir                 // with the settings from the old table, including the name.
1283cdf0e10cSrcweir                 // So we have to check for duplicate names here (before inserting).
1284cdf0e10cSrcweir                 if ( pDoc->GetDPCollection()->GetByName(pDestObj->GetName()) )
1285cdf0e10cSrcweir                     pDestObj->SetName( String() );      // ignore the invalid name, create a new name below
1286cdf0e10cSrcweir 
1287cdf0e10cSrcweir 				pDestObj->SetAlive(sal_True);
1288cdf0e10cSrcweir                 if ( !pDoc->GetDPCollection()->InsertNewTable(pDestObj) )
1289cdf0e10cSrcweir 				{
1290cdf0e10cSrcweir 					DBG_ERROR("cannot insert DPObject");
1291cdf0e10cSrcweir 					DELETEZ( pDestObj );
1292cdf0e10cSrcweir 				}
1293cdf0e10cSrcweir 			}
1294cdf0e10cSrcweir 			if ( pDestObj )
1295cdf0e10cSrcweir 			{
1296cdf0e10cSrcweir 				// #78541# create new database connection for "refresh"
1297cdf0e10cSrcweir 				// (and re-read column entry collections)
1298cdf0e10cSrcweir 				// so all changes take effect
1299cdf0e10cSrcweir 				if ( pNewObj == pOldObj && pDestObj->IsImportData() )
1300cdf0e10cSrcweir 					pDestObj->InvalidateSource();
1301cdf0e10cSrcweir 
1302cdf0e10cSrcweir 				pDestObj->InvalidateData();				// before getting the new output area
1303cdf0e10cSrcweir 
1304cdf0e10cSrcweir 				//	make sure the table has a name (not set by dialog)
1305cdf0e10cSrcweir 				if ( !pDestObj->GetName().Len() )
1306cdf0e10cSrcweir 					pDestObj->SetName( pDoc->GetDPCollection()->CreateNewName() );
1307cdf0e10cSrcweir 
1308cdf0e10cSrcweir 				sal_Bool bOverflow = sal_False;
1309cdf0e10cSrcweir 				ScRange aNewOut = pDestObj->GetNewOutputRange( bOverflow );
1310cdf0e10cSrcweir 
1311cdf0e10cSrcweir                 //!	test for overlap with other data pilot tables
1312cdf0e10cSrcweir                 if( pOldObj )
1313cdf0e10cSrcweir                 {
1314cdf0e10cSrcweir                     const ScSheetSourceDesc* pSheetDesc = pOldObj->GetSheetDesc();
1315cdf0e10cSrcweir                     if( pSheetDesc && pSheetDesc->aSourceRange.Intersects( aNewOut ) )
1316cdf0e10cSrcweir                     {
1317cdf0e10cSrcweir                         ScRange aOldRange = pOldObj->GetOutRange();
1318cdf0e10cSrcweir                         SCsROW nDiff = aOldRange.aStart.Row()-aNewOut.aStart.Row();
1319cdf0e10cSrcweir                         aNewOut.aStart.SetRow( aOldRange.aStart.Row() );
1320cdf0e10cSrcweir                         aNewOut.aEnd.SetRow( aNewOut.aEnd.Row()+nDiff );
1321cdf0e10cSrcweir                         if( !ValidRow( aNewOut.aStart.Row() ) || !ValidRow( aNewOut.aEnd.Row() ) )
1322cdf0e10cSrcweir                             bOverflow = sal_True;
1323cdf0e10cSrcweir                     }
1324cdf0e10cSrcweir                 }
1325cdf0e10cSrcweir 
1326cdf0e10cSrcweir 				if ( bOverflow )
1327cdf0e10cSrcweir 				{
1328cdf0e10cSrcweir 					//	like with STR_PROTECTIONERR, use undo to reverse everything
1329cdf0e10cSrcweir 					DBG_ASSERT( bRecord, "DataPilotUpdate: can't undo" );
1330cdf0e10cSrcweir 					bUndoSelf = sal_True;
1331cdf0e10cSrcweir 					nErrId = STR_PIVOT_ERROR;
1332cdf0e10cSrcweir 				}
1333cdf0e10cSrcweir 				else
1334cdf0e10cSrcweir 				{
1335cdf0e10cSrcweir 					ScEditableTester aTester( pDoc, aNewOut );
1336cdf0e10cSrcweir 					if ( !aTester.IsEditable() )
1337cdf0e10cSrcweir 					{
1338cdf0e10cSrcweir 						//	destination area isn't editable
1339cdf0e10cSrcweir 						//!	reverse everything done so far, don't proceed
1340cdf0e10cSrcweir 
1341cdf0e10cSrcweir 						//	quick solution: proceed to end, use undo action
1342cdf0e10cSrcweir 						//	to reverse everything:
1343cdf0e10cSrcweir 						DBG_ASSERT( bRecord, "DataPilotUpdate: can't undo" );
1344cdf0e10cSrcweir 						bUndoSelf = sal_True;
1345cdf0e10cSrcweir 						nErrId = aTester.GetMessageId();
1346cdf0e10cSrcweir 					}
1347cdf0e10cSrcweir 				}
1348cdf0e10cSrcweir 
1349cdf0e10cSrcweir 				//	test if new output area is empty except for old area
1350cdf0e10cSrcweir 				if ( !bApi )
1351cdf0e10cSrcweir 				{
1352cdf0e10cSrcweir 					sal_Bool bEmpty;
1353cdf0e10cSrcweir 					if ( pOldObj )	// OutRange of pOldObj (pDestObj) is still old area
1354cdf0e10cSrcweir 						bEmpty = lcl_EmptyExcept( pDoc, aNewOut, pOldObj->GetOutRange() );
1355cdf0e10cSrcweir 					else
1356cdf0e10cSrcweir 						bEmpty = pDoc->IsBlockEmpty( aNewOut.aStart.Tab(),
1357cdf0e10cSrcweir 											aNewOut.aStart.Col(), aNewOut.aStart.Row(),
1358cdf0e10cSrcweir 											aNewOut.aEnd.Col(), aNewOut.aEnd.Row() );
1359cdf0e10cSrcweir 
1360cdf0e10cSrcweir 					if ( !bEmpty )
1361cdf0e10cSrcweir 					{
1362cdf0e10cSrcweir 						QueryBox aBox( rDocShell.GetActiveDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
1363cdf0e10cSrcweir 										 ScGlobal::GetRscString(STR_PIVOT_NOTEMPTY) );
1364cdf0e10cSrcweir 						if (aBox.Execute() == RET_NO)
1365cdf0e10cSrcweir 						{
1366cdf0e10cSrcweir 							//!	like above (not editable), use undo to reverse everything
1367cdf0e10cSrcweir 							DBG_ASSERT( bRecord, "DataPilotUpdate: can't undo" );
1368cdf0e10cSrcweir 							bUndoSelf = sal_True;
1369cdf0e10cSrcweir 						}
1370cdf0e10cSrcweir 					}
1371cdf0e10cSrcweir 				}
1372cdf0e10cSrcweir 
1373cdf0e10cSrcweir 				if ( bRecord )
1374cdf0e10cSrcweir 				{
1375cdf0e10cSrcweir 					SCTAB nTab = aNewOut.aStart.Tab();
1376cdf0e10cSrcweir 					pNewUndoDoc = new ScDocument( SCDOCMODE_UNDO );
1377cdf0e10cSrcweir 					pNewUndoDoc->InitUndo( pDoc, nTab, nTab );
1378cdf0e10cSrcweir 					pDoc->CopyToDocument( aNewOut, IDF_ALL, sal_False, pNewUndoDoc );
1379cdf0e10cSrcweir 				}
1380cdf0e10cSrcweir 
1381cdf0e10cSrcweir 				pDestObj->Output( aNewOut.aStart );
1382cdf0e10cSrcweir 
1383cdf0e10cSrcweir 				rDocShell.PostPaintGridAll();			//! only necessary parts
1384cdf0e10cSrcweir 				bDone = sal_True;
1385cdf0e10cSrcweir 			}
1386cdf0e10cSrcweir 		}
1387cdf0e10cSrcweir 		// else nothing (no old, no new)
1388cdf0e10cSrcweir 	}
1389cdf0e10cSrcweir 
1390cdf0e10cSrcweir 	if ( bRecord && bDone )
1391cdf0e10cSrcweir 	{
1392cdf0e10cSrcweir 		SfxUndoAction* pAction = new ScUndoDataPilot( &rDocShell,
1393cdf0e10cSrcweir 									pOldUndoDoc, pNewUndoDoc, pUndoDPObj, pDestObj, bAllowMove );
1394cdf0e10cSrcweir 		pOldUndoDoc = NULL;
1395cdf0e10cSrcweir 		pNewUndoDoc = NULL;		// pointers are used in undo action
1396cdf0e10cSrcweir 		// pUndoDPObj is copied
1397cdf0e10cSrcweir 
1398cdf0e10cSrcweir 		if (bUndoSelf)
1399cdf0e10cSrcweir 		{
1400cdf0e10cSrcweir 			//	use undo action to restore original state
1401cdf0e10cSrcweir 			//!	prevent setting the document modified? (ScDocShellModificator)
1402cdf0e10cSrcweir 
1403cdf0e10cSrcweir 			pAction->Undo();
1404cdf0e10cSrcweir 			delete pAction;
1405cdf0e10cSrcweir 			bDone = sal_False;
1406cdf0e10cSrcweir 		}
1407cdf0e10cSrcweir 		else
1408cdf0e10cSrcweir 			rDocShell.GetUndoManager()->AddUndoAction( pAction );
1409cdf0e10cSrcweir 	}
1410cdf0e10cSrcweir 
1411cdf0e10cSrcweir 	delete pOldUndoDoc;		// if not used for undo
1412cdf0e10cSrcweir 	delete pNewUndoDoc;
1413cdf0e10cSrcweir 	delete pUndoDPObj;
1414cdf0e10cSrcweir 
1415cdf0e10cSrcweir 	if (bDone)
1416cdf0e10cSrcweir     {
1417cdf0e10cSrcweir         // notify API objects
1418cdf0e10cSrcweir         if (pDestObj)
1419cdf0e10cSrcweir             pDoc->BroadcastUno( ScDataPilotModifiedHint( pDestObj->GetName() ) );
1420cdf0e10cSrcweir 		aModificator.SetDocumentModified();
1421cdf0e10cSrcweir     }
1422cdf0e10cSrcweir 
1423cdf0e10cSrcweir 	if ( nErrId && !bApi )
1424cdf0e10cSrcweir 		rDocShell.ErrorMessage( nErrId );
1425cdf0e10cSrcweir 
1426cdf0e10cSrcweir 	return bDone;
1427cdf0e10cSrcweir }
1428cdf0e10cSrcweir 
1429cdf0e10cSrcweir //==================================================================
1430cdf0e10cSrcweir //
1431cdf0e10cSrcweir //      database import
1432cdf0e10cSrcweir 
UpdateImport(const String & rTarget,const svx::ODataAccessDescriptor & rDescriptor)1433cdf0e10cSrcweir void ScDBDocFunc::UpdateImport( const String& rTarget, const svx::ODataAccessDescriptor& rDescriptor )
1434cdf0e10cSrcweir {
1435cdf0e10cSrcweir     // rTarget is the name of a database range
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir 	ScDocument* pDoc = rDocShell.GetDocument();
1438cdf0e10cSrcweir 	ScDBCollection& rDBColl = *pDoc->GetDBCollection();
1439cdf0e10cSrcweir     ScDBData* pData = NULL;
1440cdf0e10cSrcweir 	ScImportParam aImportParam;
1441cdf0e10cSrcweir 	sal_Bool bFound = sal_False;
1442cdf0e10cSrcweir 	sal_uInt16 nCount = rDBColl.GetCount();
1443cdf0e10cSrcweir 	for (sal_uInt16 i=0; i<nCount && !bFound; i++)
1444cdf0e10cSrcweir 	{
1445cdf0e10cSrcweir 		pData = rDBColl[i];
1446cdf0e10cSrcweir 		if (pData->GetName() == rTarget)
1447cdf0e10cSrcweir 			bFound = sal_True;
1448cdf0e10cSrcweir 	}
1449cdf0e10cSrcweir 	if (!bFound)
1450cdf0e10cSrcweir 	{
1451cdf0e10cSrcweir 		InfoBox aInfoBox(rDocShell.GetActiveDialogParent(),
1452cdf0e10cSrcweir 					ScGlobal::GetRscString( STR_TARGETNOTFOUND ) );
1453cdf0e10cSrcweir 		aInfoBox.Execute();
1454cdf0e10cSrcweir 		return;
1455cdf0e10cSrcweir 	}
1456cdf0e10cSrcweir 
1457cdf0e10cSrcweir 	SCTAB nTab;
1458cdf0e10cSrcweir 	SCCOL nDummyCol;
1459cdf0e10cSrcweir     SCROW nDummyRow;
1460cdf0e10cSrcweir 	pData->GetArea( nTab, nDummyCol,nDummyRow,nDummyCol,nDummyRow );
1461cdf0e10cSrcweir 	pData->GetImportParam( aImportParam );
1462cdf0e10cSrcweir 
1463cdf0e10cSrcweir     rtl::OUString sDBName;
1464cdf0e10cSrcweir     rtl::OUString sDBTable;
1465cdf0e10cSrcweir     sal_Int32 nCommandType = 0;
1466cdf0e10cSrcweir     rDescriptor[svx::daDataSource]  >>= sDBName;
1467cdf0e10cSrcweir     rDescriptor[svx::daCommand]     >>= sDBTable;
1468cdf0e10cSrcweir     rDescriptor[svx::daCommandType] >>= nCommandType;
1469cdf0e10cSrcweir 
1470cdf0e10cSrcweir     aImportParam.aDBName    = sDBName;
1471cdf0e10cSrcweir     aImportParam.bSql       = ( nCommandType == sdb::CommandType::COMMAND );
1472cdf0e10cSrcweir     aImportParam.aStatement = sDBTable;
1473cdf0e10cSrcweir     aImportParam.bNative    = sal_False;
1474cdf0e10cSrcweir     aImportParam.nType      = static_cast<sal_uInt8>( ( nCommandType == sdb::CommandType::QUERY ) ? ScDbQuery : ScDbTable );
1475cdf0e10cSrcweir     aImportParam.bImport	= sal_True;
1476cdf0e10cSrcweir 
1477cdf0e10cSrcweir     sal_Bool bContinue = DoImport( nTab, aImportParam, &rDescriptor, sal_True );
1478cdf0e10cSrcweir 
1479cdf0e10cSrcweir 	//	DB-Operationen wiederholen
1480cdf0e10cSrcweir 
1481cdf0e10cSrcweir 	ScTabViewShell* pViewSh = rDocShell.GetBestViewShell();
1482cdf0e10cSrcweir 	if (pViewSh)
1483cdf0e10cSrcweir 	{
1484cdf0e10cSrcweir 		ScRange aRange;
1485cdf0e10cSrcweir 		pData->GetArea(aRange);
1486cdf0e10cSrcweir 		pViewSh->MarkRange(aRange);			// selektieren
1487cdf0e10cSrcweir 
1488cdf0e10cSrcweir 		if ( bContinue )		// #41905# Fehler beim Import -> Abbruch
1489cdf0e10cSrcweir 		{
1490cdf0e10cSrcweir 			//	interne Operationen, wenn welche gespeichert
1491cdf0e10cSrcweir 
1492cdf0e10cSrcweir 			if ( pData->HasQueryParam() || pData->HasSortParam() || pData->HasSubTotalParam() )
1493cdf0e10cSrcweir 				pViewSh->RepeatDB();
1494cdf0e10cSrcweir 
1495cdf0e10cSrcweir 			//	Pivottabellen die den Bereich als Quelldaten haben
1496cdf0e10cSrcweir 
1497cdf0e10cSrcweir 			rDocShell.RefreshPivotTables(aRange);
1498cdf0e10cSrcweir 		}
1499cdf0e10cSrcweir 	}
1500cdf0e10cSrcweir }
1501cdf0e10cSrcweir 
1502cdf0e10cSrcweir 
1503cdf0e10cSrcweir 
1504cdf0e10cSrcweir 
1505