summaryrefslogtreecommitdiffstats
path: root/sw/inc/pagepreviewlayout.hxx
blob: d2af5f846ea180252b39df8c3ec67658402ef0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_SW_INC_PAGEPREVIEWLAYOUT_HXX
#define INCLUDED_SW_INC_PAGEPREVIEWLAYOUT_HXX

// template class <std::vector>
#include <vector>
// datatypes sal_xyz
#include <sal/types.h>
// classes <Point>, <Size> and <Rectangle>
#include <tools/gen.hxx>
// RenderContext type
#include <vcl/outdev.hxx>
// datatype <SwTwips>
#include "swtypes.hxx"
#include <prevwpage.hxx>

class SwViewShell;
class SwRootFrame;
class SwPageFrame;
class Fraction;
struct PreviewPage;

/** page preview functionality in the writer */
class SwPagePreviewLayout
{
private:
    friend class SwViewShell;

    /// number of horizontal and vertical twips for spacing between the pages.
    static constexpr SwTwips gnXFree = 4 * 142;
    static constexpr SwTwips gnYFree = 4 * 142;

    /// view shell the print preview is generated for.
    SwViewShell& mrParentViewShell;
    /// top layout frame of the layout for accessing the pages
    const SwRootFrame& mrLayoutRootFrame;

    /** boolean indicating, if the layout information (number of columns and rows)
       are valid. */
    bool        mbLayoutInfoValid;
    /** boolean indicating, if the calculated print preview layout sizes
       ( windows size in twips, maximal page size, column width, row height,
         width and height of a print preview page, size of the print preview
         document ) are valid */
    bool        mbLayoutSizesValid;
    /** boolean indicating, if the paint information ( physical number of
         start page, start column and row, paint offsets, rectangle visible of
         the print preview document. */
    bool        mbPaintInfoValid;

    Size        maWinSize;
    sal_uInt16  mnCols;
    sal_uInt16  mnRows;
    sal_uInt16  mnPages;
    // #i18143# - the book preview is controlled by this flag
    bool        mbBookPreview;
    bool        mbBookPreviewModeToggled;

    Size        maMaxPageSize;
    tools::Rectangle   maPreviewDocRect;
    SwTwips     mnColWidth;
    SwTwips     mnRowHeight;
    SwTwips     mnPreviewLayoutWidth;
    SwTwips     mnPreviewLayoutHeight;
    bool        mbDoesLayoutColsFitIntoWindow;
    bool        mbDoesLayoutRowsFitIntoWindow;

    sal_uInt16  mnPaintPhyStartPageNum;
    sal_uInt16  mnPaintStartCol;
    sal_uInt16  mnPaintStartRow;
    bool        mbNoPageVisible;
    Point       maPaintStartPageOffset;
    Point       maPaintPreviewDocOffset;
    Point       maAdditionalPaintOffset;
    tools::Rectangle   maPaintedPreviewDocRect;
    sal_uInt16  mnSelectedPageNum;

    std::vector<std::unique_ptr<PreviewPage>> maPreviewPages;

    /** #i22014# - internal booleans to indicate, that a new print
       preview layout has been created during a paint. */
    mutable bool mbInPaint;
    mutable bool mbNewLayoutDuringPaint;

    bool mbPrintEmptyPages;

    /** clear internal data about current page preview */
    void Clear_();

    /** helper method to clear preview page layout sizes */
    void ClearPreviewLayoutSizes();

    /** helper method to clear data in preview page vectors */
    void ClearPreviewPageData();

    /** calculate page preview layout sizes */
    void CalcPreviewLayoutSizes();

    /** apply new zoom at given view shell

        @param _aNewZoom
        input parameter - new zoom percentage
    */
    void ApplyNewZoomAtViewShell( sal_uInt8 _aNewZoom );

    /** calculate additional paint offset

        helper method called by <Prepare> in order to calculate an additional
        paint offset to center output in given window size.
        The booleans <mbDoesLayoutRowsFitIntoWindow> and <mbDoesLayoutColsFitIntoWindow>
        are also determined.
        preconditions:
        (1) preview layout is given (number of rows and columns).
        (2) window size is given.
        (3) height of row and width of column are calculated.
        (4) paint offset of start page is calculated.
    */
    void CalcAdditionalPaintOffset();

    /** calculate painted preview document rectangle

        helper method called by <Prepare> in order to calculate the rectangle,
        which will be painted for the document arranged by the given preview
        layout.
        preconditions:
        (1) paint offset of document preview is calculated.
        (2) size of document preview is calculated.
        (3) additional paint offset is calculated - see <CalcAdditionalPaintOffset>.
    */
    void CalcDocPreviewPaintRect();

    /** determines preview data for a given page and a given preview offset

        @param _rPage
        input parameter - constant reference to page frame, for which the
        preview data will be calculated.

        @param _rPreviewOffset
        input parameter - constant reference to the offset the given page has
        in the current preview window.
        Note: Offset can be negative.

        @param _opPreviewPage
        output parameter - calculated preview data.
    */
    void CalcPreviewDataForPage( const SwPageFrame& _rPage,
                                  const Point& _rPreviewOffset,
                                  PreviewPage* _opPreviewPage );

    /** calculate preview pages

        helper method called by <Prepare> in order to determine which pages
        will be visible in the current preview and calculate the data needed
        to paint these pages. Also the accessible pages with its needed data
        are determined.
    */
    void CalcPreviewPages();

    /** get preview page by physical page number

        @param _nPageNum
        input parameter - physical page number of page, for which the preview
        page will be returned.

        @return pointer to preview page of current preview pages. If page doesn't
        belongs to current preview pages, <0> is returned.
    */
    const PreviewPage* GetPreviewPageByPageNum( const sal_uInt16 _nPageNum ) const;

    /** paint selection mark at page */
    void PaintSelectMarkAtPage(vcl::RenderContext& rRenderContext, const PreviewPage* _aSelectedPreviewPage) const;

public:
    /** constructor of <SwPagePreviewLayout>

        @param _rParentViewShell
        input parameter - reference to the view shell the page preview
        layout belongs to. Reference will be hold as member <mrParentViewShell>.
        Adjustments/Changes at this view shell:
        (1) Adjustment of the mapping mode at the output device.
        (2) Change of the zoom at the view options.
        (3) Preparations for paint of the page preview.

        @param _rLayoutRootFrame
        input parameter - constant reference to the root frame of the layout.
        Reference will be hold as member <mrLayoutRootFrame> in order to get
        access to the page frames.
    */
    SwPagePreviewLayout( SwViewShell& _rParentViewShell,
                         const SwRootFrame&    _rLayoutRootFrame );

    /** destructor of <SwPagePreviewLayout> */
    ~SwPagePreviewLayout()
    {
        ClearPreviewPageData();
    }

    /** init page preview layout

        initialize the page preview settings for a given layout.
        side effects:
        (1) If parameter <_bCalcScale> is true, mapping mode with calculated
        scaling is set at the output device and the zoom at the view options of
        the given view shell is set with the calculated scaling.

        @param _nCols
        input parameter - initial number of page columns in the preview.

        @param _nRows
        input parameter - initial number of page rows in the preview.

        @param _rPxWinSize
        input parameter - window size in which the preview will be displayed and
        for which the scaling will be calculated.
    */
    void Init( const sal_uInt16 _nCols,
               const sal_uInt16 _nRows,
               const Size&      _rPxWinSize
              );

    /** method to adjust page preview layout to document changes */
    void ReInit();

    /** prepare paint of page preview

        With the valid preview layout settings - calculated and set by method
        <Init(..)> - the paint of a specific part of the virtual preview
        document is prepared. The corresponding part is given by either
        a start page (parameter <_nProposedStartPageNum>) or an absolute position
        (parameter <_aProposedStartPoint>).
        The accessibility preview will also be updated via a corresponding
        method call.

        @param _nProposedStartPageNum [0..<number of document pages>]
        input parameter - proposed number of page, which should be painted in
        the left-top-corner in the current output device. input parameter
        <_bStartWithPageAtFirstCol> influences, if proposed page is actual
        painted in the left-top-corner.

        @param _nProposedStartPos [(0,0)..<PreviewDocumentSize>]
        input parameter - proposed absolute position in the virtual preview
        document, which should be painted in the left-top-corner in the current
        output device.

        @param _rPxWinSize
        input parameter - pixel size of window the preview will be painted in.

        @param _onStartPageNum
        output parameter - physical number of page, which will be painted in the
        left-top-corner in the current output device.

        @param _orDocPreviewPaintRect
        output parameter - rectangle of preview document, which will be painted.

        @param _bStartWithPageAtFirstCol
        input parameter with default value "true" - controls, if start page
        is set to page in first column the proposed start page is located.

        @return boolean, indicating, if prepare of preview paint was successful.
    */
    bool Prepare( const sal_uInt16 _nProposedStartPageNum,
                  const Point&      rProposedStartPos,
                  const Size&      _rPxWinSize,
                  sal_uInt16&      _onStartPageNum,
                  tools::Rectangle&       _orDocPreviewPaintRect,
                  const bool       _bStartWithPageAtFirstCol = true
                );

    /** get selected page number */
    sal_uInt16 SelectedPage()
    {
        return mnSelectedPageNum;
    }

    /** set selected page number */
    void SetSelectedPage( sal_uInt16 _nSelectedPageNum )
    {
        mnSelectedPageNum = _nSelectedPageNum;
    }

    /** paint prepared preview

        @param _aOutRect
        input parameter - Twip rectangle of window, which should be painted.

        @return boolean, indicating, if paint of preview was performed
    */
    bool Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rOutRect) const;

    /** repaint pages on page preview

        method to invalidate visible pages due to changes in a different
        view shell.
    */
    void Repaint(const tools::Rectangle& rInvalidCoreRect) const;

    /** paint to mark new selected page

        Perform paint for current selected page in order to unmark it.
        Set new selected page and perform paint to mark this page.

        @param _nNewSelectedPage
        input parameter - physical number of page, which will be marked as selected.
    */
    void MarkNewSelectedPage( const sal_uInt16 _nSelectedPage );

    /** calculate start position for new scale

        calculate new start position for a new scale. Calculation bases on the
        current visible part of the document arranged in the given preview layout.
        preconditions:
        (1) new scaling is already set at the given output device.

        @return Point, start position for new scale
    */
    Point GetPreviewStartPosForNewScale( const Fraction& _aNewScale,
                                         const Fraction& _aOldScale,
                                         const Size&     _aNewWinSize ) const;

    /** determines, if page with given page number is visible in preview

        @param _nPageNum
        input parameter - physical number of page, for which it will be
        determined, if it is visible.

        @return boolean, indicating, if page with given page number is visible
        in preview.
    */
    bool IsPageVisible( const sal_uInt16 _nPageNum ) const;

    /** calculate data to bring new selected page into view.

        @param _nHoriMove
        input parameter - positive/negative number of columns the current
        selected page have to be moved.

        @param _nVertMove
        input parameter - positive/negative number of rows the current
        selected page have to be moved.

        @param _orNewSelectedPage
        output parameter - number of new selected page

        @param _orNewStartPage
        output parameter - number of new start page

        @param _orNewStartPos
        output parameter - new start position in document preview
    */
    void CalcStartValuesForSelectedPageMove( const sal_Int16  _nHoriMove,
                                             const sal_Int16  _nVertMove,
                                             sal_uInt16&      _orNewSelectedPage,
                                             sal_uInt16&      _orNewStartPage,
                                             Point&           _orNewStartPos ) const;

    /** checks, if given position is inside a shown document page

        @param _aPreviewPos
        input parameter - position inside the visible preview window.

        @param _orDocPos
        output parameter - corresponding position in the document, if given
        preview position is inside a shown document page, not an empty page.
        If not, its value is <Point( 0, 0 )>.

        @param _obPosInEmptyPage
        output parameter - indicates, that given preview position lays inside
        an shown empty page.

        @param _onPageNum
        output parameter - corresponding physical number of page, if given
        preview position is inside a shown document page, considers also empty
        pages. If not, its value is <0>.

        @return boolean - indicating, that given preview position lays inside
        a shown document preview page, not an empty page.
    */
    bool IsPreviewPosInDocPreviewPage( const Point& rPreviewPos,
                                   Point&       _orDocPos,
                                   bool&        _obPosInEmptyPage,
                                   sal_uInt16&  _onPageNum ) const;

    bool DoesPreviewLayoutRowsFitIntoWindow() const
    {
        return mbDoesLayoutRowsFitIntoWindow;
    }

    bool DoesPreviewLayoutColsFitIntoWindow() const
    {
        return mbDoesLayoutColsFitIntoWindow;
    }

    bool PreviewLayoutValid() const
    {
        return mbLayoutInfoValid && mbLayoutSizesValid && mbPaintInfoValid;
    }

    /** determine preview window page scroll amount

        @param _nWinPagesToScroll
        input parameter - number of preview window pages the scroll amount has
        to be calculated for. Negative values for preview window page up
        scrolling, positive values for preview window page down scrolling.

        @return scroll amount in SwTwips
    */
    SwTwips GetWinPagesScrollAmount( const sal_Int16 _nWinPagesToScroll ) const;

    /** determine row the page with the given number is in

        @param _nPageNum
        input parameter - physical page number of page, for which the row in
        preview layout has to be calculated.

        @return number of row the page with the given physical page number is in
    */
    sal_uInt16 GetRowOfPage( sal_uInt16 _nPageNum ) const;

    /** determine column the page with the given number is in

        @param _nPageNum
        input parameter - physical page number of page, for which the column in
        preview layout has to be calculated.

        @return number of column the page with the given physical page number is in
    */
    sal_uInt16 GetColOfPage( sal_uInt16 _nPageNum ) const;

    Size GetPreviewDocSize() const;

    /** get size of a preview page by its physical page number

        @param _nPageNum
        input parameter - physical page number of preview page, for which the
        page size has to be returned.

        @return an object of class <Size>
    */
    Size GetPreviewPageSizeByPageNum( sal_uInt16 _nPageNum ) const;

    /** get virtual page number by its physical page number

        @param _nPageNum
        input parameter - physical page number of preview page, for which the
        virtual page number has to be determined.

        @return virtual page number of page given by its physical page number,
        if the page is in the current preview pages vector, otherwise 0.
    */
    sal_uInt16 GetVirtPageNumByPageNum( sal_uInt16 _nPageNum ) const;

    /** enable/disable book preview */
    bool SetBookPreviewMode( const bool  _bEnableBookPreview,
                             sal_uInt16& _onStartPageNum,
                             tools::Rectangle&  _orDocPreviewPaintRect );

    /** Convert relative to absolute page numbers (see PrintEmptyPages) */
    sal_uInt16 ConvertRelativeToAbsolutePageNum( sal_uInt16 _nRelPageNum ) const;

    /** Convert absolute to relative page numbers (see PrintEmptyPages) */
    sal_uInt16 ConvertAbsoluteToRelativePageNum( sal_uInt16 _nAbsPageNum ) const;

    SwViewShell& GetParentViewShell()
    {
        return mrParentViewShell;
    }
};

#endif // INCLUDED_SW_INC_PAGEPREVIEWLAYOUT_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */