summaryrefslogtreecommitdiff
path: root/svx/source/dialog/pagectrl.cxx
blob: 2a9dfb0a30b340c774ed23eb67b3eebea3e33a02 (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
/* -*- 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 .
 */

#include <memory>
#include <editeng/frmdir.hxx>
#include <vcl/canvastools.hxx>
#include <vcl/outdev.hxx>
#include <vcl/settings.hxx>
#include <tools/fract.hxx>
#include <svx/pageitem.hxx>
#include <svx/pagectrl.hxx>
#include <algorithm>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <drawinglayer/geometry/viewinformation2d.hxx>
#include <drawinglayer/processor2d/baseprocessor2d.hxx>
#include <drawinglayer/processor2d/processor2dtools.hxx>
#include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>

#define CELL_WIDTH      1600L
#define CELL_HEIGHT      800L

SvxPageWindow::SvxPageWindow() :
    aWinSize(),
    aSize(),
    nTop(0),
    nBottom(0),
    nLeft(0),
    nRight(0),
    bResetBackground(false),
    bFrameDirection(false),
    nFrameDirection(SvxFrameDirection::Horizontal_LR_TB),
    nHdLeft(0),
    nHdRight(0),
    nHdDist(0),
    nHdHeight(0),
    nFtLeft(0),
    nFtRight(0),
    nFtDist(0),
    nFtHeight(0),
    maHeaderFillAttributes(),
    maFooterFillAttributes(),
    maPageFillAttributes(),
    bFooter(false),
    bHeader(false),
    bTable(false),
    bHorz(false),
    bVert(false),
    eUsage(SvxPageUsage::All)
{
}

SvxPageWindow::~SvxPageWindow()
{
}

void SvxPageWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
{
    rRenderContext.Push(PushFlags::MAPMODE);
    rRenderContext.SetMapMode(MapMode(MapUnit::MapTwip));

    Fraction aXScale(aWinSize.Width(), std::max(tools::Long(aSize.Width() * 2 + aSize.Width() / 8), 1L));
    Fraction aYScale(aWinSize.Height(), std::max(aSize.Height(), 1L));
    MapMode aMapMode(rRenderContext.GetMapMode());

    if(aYScale < aXScale)
    {
        aMapMode.SetScaleX(aYScale);
        aMapMode.SetScaleY(aYScale);
    }
    else
    {
        aMapMode.SetScaleX(aXScale);
        aMapMode.SetScaleY(aXScale);
    }
    rRenderContext.SetMapMode(aMapMode);
    Size aSz(rRenderContext.PixelToLogic(GetOutputSizePixel()));
    tools::Long nYPos = (aSz.Height() - aSize.Height()) / 2;

    if (eUsage == SvxPageUsage::All)
    {
        // all pages are equal -> draw one page
        if (aSize.Width() > aSize.Height())
        {
            // Draw Landscape page of the same size
            Fraction aX = aMapMode.GetScaleX();
            Fraction aY = aMapMode.GetScaleY();
            Fraction a2(1.5);
            aX *= a2;
            aY *= a2;
            aMapMode.SetScaleX(aX);
            aMapMode.SetScaleY(aY);
            rRenderContext.SetMapMode(aMapMode);
            aSz = rRenderContext.PixelToLogic(GetOutputSizePixel());
            nYPos = (aSz.Height() - aSize.Height()) / 2;
            tools::Long nXPos = (aSz.Width() - aSize.Width()) / 2;
            DrawPage(rRenderContext, Point(nXPos,nYPos),true,true);
        }
        else
            // Portrait
            DrawPage(rRenderContext, Point((aSz.Width() - aSize.Width()) / 2,nYPos),true,true);
    }
    else
    {
        // Left and right page are different -> draw two pages if possible
        DrawPage(rRenderContext, Point(0, nYPos), false,
                 eUsage == SvxPageUsage::Left || eUsage == SvxPageUsage::All || eUsage == SvxPageUsage::Mirror);
        DrawPage(rRenderContext, Point(aSize.Width() + aSize.Width() / 8, nYPos), true,
                 eUsage == SvxPageUsage::Right || eUsage == SvxPageUsage::All || eUsage == SvxPageUsage::Mirror);
    }
    rRenderContext.Pop();
}

void SvxPageWindow::DrawPage(vcl::RenderContext& rRenderContext, const Point& rOrg, const bool bSecond, const bool bEnabled)
{
    const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
    const Color& rFieldColor = rStyleSettings.GetFieldColor();
    const Color& rFieldTextColor = rStyleSettings.GetFieldTextColor();
    const Color& rDisableColor = rStyleSettings.GetDisableColor();
    const Color& rDlgColor = rStyleSettings.GetDialogColor();

    // background
    if (!bSecond || bResetBackground)
    {
        rRenderContext.SetLineColor(COL_TRANSPARENT);
        rRenderContext.SetFillColor(rDlgColor);
        Size winSize(rRenderContext.GetOutputSize());
        rRenderContext.DrawRect(tools::Rectangle(Point(0,0), winSize));

        if (bResetBackground)
            bResetBackground = false;
    }
    rRenderContext.SetLineColor(rFieldTextColor);

    // Shadow
    Size aTempSize = aSize;

    // Page
    if (!bEnabled)
    {
        rRenderContext.SetFillColor(rDisableColor);
        rRenderContext.DrawRect(tools::Rectangle(rOrg, aTempSize));
        return;
    }
    rRenderContext.SetFillColor(rFieldColor);
    rRenderContext.DrawRect(tools::Rectangle(rOrg, aTempSize));

    tools::Long nL = nLeft;
    tools::Long nR = nRight;

    if (eUsage == SvxPageUsage::Mirror && !bSecond)
    {
        // turn for mirrored
        nL = nRight;
        nR = nLeft;
    }

    tools::Rectangle aRect;

    aRect.SetLeft( rOrg.X() + nL );
    aRect.SetRight( rOrg.X() + aTempSize.Width() - nR );
    aRect.SetTop( rOrg.Y() + nTop );
    aRect.SetBottom( rOrg.Y() + aTempSize.Height() - nBottom );

    tools::Rectangle aHdRect(aRect);
    tools::Rectangle aFtRect(aRect);

    if (bHeader || bFooter)
    {
        // Header and/or footer used
        const Color aLineColor(rRenderContext.GetLineColor());

        // draw PageFill first and on the whole page, no outline
        rRenderContext.SetLineColor();
        drawFillAttributes(rRenderContext, maPageFillAttributes, aRect, aRect);
        rRenderContext.SetLineColor(aLineColor);

        if (bHeader)
        {
            // show headers if possible
            aHdRect.AdjustLeft(nHdLeft );
            aHdRect.AdjustRight( -nHdRight );
            aHdRect.SetBottom( aRect.Top() + nHdHeight );
            aRect.AdjustTop(nHdHeight + nHdDist );

            // draw header over PageFill, plus outline
            drawFillAttributes(rRenderContext, maHeaderFillAttributes, aHdRect, aHdRect);
        }

        if (bFooter)
        {
            // show footer if possible
            aFtRect.AdjustLeft(nFtLeft );
            aFtRect.AdjustRight( -nFtRight );
            aFtRect.SetTop( aRect.Bottom() - nFtHeight );
            aRect.AdjustBottom( -(nFtHeight + nFtDist) );

            // draw footer over PageFill, plus outline
            drawFillAttributes(rRenderContext, maFooterFillAttributes, aFtRect, aFtRect);
        }

        // draw page's reduced outline, only outline
        drawFillAttributes(rRenderContext, drawinglayer::attribute::SdrAllFillAttributesHelperPtr(), aRect, aRect);
    }
    else
    {
        // draw PageFill and outline
        drawFillAttributes(rRenderContext, maPageFillAttributes, aRect, aRect);
    }

    if (bFrameDirection && !bTable)
    {
        Point aPos;
        vcl::Font aFont(rRenderContext.GetFont());
        const Size aSaveSize = aFont.GetFontSize();
        Size aDrawSize(0,aRect.GetHeight() / 6);
        aFont.SetFontSize(aDrawSize);
        rRenderContext.SetFont(aFont);
        OUString sText("ABC");
        Point aMove(1, rRenderContext.GetTextHeight());
        sal_Unicode cArrow = 0x2193;
        tools::Long nAWidth = rRenderContext.GetTextWidth(sText.copy(0,1));
        switch (nFrameDirection)
        {
        case SvxFrameDirection::Horizontal_LR_TB:
            aPos = aRect.TopLeft();
            aPos.AdjustX(rRenderContext.PixelToLogic(Point(1,1)).X() );
            aMove.setY( 0 );
            cArrow = 0x2192;
            break;
        case SvxFrameDirection::Horizontal_RL_TB:
            aPos = aRect.TopRight();
            aPos.AdjustX( -nAWidth );
            aMove.setY( 0 );
            aMove.setX( aMove.X() * -1 );
            cArrow = 0x2190;
            break;
        case SvxFrameDirection::Vertical_LR_TB:
            aPos = aRect.TopLeft();
            aPos.AdjustX(rRenderContext.PixelToLogic(Point(1,1)).X() );
            aMove.setX( 0 );
            break;
        case SvxFrameDirection::Vertical_RL_TB:
            aPos = aRect.TopRight();
            aPos.AdjustX( -nAWidth );
            aMove.setX( 0 );
            break;
        default: break;
        }
        sText += OUStringChar(cArrow);
        for (sal_Int32 i = 0; i < sText.getLength(); i++)
        {
            OUString sDraw(sText.copy(i,1));
            tools::Long nHDiff = 0;
            tools::Long nCharWidth = rRenderContext.GetTextWidth(sDraw);
            bool bHorizontal = 0 == aMove.Y();
            if (!bHorizontal)
            {
                nHDiff = (nAWidth - nCharWidth) / 2;
                aPos.AdjustX(nHDiff );
            }
            rRenderContext.DrawText(aPos,sDraw);
            if (bHorizontal)
            {
                aPos.AdjustX(aMove.X() < 0 ? -nCharWidth : nCharWidth );
            }
            else
            {
                aPos.AdjustX( -nHDiff );
                aPos.AdjustY(aMove.Y() );
            }
        }
        aFont.SetFontSize(aSaveSize);
        rRenderContext.SetFont(aFont);

    }
    if (!bTable)
        return;

    // Paint Table, if necessary center it
    rRenderContext.SetLineColor(COL_LIGHTGRAY);

    tools::Long nW = aRect.GetWidth();
    tools::Long nH = aRect.GetHeight();
    tools::Long const nTW = CELL_WIDTH * 3;
    tools::Long const nTH = CELL_HEIGHT * 3;
    tools::Long _nLeft = bHorz ? aRect.Left() + ((nW - nTW) / 2) : aRect.Left();
    tools::Long _nTop = bVert ? aRect.Top() + ((nH - nTH) / 2) : aRect.Top();
    tools::Rectangle aCellRect(Point(_nLeft, _nTop),Size(CELL_WIDTH, CELL_HEIGHT));

    for (sal_uInt16 i = 0; i < 3; ++i)
    {
        aCellRect.SetLeft( _nLeft );
        aCellRect.SetRight( _nLeft + CELL_WIDTH );
        if(i > 0)
            aCellRect.Move(0,CELL_HEIGHT);

        for (sal_uInt16 j = 0; j < 3; ++j)
        {
            if (j > 0)
                aCellRect.Move(CELL_WIDTH,0);
            rRenderContext.DrawRect(aCellRect);
        }
    }
}

void SvxPageWindow::drawFillAttributes(vcl::RenderContext& rRenderContext,
                                       const drawinglayer::attribute::SdrAllFillAttributesHelperPtr& rFillAttributes,
                                       const tools::Rectangle& rPaintRange,
                                       const tools::Rectangle& rDefineRange)
{
    const basegfx::B2DRange aPaintRange = vcl::unotools::b2DRectangleFromRectangle(rPaintRange);

    if(aPaintRange.isEmpty() ||
       basegfx::fTools::equalZero(aPaintRange.getWidth()) ||
       basegfx::fTools::equalZero(aPaintRange.getHeight()))
        return;

    const basegfx::B2DRange aDefineRange = vcl::unotools::b2DRectangleFromRectangle(rDefineRange);

    // prepare primitive sequence
    drawinglayer::primitive2d::Primitive2DContainer aSequence;

    // create fill geometry if there is something to fill
    if (rFillAttributes && rFillAttributes->isUsed())
    {
        aSequence = rFillAttributes->getPrimitive2DSequence(aPaintRange, aDefineRange);
    }

    // create line geometry if a LineColor is set at the target device
    if (rRenderContext.IsLineColor())
    {
        const drawinglayer::primitive2d::Primitive2DReference xOutline(
            new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
                basegfx::utils::createPolygonFromRect(aPaintRange), rRenderContext.GetLineColor().getBColor()));

        aSequence.push_back(xOutline);
    }

    // draw that if we have something to draw
    if (aSequence.empty())
        return;

    const drawinglayer::geometry::ViewInformation2D aViewInformation2D(
                    basegfx::B2DHomMatrix(), rRenderContext.GetViewTransformation(), aPaintRange, nullptr,
                    0.0, css::uno::Sequence<css::beans::PropertyValue >());

    std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
        drawinglayer::processor2d::createProcessor2DFromOutputDevice(rRenderContext, aViewInformation2D));
    if (pProcessor)
    {
        pProcessor->process(aSequence);
    }
}


void SvxPageWindow::EnableFrameDirection(bool bEnable)
{
    bFrameDirection = bEnable;
}

void SvxPageWindow::SetFrameDirection(SvxFrameDirection nDirection)
{
    nFrameDirection = nDirection;
}

void SvxPageWindow::ResetBackground()
{
    bResetBackground = true;
}

void SvxPageWindow::SetDrawingArea(weld::DrawingArea* pDrawingArea)
{
    CustomWidgetController::SetDrawingArea(pDrawingArea);

    OutputDevice& rRefDevice = pDrawingArea->get_ref_device();
    // Count in Twips by default
    rRefDevice.Push(PushFlags::MAPMODE);
    rRefDevice.SetMapMode(MapMode(MapUnit::MapTwip));
    aWinSize = rRefDevice.LogicToPixel(Size(75, 46), MapMode(MapUnit::MapAppFont));
    pDrawingArea->set_size_request(aWinSize.Width(), aWinSize.Height());

    aWinSize.AdjustHeight( -4 );
    aWinSize.AdjustWidth( -4 );

    aWinSize = rRefDevice.PixelToLogic(aWinSize);
    rRefDevice.Pop();
}

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