calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / drawinglayer / source / primitive2d / wallpaperprimitive2d.cxx
blob1211e21c718fbaffa93cce4cc89fc40284cde884
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <primitive2d/wallpaperprimitive2d.hxx>
21 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
22 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
23 #include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/polygon/b2dpolygon.hxx>
26 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
27 #include <basegfx/matrix/b2dhommatrixtools.hxx>
28 #include <vcl/graph.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
32 namespace drawinglayer::primitive2d
34 void WallpaperBitmapPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*rViewInformation*/) const
36 Primitive2DReference aRetval;
38 if(!getLocalObjectRange().isEmpty() && !getBitmapEx().IsEmpty())
40 // get bitmap PIXEL size
41 const Size& rPixelSize = getBitmapEx().GetSizePixel();
43 if(rPixelSize.Width() > 0 && rPixelSize.Height() > 0)
45 if(WallpaperStyle::Scale == getWallpaperStyle())
47 // shortcut for scale; use simple BitmapPrimitive2D
48 basegfx::B2DHomMatrix aObjectTransform;
50 aObjectTransform.set(0, 0, getLocalObjectRange().getWidth());
51 aObjectTransform.set(1, 1, getLocalObjectRange().getHeight());
52 aObjectTransform.set(0, 2, getLocalObjectRange().getMinX());
53 aObjectTransform.set(1, 2, getLocalObjectRange().getMinY());
55 Primitive2DReference xReference(
56 new BitmapPrimitive2D(
57 getBitmapEx(),
58 aObjectTransform));
60 aRetval = xReference;
62 else
64 // transform to logic size
65 basegfx::B2DHomMatrix aInverseViewTransformation(getViewTransformation());
66 aInverseViewTransformation.invert();
67 basegfx::B2DVector aLogicSize(rPixelSize.Width(), rPixelSize.Height());
68 aLogicSize = aInverseViewTransformation * aLogicSize;
70 // apply layout
71 basegfx::B2DPoint aTargetTopLeft(getLocalObjectRange().getMinimum());
72 bool bUseTargetTopLeft(true);
73 bool bNeedsClipping(false);
75 switch(getWallpaperStyle())
77 default: //case WallpaperStyle::Tile :, also WallpaperStyle::NONE and WallpaperStyle::ApplicationGradient
79 bUseTargetTopLeft = false;
80 break;
82 case WallpaperStyle::Scale :
84 // handled by shortcut above
85 break;
87 case WallpaperStyle::TopLeft :
89 // nothing to do
90 break;
92 case WallpaperStyle::Top :
94 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
95 aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
96 break;
98 case WallpaperStyle::TopRight :
100 aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
101 break;
103 case WallpaperStyle::Left :
105 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
106 aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
107 break;
109 case WallpaperStyle::Center :
111 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
112 aTargetTopLeft = aCenter - (aLogicSize * 0.5);
113 break;
115 case WallpaperStyle::Right :
117 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
118 aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
119 aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
120 break;
122 case WallpaperStyle::BottomLeft :
124 aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
125 break;
127 case WallpaperStyle::Bottom :
129 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
130 aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
131 aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
132 break;
134 case WallpaperStyle::BottomRight :
136 aTargetTopLeft = getLocalObjectRange().getMaximum() - aLogicSize;
137 break;
141 if(bUseTargetTopLeft)
143 // fill target range
144 const basegfx::B2DRange aTargetRange(aTargetTopLeft, aTargetTopLeft + aLogicSize);
146 // create aligned, single BitmapPrimitive2D
147 basegfx::B2DHomMatrix aObjectTransform;
149 aObjectTransform.set(0, 0, aTargetRange.getWidth());
150 aObjectTransform.set(1, 1, aTargetRange.getHeight());
151 aObjectTransform.set(0, 2, aTargetRange.getMinX());
152 aObjectTransform.set(1, 2, aTargetRange.getMinY());
154 Primitive2DReference xReference(
155 new BitmapPrimitive2D(
156 getBitmapEx(),
157 aObjectTransform));
158 aRetval = xReference;
160 // clip when not completely inside object range
161 bNeedsClipping = !getLocalObjectRange().isInside(aTargetRange);
163 else
165 // WallpaperStyle::Tile, WallpaperStyle::NONE, WallpaperStyle::ApplicationGradient
166 // convert to relative positions
167 const basegfx::B2DVector aRelativeSize(
168 aLogicSize.getX() / (getLocalObjectRange().getWidth() ? getLocalObjectRange().getWidth() : 1.0),
169 aLogicSize.getY() / (getLocalObjectRange().getHeight() ? getLocalObjectRange().getHeight() : 1.0));
170 basegfx::B2DPoint aRelativeTopLeft(0.0, 0.0);
172 if(WallpaperStyle::Tile != getWallpaperStyle())
174 aRelativeTopLeft.setX(0.5 - aRelativeSize.getX());
175 aRelativeTopLeft.setY(0.5 - aRelativeSize.getY());
178 // prepare FillGraphicAttribute
179 const attribute::FillGraphicAttribute aFillGraphicAttribute(
180 Graphic(getBitmapEx()),
181 basegfx::B2DRange(aRelativeTopLeft, aRelativeTopLeft+ aRelativeSize),
182 true);
184 // create ObjectTransform
185 const basegfx::B2DHomMatrix aObjectTransform(
186 basegfx::utils::createScaleTranslateB2DHomMatrix(
187 getLocalObjectRange().getRange(),
188 getLocalObjectRange().getMinimum()));
190 // create FillBitmapPrimitive
191 const drawinglayer::primitive2d::Primitive2DReference xFillBitmap(
192 new drawinglayer::primitive2d::FillGraphicPrimitive2D(
193 aObjectTransform,
194 aFillGraphicAttribute));
195 aRetval = xFillBitmap;
197 // always embed tiled fill to clipping
198 bNeedsClipping = true;
201 if(bNeedsClipping)
203 // embed to clipping; this is necessary for tiled fills
204 basegfx::B2DPolyPolygon aPolyPolygon(
205 basegfx::utils::createPolygonFromRect(getLocalObjectRange()));
206 const drawinglayer::primitive2d::Primitive2DReference xClippedFill(
207 new drawinglayer::primitive2d::MaskPrimitive2D(
208 std::move(aPolyPolygon),
209 { aRetval }));
210 aRetval = xClippedFill;
216 if (aRetval.is())
217 rContainer.push_back(aRetval);
220 WallpaperBitmapPrimitive2D::WallpaperBitmapPrimitive2D(
221 const basegfx::B2DRange& rObjectRange,
222 const BitmapEx& rBitmapEx,
223 WallpaperStyle eWallpaperStyle)
224 : maObjectRange(rObjectRange),
225 maBitmapEx(rBitmapEx),
226 meWallpaperStyle(eWallpaperStyle)
230 bool WallpaperBitmapPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
232 if(ViewTransformationDependentPrimitive2D::operator==(rPrimitive))
234 const WallpaperBitmapPrimitive2D& rCompare = static_cast<const WallpaperBitmapPrimitive2D&>(rPrimitive);
236 return (getLocalObjectRange() == rCompare.getLocalObjectRange()
237 && getBitmapEx() == rCompare.getBitmapEx()
238 && getWallpaperStyle() == rCompare.getWallpaperStyle());
241 return false;
244 basegfx::B2DRange WallpaperBitmapPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
246 return getLocalObjectRange();
249 // provide unique ID
250 sal_uInt32 WallpaperBitmapPrimitive2D::getPrimitive2DID() const
252 return PRIMITIVE2D_ID_WALLPAPERBITMAPPRIMITIVE2D;
255 } // end of namespace
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */