summaryrefslogtreecommitdiff
path: root/sw/inc/pagepreviewlayout.hxx
blob: 4b9e924ea9bb1cb26e7262ff21d8f185661c012f (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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* -*- 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>
// datatype <SwTwips>
#include <swtypes.hxx>

class SwViewShell;
class SwRootFrm;
class SwPageFrm;
class Fraction;
struct PreviewPage;

// =============================================================================
/** page preview functionality in the writer

    @author OD
*/
class SwPagePreviewLayout
{
private:
    friend class SwViewShell;

    /// number of horizontal and vertical twips for spacing between the pages.
    const SwTwips mnXFree;
    const SwTwips mnYFree;

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

    /** 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;
    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;
    Rectangle   maPaintedPreviewDocRect;
    sal_uInt16  mnSelectedPageNum;

    std::vector<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

        @author OD
    */
    void _Clear();

    /** helper method to clear preview page layout sizes

        @author OD
    */
    void _ClearPreviewLayoutSizes();

    /** helper method to clear data in preview page vectors

        @author OD
    */
    void _ClearPreviewPageData();

    /** calculate page preview layout sizes

        @author OD
    */
    void _CalcPreviewLayoutSizes();

    /** apply new zoom at given view shell

        @author OD

        @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.

        @author OD
    */
    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>.

        @author OD
    */
    void _CalcDocPreviewPaintRect();

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


        @author OD

        @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.

        @return boolean, indicating, if calculation was successful.
    */
    bool _CalcPreviewDataForPage( const SwPageFrm& _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.

        @author OD
    */
    void _CalcPreviewPages();

    /** get preview page by physical page number

        @author OD

        @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

        @author OD
    */
    void _PaintSelectMarkAtPage( const PreviewPage* _aSelectedPreviewPage ) const;

public:
    /** constructor of <SwPagePreviewLayout>

        @author OD

        @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 _rLayoutRootFrm
        input parameter - constant reference to the root frame of the layout.
        Reference will be hold as member <mrLayoutRootFrm> in order to get
        access to the page frames.
    */
    SwPagePreviewLayout( SwViewShell& _rParentViewShell,
                         const SwRootFrm&    _rLayoutRootFrm );

    /** destructor of <SwPagePreviewLayout>

        @author
    */
    inline ~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.

        @author OD

        @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.

        @param _bCalcScale
        input parameter - control, if method should calculate the needed
        scaling for the proposed preview layout for the given window size
        and sets the scaling at the output device and the view options.

        @return boolean, indicating, if preview layout is successful initialized.
    */
    bool Init( const sal_uInt16 _nCols,
               const sal_uInt16 _nRows,
               const Size&      _rPxWinSize,
               const bool       _bCalcScale
              );

    /** method to adjust page preview layout to document changes

        @author OD

        @return boolean, indicating, if preview layout is successful initialized.
    */
    bool 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 a absolute position
        (parameter <_aProposedStartPoint>).
        The accessibility preview will also be updated via a corresponding
        method call.

        @author OD

        @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      _aProposedStartPos,
                  const Size&      _rPxWinSize,
                  sal_uInt16&      _onStartPageNum,
                  Rectangle&       _orDocPreviewPaintRect,
                  const bool       _bStartWithPageAtFirstCol = true
                );

    /** get selected page number

        @author OD
    */
    inline sal_uInt16 SelectedPage()
    {
        return mnSelectedPageNum;
    }

    /** set selected page number

        @author OD
    */
    inline void SetSelectedPage( sal_uInt16 _nSelectedPageNum )
    {
        mnSelectedPageNum = _nSelectedPageNum;
    }

    /** paint prepared preview

        @author OD

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

        @return boolean, indicating, if paint of preview was performed
    */
    bool Paint( const Rectangle _aOutRect ) const;

    /** repaint pages on page preview

        method to invalidate visible pages due to changes in a different
        view shell.

        @author OD
    */
    void Repaint( const Rectangle _aInvalidCoreRect ) 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.

        @author OD

        @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.

        @author OD

        @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

        @author OD

        @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.

        @author OD

        @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

        @return boolean - indicating, that move was sucessful.
    */
    bool 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

        @author OD

        @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, it's 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, it's 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  _aPreviewPos,
                                   Point&       _orDocPos,
                                   bool&        _obPosInEmptyPage,
                                   sal_uInt16&  _onPageNum ) const;

    inline bool DoesPreviewLayoutRowsFitIntoWindow() const
    {
        return mbDoesLayoutRowsFitIntoWindow;
    }

    inline bool DoesPreviewLayoutColsFitIntoWindow() const
    {
        return mbDoesLayoutColsFitIntoWindow;
    }

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

    /** determine preview window page scroll amount

        @author OD

        @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

        @author OD

        @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

        @author OD

        @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

        @author OD

        @param _nPageNum
        input parameter - pysical 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

        @author OD

        @param _nPageNum
        input parameter - pysical 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

        @author OD
    */
    bool SetBookPreviewMode( const bool  _bEnableBookPreview,
                             sal_uInt16& _onStartPageNum,
                             Rectangle&  _orDocPreviewPaintRect );

    /** Convert relative to absolute page numbers (see PrintEmptyPages)

        @author FME
    */
    sal_uInt16 ConvertRelativeToAbsolutePageNum( sal_uInt16 _nRelPageNum ) const;

    /** Convert absolute to relative page numbers (see PrintEmptyPages)

        @author FME
    */
    sal_uInt16 ConvertAbsoluteToRelativePageNum( sal_uInt16 _nAbsPageNum ) const;

    /** get the number of preview pages

        @author FME
    */
    sal_uInt16 GetNumberOfPreviewPages() { return sal::static_int_cast< sal_uInt16 >(maPreviewPages.size()); }
};

#endif // INCLUDED_SW_INC_PAGEPREVIEWLAYOUT_HXX

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