xref: /aoo41x/main/sc/source/ui/view/pfuncache.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew 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 <tools/multisel.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "pfuncache.hxx"
34cdf0e10cSrcweir #include "printfun.hxx"
35cdf0e10cSrcweir #include "docsh.hxx"
36cdf0e10cSrcweir #include "markdata.hxx"
37cdf0e10cSrcweir #include "prevloc.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //------------------------------------------------------------------------
40cdf0e10cSrcweir 
ScPrintFuncCache(ScDocShell * pD,const ScMarkData & rMark,const ScPrintSelectionStatus & rStatus)41cdf0e10cSrcweir ScPrintFuncCache::ScPrintFuncCache( ScDocShell* pD, const ScMarkData& rMark,
42cdf0e10cSrcweir 									const ScPrintSelectionStatus& rStatus ) :
43cdf0e10cSrcweir 	aSelection( rStatus ),
44cdf0e10cSrcweir 	pDocSh( pD ),
45cdf0e10cSrcweir 	nTotalPages( 0 ),
46cdf0e10cSrcweir 	bLocInitialized( false )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	//	page count uses the stored cell widths for the printer anyway,
49cdf0e10cSrcweir 	//	so ScPrintFunc with the document's printer can be used to count
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	SfxPrinter* pPrinter = pDocSh->GetPrinter();
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	ScRange aRange;
54cdf0e10cSrcweir 	const ScRange* pSelRange = NULL;
55cdf0e10cSrcweir 	if ( rMark.IsMarked() )
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		rMark.GetMarkArea( aRange );
58cdf0e10cSrcweir 		pSelRange = &aRange;
59cdf0e10cSrcweir 	}
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	ScDocument* pDoc = pDocSh->GetDocument();
62cdf0e10cSrcweir 	SCTAB nTabCount = pDoc->GetTableCount();
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     // avoid repeated progress bars if row heights for all sheets are needed
65cdf0e10cSrcweir     if ( nTabCount > 1 && rMark.GetSelectCount() == nTabCount )
66cdf0e10cSrcweir         pDocSh->UpdatePendingRowHeights( nTabCount-1, true );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	SCTAB nTab;
69cdf0e10cSrcweir 	for ( nTab=0; nTab<nTabCount; nTab++ )
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		long nAttrPage = nTab > 0 ? nFirstAttr[nTab-1] : 1;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 		long nThisTab = 0;
74cdf0e10cSrcweir 		if ( rMark.GetTableSelect( nTab ) )
75cdf0e10cSrcweir 		{
76cdf0e10cSrcweir 			ScPrintFunc aFunc( pDocSh, pPrinter, nTab, nAttrPage, 0, pSelRange, &aSelection.GetOptions() );
77cdf0e10cSrcweir 			nThisTab = aFunc.GetTotalPages();
78cdf0e10cSrcweir 			nFirstAttr[nTab] = aFunc.GetFirstPageNo();			// from page style or previous sheet
79cdf0e10cSrcweir 		}
80cdf0e10cSrcweir 		else
81cdf0e10cSrcweir 			nFirstAttr[nTab] = nAttrPage;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 		nPages[nTab] = nThisTab;
84cdf0e10cSrcweir 		nTotalPages += nThisTab;
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
~ScPrintFuncCache()88cdf0e10cSrcweir ScPrintFuncCache::~ScPrintFuncCache()
89cdf0e10cSrcweir {
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
InitLocations(const ScMarkData & rMark,OutputDevice * pDev)92cdf0e10cSrcweir void ScPrintFuncCache::InitLocations( const ScMarkData& rMark, OutputDevice* pDev )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     if ( bLocInitialized )
95cdf0e10cSrcweir         return;                 // initialize only once
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     ScRange aRange;
98cdf0e10cSrcweir     const ScRange* pSelRange = NULL;
99cdf0e10cSrcweir     if ( rMark.IsMarked() )
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         rMark.GetMarkArea( aRange );
102cdf0e10cSrcweir         pSelRange = &aRange;
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     long nRenderer = 0;     // 0-based physical page number across sheets
106cdf0e10cSrcweir     long nTabStart = 0;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     ScDocument* pDoc = pDocSh->GetDocument();
109cdf0e10cSrcweir     SCTAB nTabCount = pDoc->GetTableCount();
110cdf0e10cSrcweir     for ( SCTAB nTab=0; nTab<nTabCount; nTab++ )
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         if ( rMark.GetTableSelect( nTab ) )
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             ScPrintFunc aFunc( pDev, pDocSh, nTab, nFirstAttr[nTab], nTotalPages, pSelRange, &aSelection.GetOptions() );
115cdf0e10cSrcweir             aFunc.SetRenderFlag( sal_True );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir             long nDisplayStart = GetDisplayStart( nTab );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             for ( long nPage=0; nPage<nPages[nTab]; nPage++ )
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 Range aPageRange( nRenderer+1, nRenderer+1 );
122cdf0e10cSrcweir                 MultiSelection aPage( aPageRange );
123cdf0e10cSrcweir                 aPage.SetTotalRange( Range(0,RANGE_MAX) );
124cdf0e10cSrcweir                 aPage.Select( aPageRange );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir                 ScPreviewLocationData aLocData( pDoc, pDev );
127cdf0e10cSrcweir                 aFunc.DoPrint( aPage, nTabStart, nDisplayStart, sal_False, &aLocData );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir                 ScRange aCellRange;
130cdf0e10cSrcweir                 Rectangle aPixRect;
131cdf0e10cSrcweir                 if ( aLocData.GetMainCellRange( aCellRange, aPixRect ) )
132cdf0e10cSrcweir                     aLocations.push_back( ScPrintPageLocation( nRenderer, aCellRange, aPixRect ) );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir                 ++nRenderer;
135cdf0e10cSrcweir             }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             nTabStart += nPages[nTab];
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     bLocInitialized = true;
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
FindLocation(const ScAddress & rCell,ScPrintPageLocation & rLocation) const144cdf0e10cSrcweir bool ScPrintFuncCache::FindLocation( const ScAddress& rCell, ScPrintPageLocation& rLocation ) const
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     for ( std::vector<ScPrintPageLocation>::const_iterator aIter(aLocations.begin());
147cdf0e10cSrcweir           aIter != aLocations.end(); aIter++ )
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         if ( aIter->aCellRange.In( rCell ) )
150cdf0e10cSrcweir         {
151cdf0e10cSrcweir             rLocation = *aIter;
152cdf0e10cSrcweir             return true;
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir     return false;   // not found
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
IsSameSelection(const ScPrintSelectionStatus & rStatus) const158cdf0e10cSrcweir sal_Bool ScPrintFuncCache::IsSameSelection( const ScPrintSelectionStatus& rStatus ) const
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	return aSelection == rStatus;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
GetTabForPage(long nPage) const163cdf0e10cSrcweir SCTAB ScPrintFuncCache::GetTabForPage( long nPage ) const
164cdf0e10cSrcweir {
165cdf0e10cSrcweir 	ScDocument* pDoc = pDocSh->GetDocument();
166cdf0e10cSrcweir 	SCTAB nTabCount = pDoc->GetTableCount();
167cdf0e10cSrcweir 	SCTAB nTab = 0;
168cdf0e10cSrcweir 	while ( nTab < nTabCount && nPage >= nPages[nTab] )
169cdf0e10cSrcweir 		nPage -= nPages[nTab++];
170cdf0e10cSrcweir 	return nTab;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
GetTabStart(SCTAB nTab) const173cdf0e10cSrcweir long ScPrintFuncCache::GetTabStart( SCTAB nTab ) const
174cdf0e10cSrcweir {
175cdf0e10cSrcweir 	long nRet = 0;
176cdf0e10cSrcweir 	for ( SCTAB i=0; i<nTab; i++ )
177cdf0e10cSrcweir 		nRet += nPages[i];
178cdf0e10cSrcweir 	return nRet;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
GetDisplayStart(SCTAB nTab) const181cdf0e10cSrcweir long ScPrintFuncCache::GetDisplayStart( SCTAB nTab ) const
182cdf0e10cSrcweir {
183cdf0e10cSrcweir 	//!	merge with lcl_GetDisplayStart in preview?
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	long nDisplayStart = 0;
186cdf0e10cSrcweir 	ScDocument* pDoc = pDocSh->GetDocument();
187cdf0e10cSrcweir 	for (SCTAB i=0; i<nTab; i++)
188cdf0e10cSrcweir 	{
189cdf0e10cSrcweir 		if ( pDoc->NeedPageResetAfterTab(i) )
190cdf0e10cSrcweir 			nDisplayStart = 0;
191cdf0e10cSrcweir 		else
192cdf0e10cSrcweir 			nDisplayStart += nPages[i];
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir 	return nDisplayStart;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
198