Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <tools/multisel.hxx>
21 :
22 : #include "pfuncache.hxx"
23 : #include "printfun.hxx"
24 : #include "docsh.hxx"
25 : #include "markdata.hxx"
26 : #include "prevloc.hxx"
27 :
28 0 : ScPrintFuncCache::ScPrintFuncCache( ScDocShell* pD, const ScMarkData& rMark,
29 : const ScPrintSelectionStatus& rStatus ) :
30 : aSelection( rStatus ),
31 : pDocSh( pD ),
32 : nTotalPages( 0 ),
33 : nPages(),
34 : nFirstAttr(),
35 0 : bLocInitialized( false )
36 : {
37 : // page count uses the stored cell widths for the printer anyway,
38 : // so ScPrintFunc with the document's printer can be used to count
39 :
40 0 : SfxPrinter* pPrinter = pDocSh->GetPrinter();
41 :
42 0 : ScRange aRange;
43 0 : const ScRange* pSelRange = NULL;
44 0 : if ( rMark.IsMarked() )
45 : {
46 0 : rMark.GetMarkArea( aRange );
47 0 : pSelRange = &aRange;
48 : }
49 :
50 0 : ScDocument& rDoc = pDocSh->GetDocument();
51 0 : SCTAB nTabCount = rDoc.GetTableCount();
52 :
53 : // avoid repeated progress bars if row heights for all sheets are needed
54 0 : if ( nTabCount > 1 && rMark.GetSelectCount() == nTabCount )
55 0 : pDocSh->UpdatePendingRowHeights( nTabCount-1, true );
56 :
57 : SCTAB nTab;
58 0 : for ( nTab=0; nTab<nTabCount; nTab++ )
59 : {
60 0 : long nAttrPage = nTab > 0 ? nFirstAttr[nTab-1] : 1;
61 :
62 0 : long nThisTab = 0;
63 0 : if ( rMark.GetTableSelect( nTab ) )
64 : {
65 0 : ScPrintFunc aFunc( pDocSh, pPrinter, nTab, nAttrPage, 0, pSelRange, &aSelection.GetOptions() );
66 0 : nThisTab = aFunc.GetTotalPages();
67 0 : nFirstAttr.push_back( aFunc.GetFirstPageNo() ); // from page style or previous sheet
68 : }
69 : else
70 0 : nFirstAttr.push_back( nAttrPage );
71 :
72 0 : nPages.push_back( nThisTab );
73 0 : nTotalPages += nThisTab;
74 : }
75 0 : }
76 :
77 0 : ScPrintFuncCache::~ScPrintFuncCache()
78 : {
79 0 : }
80 :
81 0 : void ScPrintFuncCache::InitLocations( const ScMarkData& rMark, OutputDevice* pDev )
82 : {
83 0 : if ( bLocInitialized )
84 0 : return; // initialize only once
85 :
86 0 : ScRange aRange;
87 0 : const ScRange* pSelRange = NULL;
88 0 : if ( rMark.IsMarked() )
89 : {
90 0 : rMark.GetMarkArea( aRange );
91 0 : pSelRange = &aRange;
92 : }
93 :
94 0 : long nRenderer = 0; // 0-based physical page number across sheets
95 0 : long nTabStart = 0;
96 :
97 0 : ScDocument& rDoc = pDocSh->GetDocument();
98 0 : SCTAB nTabCount = rDoc.GetTableCount();
99 0 : ScMarkData::const_iterator itr = rMark.begin(), itrEnd = rMark.end();
100 0 : for (; itr != itrEnd && (*itr) < nTabCount; ++itr)
101 : {
102 0 : SCTAB nTab = *itr;
103 0 : ScPrintFunc aFunc( pDev, pDocSh, nTab, nFirstAttr[nTab], nTotalPages, pSelRange, &aSelection.GetOptions() );
104 0 : aFunc.SetRenderFlag( true );
105 :
106 0 : long nDisplayStart = GetDisplayStart( nTab );
107 :
108 0 : for ( long nPage=0; nPage<nPages[nTab]; nPage++ )
109 : {
110 0 : Range aPageRange( nRenderer+1, nRenderer+1 );
111 0 : MultiSelection aPage( aPageRange );
112 0 : aPage.SetTotalRange( Range(0,RANGE_MAX) );
113 0 : aPage.Select( aPageRange );
114 :
115 0 : ScPreviewLocationData aLocData( &rDoc, pDev );
116 0 : aFunc.DoPrint( aPage, nTabStart, nDisplayStart, false, &aLocData );
117 :
118 0 : ScRange aCellRange;
119 0 : Rectangle aPixRect;
120 0 : if ( aLocData.GetMainCellRange( aCellRange, aPixRect ) )
121 0 : aLocations.push_back( ScPrintPageLocation( nRenderer, aCellRange, aPixRect ) );
122 :
123 0 : ++nRenderer;
124 0 : }
125 :
126 0 : nTabStart += nPages[nTab];
127 0 : }
128 :
129 0 : bLocInitialized = true;
130 : }
131 :
132 0 : bool ScPrintFuncCache::FindLocation( const ScAddress& rCell, ScPrintPageLocation& rLocation ) const
133 : {
134 0 : for ( std::vector<ScPrintPageLocation>::const_iterator aIter(aLocations.begin()), aEnd(aLocations.end());
135 : aIter != aEnd; ++aIter )
136 : {
137 0 : if ( aIter->aCellRange.In( rCell ) )
138 : {
139 0 : rLocation = *aIter;
140 0 : return true;
141 : }
142 : }
143 0 : return false; // not found
144 : }
145 :
146 0 : bool ScPrintFuncCache::IsSameSelection( const ScPrintSelectionStatus& rStatus ) const
147 : {
148 0 : return aSelection == rStatus;
149 : }
150 :
151 0 : SCTAB ScPrintFuncCache::GetTabForPage( long nPage ) const
152 : {
153 0 : ScDocument& rDoc = pDocSh->GetDocument();
154 0 : SCTAB nTabCount = rDoc.GetTableCount();
155 0 : SCTAB nTab = 0;
156 0 : while ( nTab < nTabCount && nPage >= nPages[nTab] )
157 0 : nPage -= nPages[nTab++];
158 0 : if (nTab >= nTabCount)
159 0 : nTab = nTabCount - 1;
160 0 : return nTab;
161 : }
162 :
163 0 : long ScPrintFuncCache::GetTabStart( SCTAB nTab ) const
164 : {
165 0 : long nRet = 0;
166 0 : for ( SCTAB i=0; i<nTab&& i < static_cast<SCTAB>(nPages.size()); i++ )
167 0 : nRet += nPages[i];
168 0 : return nRet;
169 : }
170 :
171 0 : long ScPrintFuncCache::GetDisplayStart( SCTAB nTab ) const
172 : {
173 : //! merge with lcl_GetDisplayStart in preview?
174 :
175 0 : long nDisplayStart = 0;
176 0 : ScDocument& rDoc = pDocSh->GetDocument();
177 0 : for (SCTAB i=0; i<nTab; i++)
178 : {
179 0 : if ( rDoc.NeedPageResetAfterTab(i) )
180 0 : nDisplayStart = 0;
181 : else
182 : {
183 0 : if ( i < static_cast<SCTAB>(nPages.size()) )
184 0 : nDisplayStart += nPages[i];
185 : else
186 : OSL_FAIL("nPages out of bounds, FIX IT!");
187 : }
188 : }
189 0 : return nDisplayStart;
190 156 : }
191 :
192 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|