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 #pragma once
21 
22 #include <memory>
23 #include <map>
24 #include <pagepar.hxx>
25 #include <editutil.hxx>
26 
27 class SfxPrinter;
28 class ScDocShell;
29 class ScDocument;
30 class ScViewData;
31 class SfxItemSet;
32 class ScPageHFItem;
33 class EditTextObject;
34 class MultiSelection;
35 class ScPageBreakData;
36 class ScPreviewLocationData;
37 class ScPrintOptions;
38 class SvxBoxItem;
39 class SvxBrushItem;
40 class SvxShadowItem;
41 class FmFormView;
42 
43 #define RANGENO_NORANGE             USHRT_MAX
44 
45 #define PRINT_HEADER_WIDTH          (1.0 * TWIPS_PER_CM)
46 #define PRINT_HEADER_HEIGHT         (12.8 * TWIPS_PER_POINT)
47 
48                                             // Settings for headers/footers
49 struct ScPrintHFParam
50 {
51     bool                bEnable;
52     bool                bDynamic;
53     bool                bShared;
54     bool                bSharedFirst;
55     tools::Long                nHeight;            // in total (height + distance + frames)
56     tools::Long                nManHeight;         // set size (min when dynamic)
57     sal_uInt16          nDistance;
58     sal_uInt16          nLeft;              // edges
59     sal_uInt16          nRight;
60     const ScPageHFItem* pLeft;
61     const ScPageHFItem* pRight;
62     const ScPageHFItem* pFirst;
63     const SvxBoxItem*   pBorder;
64     const SvxBrushItem* pBack;
65     const SvxShadowItem* pShadow;
66 };
67 
68 class ScPageRowEntry
69 {
70 private:
71     SCROW                    nStartRow;
72     SCROW                    nEndRow;
73     size_t                   nPagesX;
74     std::vector<bool>        aHidden;
75     //!     Cache Number of really visible?
76 
77 public:
ScPageRowEntry()78             ScPageRowEntry()    { nStartRow = nEndRow = 0; nPagesX = 0; }
79 
80             ScPageRowEntry(const ScPageRowEntry& r);
81     ScPageRowEntry& operator=(const ScPageRowEntry& r);
82 
GetStartRow() const83     SCROW   GetStartRow() const     { return nStartRow; }
GetEndRow() const84     SCROW   GetEndRow() const       { return nEndRow; }
GetPagesX() const85     size_t  GetPagesX() const       { return nPagesX; }
SetStartRow(SCROW n)86     void    SetStartRow(SCROW n)    { nStartRow = n; }
SetEndRow(SCROW n)87     void    SetEndRow(SCROW n)      { nEndRow = n; }
88 
89     void    SetPagesX(size_t nNew);
90     void    SetHidden(size_t nX);
91     bool    IsHidden(size_t nX) const;
92 
93     size_t  CountVisible() const;
94 };
95 
96 namespace sc
97 {
98 
99 struct PrintPageRangesInput
100 {
101     bool m_bSkipEmpty;
102     bool m_bPrintArea;
103     SCROW m_nStartRow;
104     SCROW m_nEndRow;
105     SCCOL m_nStartCol;
106     SCCOL m_nEndCol;
107     SCTAB m_nPrintTab;
108     Size  m_aDocSize;
109 
PrintPageRangesInputsc::PrintPageRangesInput110     PrintPageRangesInput()
111         : m_bSkipEmpty(false)
112         , m_bPrintArea(false)
113         , m_nStartRow(0)
114         , m_nEndRow(0)
115         , m_nStartCol(0)
116         , m_nEndCol(0)
117         , m_nPrintTab(0)
118     {}
119 };
120 
121 class PrintPageRanges
122 {
123 public:
124     PrintPageRanges();
125 
126     // use shared_ptr to avoid copying this (potentially large) data back and forth
127     std::shared_ptr<std::vector<SCCOL>> m_xPageEndX;
128     std::shared_ptr<std::vector<SCROW>> m_xPageEndY;
129     std::shared_ptr<std::map<size_t, ScPageRowEntry>> m_xPageRows;
130 
131     size_t m_nPagesX;
132     size_t m_nPagesY;
133     size_t m_nTotalY;
134 
135     PrintPageRangesInput m_aInput;
136 
137     bool checkIfAlreadyCalculatedAndSet(bool bSkipEmpty, bool bPrintArea,
138                                         SCROW nStartRow, SCROW nEndRow,
139                                         SCCOL nStartCol, SCCOL nEndCol,
140                                         SCTAB nPrintTab, Size const & aDocSize);
141 
142     void calculate(ScDocument& rDoc, bool bSkipEmpty, bool bPrintArea,
143                    SCROW nStartRow, SCROW nEndRow, SCCOL nStartCol, SCCOL nEndCol,
144                    SCTAB nPrintTab, Size const & aDocSize);
145 };
146 
147 }
148 
149 struct ScPrintState                         //  Save Variables from ScPrintFunc
150 {
151     SCTAB   nPrintTab;
152     SCCOL   nStartCol;
153     SCROW   nStartRow;
154     SCCOL   nEndCol;
155     SCROW   nEndRow;
156     bool    bPrintAreaValid; // the 4 variables above are set
157     sal_uInt16  nZoom;
158     size_t  nPagesX;
159     size_t  nPagesY;
160     tools::Long    nTabPages;
161     tools::Long    nTotalPages;
162     tools::Long    nPageStart;
163     tools::Long    nDocPages;
164 
165     // Additional state of page ranges
166     bool bSavedStateRanges;
167     sc::PrintPageRangesInput aPrintPageRangesInput;
168     size_t nTotalY;
169     // use shared_ptr to avoid copying this (potentially large) map back and forth
170     std::shared_ptr<std::vector<SCCOL>> xPageEndX;
171     std::shared_ptr<std::vector<SCROW>> xPageEndY;
172     std::shared_ptr<std::map<size_t, ScPageRowEntry>> xPageRows;
173 
ScPrintStateScPrintState174     ScPrintState()
175         : nPrintTab(0)
176         , nStartCol(0)
177         , nStartRow(0)
178         , nEndCol(0)
179         , nEndRow(0)
180         , bPrintAreaValid(false)
181         , nZoom(0)
182         , nPagesX(0)
183         , nPagesY(0)
184         , nTabPages(0)
185         , nTotalPages(0)
186         , nPageStart(0)
187         , nDocPages(0)
188         , bSavedStateRanges(false)
189         , nTotalY(0)
190     {}
191 };
192 
193 class ScPrintFunc
194 {
195 private:
196     ScDocShell*         pDocShell;
197     ScDocument&         rDoc;
198     VclPtr<SfxPrinter>   pPrinter;
199     VclPtr<OutputDevice> pDev;
200     FmFormView*         pDrawView;
201 
202     MapMode             aOldPrinterMode;    //  MapMode before the call
203 
204     Point               aSrcOffset;         //  Paper-1/100 mm
205     Point               aOffset;            //  scaled by a factor of page size
206     sal_uInt16          nManualZoom;        //  Zoom in Preview (percent)
207     bool                bClearWin;          //  Clear output before
208     bool                bUseStyleColor;
209     bool                bIsRender;
210 
211     SCTAB               nPrintTab;
212     tools::Long                nPageStart;         //  Offset for the first page
213     tools::Long                nDocPages;          //  Number of Pages in Document
214 
215     const ScRange*      pUserArea;          //  Selection, if set in dialog
216 
217     const SfxItemSet*   pParamSet;          //  Selected template
218     bool                bFromPrintState;    // created from State-struct
219 
220                                             //  Parameter from template:
221     sal_uInt16          nLeftMargin;
222     sal_uInt16          nTopMargin;
223     sal_uInt16          nRightMargin;
224     sal_uInt16          nBottomMargin;
225     bool                bCenterHor;
226     bool                bCenterVer;
227     bool                bLandscape;
228     bool                bSourceRangeValid;
229 
230     SvxPageUsage        nPageUsage;
231     Size                aPageSize;          // Printer Twips
232     const SvxBoxItem*   pBorderItem;
233     const SvxBrushItem* pBackgroundItem;
234     const SvxShadowItem* pShadowItem;
235 
236     ScRange             aLastSourceRange;
237     ScPrintHFParam      aHdr;
238     ScPrintHFParam      aFtr;
239     ScPageTableParam    aTableParam;
240     ScPageAreaParam     aAreaParam;
241 
242                                             // Calculated values:
243     sal_uInt16          nZoom;
244     bool                bPrintCurrentTable;
245     bool                bMultiArea;
246     bool                mbHasPrintRange;
247     tools::Long                nTabPages;
248     tools::Long                nTotalPages;
249 
250     tools::Rectangle           aPageRect;          // Document Twips
251 
252     MapMode             aLogicMode;         // Set in DoPrint
253     MapMode             aOffsetMode;
254     MapMode             aTwipMode;
255     double              nScaleX;
256     double              nScaleY;
257 
258     SCCOL               nRepeatStartCol;
259     SCCOL               nRepeatEndCol;
260     SCROW               nRepeatStartRow;
261     SCROW               nRepeatEndRow;
262 
263     SCCOL               nStartCol;
264     SCROW               nStartRow;
265     SCCOL               nEndCol;
266     SCROW               nEndRow;
267     bool                bPrintAreaValid; // the 4 variables above are set
268 
269     sc::PrintPageRanges m_aRanges;
270 
271     std::unique_ptr<ScHeaderEditEngine> pEditEngine;
272     std::unique_ptr<SfxItemSet>         pEditDefaults;
273 
274     ScHeaderFieldData   aFieldData;
275 
276     std::vector<ScAddress> aNotePosList;        // The order of notes
277 
278     ScPageBreakData*    pPageData;          // for recording the breaks etc.
279 
280 public:
281                     ScPrintFunc( ScDocShell* pShell, SfxPrinter* pNewPrinter, SCTAB nTab,
282                                  tools::Long nPage = 0, tools::Long nDocP = 0,
283                                  const ScRange* pArea = nullptr,
284                                  const ScPrintOptions* pOptions = nullptr,
285                                  ScPageBreakData* pData = nullptr );
286 
287                     ScPrintFunc( ScDocShell* pShell, SfxPrinter* pNewPrinter,
288                                 const ScPrintState& rState, const ScPrintOptions* pOptions );
289 
290                     // ctors for device other than printer - for preview and pdf:
291 
292                     ScPrintFunc( OutputDevice* pOutDev, ScDocShell* pShell, SCTAB nTab,
293                                  tools::Long nPage = 0, tools::Long nDocP = 0,
294                                  const ScRange* pArea = nullptr,
295                                  const ScPrintOptions* pOptions = nullptr );
296 
297                     ScPrintFunc( OutputDevice* pOutDev, ScDocShell* pShell,
298                                  const ScPrintState& rState,
299                                  const ScPrintOptions* pOptions );
300 
301                     ~ScPrintFunc();
302 
303     static void     DrawToDev( ScDocument& rDoc, OutputDevice* pDev, double nPrintFactor,
304                                const tools::Rectangle& rBound, ScViewData* pViewData, bool bMetaFile );
305 
306     void            SetDrawView( FmFormView* pNew );
307 
308     void            SetOffset( const Point& rOfs );
309     void            SetManualZoom( sal_uInt16 nNewZoom );
310     void            SetDateTime( const DateTime& );
311 
312     void            SetClearFlag( bool bFlag );
313     void            SetUseStyleColor( bool bFlag );
314     void            SetRenderFlag( bool bFlag );
315 
316     void            SetExclusivelyDrawOleAndDrawObjects();//for printing selected objects without surrounding cell contents
317 
318     bool            UpdatePages();
319 
320     void            ApplyPrintSettings();       // Already called from DoPrint()
321     tools::Long            DoPrint( const MultiSelection& rPageRanges,
322                                 tools::Long nStartPage, tools::Long nDisplayStart, bool bDoPrint,
323                                 ScPreviewLocationData* pLocationData );
324 
325                     // Query values - immediately
326 
GetPageSize() const327     const Size&     GetPageSize() const { return aPageSize; }
328     Size            GetDataSize() const;
329     void            GetScaleData( Size& rPhysSize, tools::Long& rDocHdr, tools::Long& rDocFtr );
GetFirstPageNo() const330     tools::Long            GetFirstPageNo() const  { return aTableParam.nFirstPageNo; }
331 
GetTotalPages() const332     tools::Long            GetTotalPages() const { return nTotalPages; }
GetZoom() const333     sal_uInt16      GetZoom() const { return nZoom; }
334 
335     void            ResetBreaks( SCTAB nTab );
336 
337     void            GetPrintState(ScPrintState& rState, bool bSavePageRanges = false);
338     bool            GetLastSourceRange( ScRange& rRange ) const;
GetLeftMargin() const339     sal_uInt16      GetLeftMargin() const{return nLeftMargin;}
GetRightMargin() const340     sal_uInt16      GetRightMargin() const{return nRightMargin;}
GetTopMargin() const341     sal_uInt16      GetTopMargin() const{return nTopMargin;}
GetBottomMargin() const342     sal_uInt16      GetBottomMargin() const{return nBottomMargin;}
GetHeader() const343     const ScPrintHFParam& GetHeader() const {return aHdr;}
GetFooter() const344     const ScPrintHFParam& GetFooter() const {return aFtr;}
345 
HasPrintRange() const346     bool HasPrintRange() const { return mbHasPrintRange;}
347 
348 private:
349     void            Construct( const ScPrintOptions* pOptions );
350     void            InitParam( const ScPrintOptions* pOptions );
351     void            CalcZoom( sal_uInt16 nRangeNo );
352     void            CalcPages();
353     tools::Long            CountPages();
354     tools::Long            CountNotePages();
355 
356     bool            AdjustPrintArea( bool bNew );
357 
358     Size            GetDocPageSize();
359 
360     tools::Long            TextHeight( const EditTextObject* pObject );
361     void            MakeEditEngine();
362     void            UpdateHFHeight( ScPrintHFParam& rParam );
363 
364     void            InitModes();
365 
366     bool            IsLeft( tools::Long nPageNo );
367     bool            IsMirror( tools::Long nPageNo );
368     void            MakeTableString();                  // sets aTableStr
369 
370     void            PrintPage( tools::Long nPageNo,
371                                     SCCOL nX1, SCROW nY1, SCCOL nX2, SCROW nY2,
372                                     bool bDoPrint, ScPreviewLocationData* pLocationData );
373     void            PrintArea( SCCOL nX1, SCROW nY1, SCCOL nX2, SCROW nY2,
374                                     tools::Long nScrX, tools::Long nScrY,
375                                     bool bShLeft, bool bShTop, bool bShRight, bool bShBottom );
376     void            LocateArea( SCCOL nX1, SCROW nY1, SCCOL nX2, SCROW nY2,
377                                     tools::Long nScrX, tools::Long nScrY, bool bRepCol, bool bRepRow,
378                                     ScPreviewLocationData& rLocationData );
379     void            PrintColHdr( SCCOL nX1, SCCOL nX2, tools::Long nScrX, tools::Long nScrY );
380     void            PrintRowHdr( SCROW nY1, SCROW nY2, tools::Long nScrX, tools::Long nScrY );
381     void            LocateColHdr( SCCOL nX1, SCCOL nX2, tools::Long nScrX, tools::Long nScrY,
382                                 bool bRepCol, ScPreviewLocationData& rLocationData );
383     void            LocateRowHdr( SCROW nY1, SCROW nY2, tools::Long nScrX, tools::Long nScrY,
384                                 bool bRepRow, ScPreviewLocationData& rLocationData );
385     void            PrintHF( tools::Long nPageNo, bool bHeader, tools::Long nStartY,
386                                     bool bDoPrint, ScPreviewLocationData* pLocationData );
387 
388     tools::Long            PrintNotes( tools::Long nPageNo, tools::Long nNoteStart, bool bDoPrint, ScPreviewLocationData* pLocationData );
389     tools::Long            DoNotes( tools::Long nNoteStart, bool bDoPrint, ScPreviewLocationData* pLocationData );
390 
391     void            DrawBorder( tools::Long nScrX, tools::Long nScrY, tools::Long nScrW, tools::Long nScrH,
392                                     const SvxBoxItem* pBorderData,
393                                     const SvxBrushItem* pBackground,
394                                     const SvxShadowItem* pShadow );
395 
396     void            FillPageData();
397 };
398 
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
400