1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
]));
50 aRetval
.append(aDiscretePolygon
);
54 aRetval
= basegfx::utils::solvePolygonOperationOr(aRetval
, basegfx::B2DPolyPolygon(aDiscretePolygon
));
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
;
92 drawinglayer::primitive2d::Primitive2DContainer
OverlaySelection::createOverlayObjectPrimitive2DSequence()
94 drawinglayer::primitive2d::Primitive2DContainer aRetval
;
95 const sal_uInt32
nCount(getRanges().size());
99 // create range primitives
100 const bool bInvert(OverlayType::Invert
== maLastOverlayType
);
101 basegfx::BColor
aRGBColor(getBaseColor().getBColor());
102 aRetval
.resize(nCount
);
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
),
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(
138 basegfx::B2DPolyPolygon
aPolyPolygon(impCombineRangesToPolyPolygon(getRanges()));
139 const drawinglayer::primitive2d::Primitive2DReference
aSelectionOutline(
140 new drawinglayer::primitive2d::PolyPolygonHairlinePrimitive2D(
141 std::move(aPolyPolygon
),
144 // add both to result
145 aRetval
= drawinglayer::primitive2d::Primitive2DContainer
{ aUnifiedTransparence
, aSelectionOutline
};
149 // just add transparent part
150 aRetval
= drawinglayer::primitive2d::Primitive2DContainer
{ aUnifiedTransparence
};
158 OverlaySelection::OverlaySelection(
161 std::vector
< basegfx::B2DRange
>&& rRanges
,
163 : OverlayObject(rColor
),
164 meOverlayType(eType
),
165 maRanges(std::move(rRanges
)),
166 maLastOverlayType(eType
),
167 mnLastTransparence(0),
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
)
213 maRanges
= std::move(rNew
);
217 } // end of namespace
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */