1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/multisel.hxx>
21 #include <osl/diagnose.h>
23 #include <pfuncache.hxx>
24 #include <printfun.hxx>
26 #include <markdata.hxx>
27 #include <prevloc.hxx>
30 ScPrintFuncCache::ScPrintFuncCache(ScDocShell
* pD
, const ScMarkData
& rMark
,
31 ScPrintSelectionStatus aStatus
, Size aPrintPageSize
, bool bPrintPageLandscape
,
32 bool bUsePrintDialogSetting
)
34 aSelection(std::move( aStatus
)),
37 bLocInitialized( false )
39 // page count uses the stored cell widths for the printer anyway,
40 // so ScPrintFunc with the document's printer can be used to count
42 SfxPrinter
* pPrinter
= pDocSh
->GetPrinter();
45 const ScRange
* pSelRange
= nullptr;
46 if ( rMark
.IsMarked() )
48 aRange
= rMark
.GetMarkArea();
52 ScDocument
& rDoc
= pDocSh
->GetDocument();
53 SCTAB nTabCount
= rDoc
.GetTableCount();
55 // avoid repeated progress bars if row heights for all sheets are needed
56 if ( nTabCount
> 1 && rMark
.GetSelectCount() == nTabCount
)
57 pDocSh
->UpdatePendingRowHeights( nTabCount
-1, true );
60 for ( nTab
=0; nTab
<nTabCount
; nTab
++ )
62 tools::Long nAttrPage
= nTab
> 0 ? nFirstAttr
[nTab
-1] : 1;
64 tools::Long nThisTab
= 0;
65 if ( rMark
.GetTableSelect( nTab
) )
67 ScPrintFunc
aFunc(pDocSh
, pPrinter
, nTab
, nAttrPage
, 0, pSelRange
,
68 &aSelection
.GetOptions(), nullptr, aPrintPageSize
,
69 bPrintPageLandscape
, bUsePrintDialogSetting
);
70 nThisTab
= aFunc
.GetTotalPages();
71 nFirstAttr
.push_back( aFunc
.GetFirstPageNo() ); // from page style or previous sheet
74 nFirstAttr
.push_back( nAttrPage
);
76 nPages
.push_back( nThisTab
);
77 nTotalPages
+= nThisTab
;
81 ScPrintFuncCache::~ScPrintFuncCache()
85 void ScPrintFuncCache::InitLocations( const ScMarkData
& rMark
, OutputDevice
* pDev
)
87 if ( bLocInitialized
)
88 return; // initialize only once
91 const ScRange
* pSelRange
= nullptr;
92 if ( rMark
.IsMarked() )
94 aRange
= rMark
.GetMarkArea();
98 tools::Long nRenderer
= 0; // 0-based physical page number across sheets
99 tools::Long nTabStart
= 0;
101 ScDocument
& rDoc
= pDocSh
->GetDocument();
102 SCTAB nTabCount
= rDoc
.GetTableCount();
103 for (SCTAB nTab
: rMark
)
105 if (nTab
>= nTabCount
)
107 ScPrintFunc
aFunc( pDev
, pDocSh
, nTab
, nFirstAttr
[nTab
], nTotalPages
, pSelRange
, &aSelection
.GetOptions() );
108 aFunc
.SetRenderFlag( true );
110 tools::Long nDisplayStart
= GetDisplayStart( nTab
);
112 for ( tools::Long nPage
=0; nPage
<nPages
[nTab
]; nPage
++ )
114 Range
aPageRange( nRenderer
+1, nRenderer
+1 );
115 MultiSelection
aPage( aPageRange
);
116 aPage
.SetTotalRange( Range(0,RANGE_MAX
) );
117 aPage
.Select( aPageRange
);
119 ScPreviewLocationData
aLocData( &rDoc
, pDev
);
120 aFunc
.DoPrint( aPage
, nTabStart
, nDisplayStart
, false, &aLocData
);
123 tools::Rectangle aPixRect
;
124 if ( aLocData
.GetMainCellRange( aCellRange
, aPixRect
) )
125 aLocations
.emplace_back( nRenderer
, aCellRange
, aPixRect
);
130 nTabStart
+= nPages
[nTab
];
133 bLocInitialized
= true;
136 bool ScPrintFuncCache::FindLocation( const ScAddress
& rCell
, ScPrintPageLocation
& rLocation
) const
138 auto aIter
= std::find_if(aLocations
.begin(), aLocations
.end(),
139 [&rCell
](const ScPrintPageLocation
& rLoc
) { return rLoc
.aCellRange
.Contains(rCell
); });
140 if (aIter
!= aLocations
.end())
145 return false; // not found
148 bool ScPrintFuncCache::IsSameSelection( const ScPrintSelectionStatus
& rStatus
) const
150 return aSelection
== rStatus
;
153 SCTAB
ScPrintFuncCache::GetTabForPage( tools::Long nPage
) const
155 ScDocument
& rDoc
= pDocSh
->GetDocument();
156 SCTAB nTabCount
= rDoc
.GetTableCount();
158 while ( nTab
< nTabCount
&& nPage
>= nPages
[nTab
] )
159 nPage
-= nPages
[nTab
++];
160 if (nTab
>= nTabCount
)
161 nTab
= nTabCount
- 1;
165 tools::Long
ScPrintFuncCache::GetTabStart( SCTAB nTab
) const
167 tools::Long nRet
= 0;
168 const SCTAB maxIndex
= std::min(nTab
, static_cast<SCTAB
>(nPages
.size()));
169 for ( SCTAB i
=0; i
<maxIndex
; i
++ )
174 tools::Long
ScPrintFuncCache::GetDisplayStart( SCTAB nTab
) const
176 //! merge with lcl_GetDisplayStart in preview?
178 tools::Long nDisplayStart
= 0;
179 ScDocument
& rDoc
= pDocSh
->GetDocument();
180 for (SCTAB i
=0; i
<nTab
; i
++)
182 if ( rDoc
.NeedPageResetAfterTab(i
) )
186 if ( i
< static_cast<SCTAB
>(nPages
.size()) )
187 nDisplayStart
+= nPages
[i
];
189 OSL_FAIL("nPages out of bounds, FIX IT!");
192 return nDisplayStart
;
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */