Bump version to 24.04.3.4
[LibreOffice.git] / svx / source / sdr / overlay / overlayselection.cxx
blob9d805fdb25c7b558eb850b14d880dfdc44d14af4
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 <svx/sdr/overlay/overlayselection.hxx>
21 #include <basegfx/polygon/b2dpolygontools.hxx>
22 #include <basegfx/polygon/b2dpolygon.hxx>
23 #include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx>
24 #include <drawinglayer/primitive2d/PolyPolygonHairlinePrimitive2D.hxx>
25 #include <svtools/optionsdrawinglayer.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/outdev.hxx>
28 #include <vcl/settings.hxx>
29 #include <drawinglayer/primitive2d/invertprimitive2d.hxx>
30 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
31 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
32 #include <svx/sdr/overlay/overlaymanager.hxx>
33 #include <officecfg/Office/Common.hxx>
36 namespace sdr::overlay
38 // combine rages geometrically to a single, ORed polygon
39 static basegfx::B2DPolyPolygon impCombineRangesToPolyPolygon(const std::vector< basegfx::B2DRange >& rRanges)
41 const sal_uInt32 nCount(rRanges.size());
42 basegfx::B2DPolyPolygon aRetval;
44 for(sal_uInt32 a(0); a < nCount; a++)
46 const basegfx::B2DPolygon aDiscretePolygon(basegfx::utils::createPolygonFromRect(rRanges[a]));
48 if(0 == a)
50 aRetval.append(aDiscretePolygon);
52 else
54 aRetval = basegfx::utils::solvePolygonOperationOr(aRetval, basegfx::B2DPolyPolygon(aDiscretePolygon));
58 return aRetval;
61 // check if wanted type OverlayType::Transparent or OverlayType::Solid
62 // is possible. If not, fallback to invert mode (classic mode)
63 static OverlayType impCheckPossibleOverlayType(OverlayType aOverlayType)
65 if(OverlayType::Invert != aOverlayType)
67 if(!officecfg::Office::Common::Drawinglayer::TransparentSelection::get())
69 // not possible when switched off by user
70 return OverlayType::Invert;
72 else if (const OutputDevice* pOut = Application::GetDefaultDevice())
75 if(pOut->GetSettings().GetStyleSettings().GetHighContrastMode())
77 // not possible when in high contrast mode
78 return OverlayType::Invert;
81 if(!pOut->SupportsOperation(OutDevSupportType::TransparentRect))
83 // not possible when no fast transparence paint is supported on the system
84 return OverlayType::Invert;
89 return aOverlayType;
92 drawinglayer::primitive2d::Primitive2DContainer OverlaySelection::createOverlayObjectPrimitive2DSequence()
94 drawinglayer::primitive2d::Primitive2DContainer aRetval;
95 const sal_uInt32 nCount(getRanges().size());
97 if(nCount)
99 // create range primitives
100 const bool bInvert(OverlayType::Invert == maLastOverlayType);
101 basegfx::BColor aRGBColor(getBaseColor().getBColor());
102 aRetval.resize(nCount);
104 if(bInvert)
106 // force color to white for invert to get a full invert
107 aRGBColor = basegfx::BColor(1.0, 1.0, 1.0);
110 for(sal_uInt32 a(0);a < nCount; a++)
112 const basegfx::B2DPolygon aPolygon(basegfx::utils::createPolygonFromRect(maRanges[a]));
113 aRetval[a] = drawinglayer::primitive2d::Primitive2DReference(
114 new drawinglayer::primitive2d::PolyPolygonColorPrimitive2D(
115 basegfx::B2DPolyPolygon(aPolygon),
116 aRGBColor));
119 if(bInvert)
121 // embed all in invert primitive
122 drawinglayer::primitive2d::Primitive2DReference aInvert(
123 new drawinglayer::primitive2d::InvertPrimitive2D(
124 std::move(aRetval)));
125 aRetval = drawinglayer::primitive2d::Primitive2DContainer { aInvert };
127 else if(OverlayType::Transparent == maLastOverlayType)
129 // embed all rectangles in transparent paint
130 const double fTransparence(mnLastTransparence / 100.0);
131 const drawinglayer::primitive2d::Primitive2DReference aUnifiedTransparence(
132 new drawinglayer::primitive2d::UnifiedTransparencePrimitive2D(
133 std::move(aRetval),
134 fTransparence));
136 if(mbBorder)
138 basegfx::B2DPolyPolygon aPolyPolygon(impCombineRangesToPolyPolygon(getRanges()));
139 const drawinglayer::primitive2d::Primitive2DReference aSelectionOutline(
140 new drawinglayer::primitive2d::PolyPolygonHairlinePrimitive2D(
141 std::move(aPolyPolygon),
142 aRGBColor));
144 // add both to result
145 aRetval = drawinglayer::primitive2d::Primitive2DContainer { aUnifiedTransparence, aSelectionOutline };
147 else
149 // just add transparent part
150 aRetval = drawinglayer::primitive2d::Primitive2DContainer { aUnifiedTransparence };
155 return aRetval;
158 OverlaySelection::OverlaySelection(
159 OverlayType eType,
160 const Color& rColor,
161 std::vector< basegfx::B2DRange >&& rRanges,
162 bool bBorder)
163 : OverlayObject(rColor),
164 meOverlayType(eType),
165 maRanges(std::move(rRanges)),
166 maLastOverlayType(eType),
167 mnLastTransparence(0),
168 mbBorder(bBorder)
170 // no AA for selection overlays
171 allowAntiAliase(false);
174 OverlaySelection::~OverlaySelection()
176 if(getOverlayManager())
178 getOverlayManager()->remove(*this);
182 drawinglayer::primitive2d::Primitive2DContainer OverlaySelection::getOverlayObjectPrimitive2DSequence() const
184 // get current values
185 const OverlayType aNewOverlayType(impCheckPossibleOverlayType(meOverlayType));
186 const sal_uInt16 nNewTransparence(SvtOptionsDrawinglayer::GetTransparentSelectionPercent());
188 if(!getPrimitive2DSequence().empty())
190 if(aNewOverlayType != maLastOverlayType
191 || nNewTransparence != mnLastTransparence)
193 // conditions of last local decomposition have changed, delete
194 const_cast< OverlaySelection* >(this)->resetPrimitive2DSequence();
198 if(getPrimitive2DSequence().empty())
200 // remember new values
201 const_cast< OverlaySelection* >(this)->maLastOverlayType = aNewOverlayType;
202 const_cast< OverlaySelection* >(this)->mnLastTransparence = nNewTransparence;
205 // call base implementation
206 return OverlayObject::getOverlayObjectPrimitive2DSequence();
209 void OverlaySelection::setRanges(std::vector< basegfx::B2DRange >&& rNew)
211 if(rNew != maRanges)
213 maRanges = std::move(rNew);
214 objectChange();
217 } // end of namespace
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */