1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #include <DashedLine.hxx>
11 
12 #include <basegfx/color/bcolortools.hxx>
13 #include <basegfx/polygon/b2dpolygon.hxx>
14 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
15 #include <drawinglayer/primitive2d/PolyPolygonStrokePrimitive2D.hxx>
16 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
17 #include <drawinglayer/processor2d/processorfromoutputdevice.hxx>
18 #include <vcl/svapp.hxx>
19 #include <vcl/settings.hxx>
20 #include <memory>
21 
SwDashedLine(vcl::Window * pParent,Color & (* pColorFn)())22 SwDashedLine::SwDashedLine( vcl::Window* pParent, Color& ( *pColorFn )() )
23     : Control( pParent, WB_DIALOGCONTROL | WB_HORZ )
24     , m_pColorFn( pColorFn )
25 {
26 }
27 
~SwDashedLine()28 SwDashedLine::~SwDashedLine( )
29 {
30 }
31 
Paint(vcl::RenderContext & rRenderContext,const tools::Rectangle &)32 void SwDashedLine::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
33 {
34     const drawinglayer::geometry::ViewInformation2D aNewViewInfos;
35     std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
36         drawinglayer::processor2d::createBaseProcessor2DFromOutputDevice(rRenderContext, aNewViewInfos));
37 
38     // Compute the start and end points
39     const tools::Rectangle aRect(tools::Rectangle(Point(0, 0), rRenderContext.PixelToLogic(GetSizePixel())));
40     double nHalfWidth = double(aRect.Top() + aRect.Bottom()) / 2.0;
41 
42     basegfx::B2DPoint aStart(double(aRect.Left()), nHalfWidth);
43     basegfx::B2DPoint aEnd(double(aRect.Right()), nHalfWidth);
44 
45     basegfx::B2DPolygon aPolygon;
46     aPolygon.append(aStart);
47     aPolygon.append(aEnd);
48 
49     drawinglayer::primitive2d::Primitive2DContainer aSeq(1);
50 
51     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
52 
53     std::vector<double> aStrokePattern;
54     basegfx::BColor aColor = m_pColorFn().getBColor();
55     if (rSettings.GetHighContrastMode())
56     {
57         // Only a solid line in high contrast mode
58         aColor = rSettings.GetDialogTextColor().getBColor();
59     }
60     else
61     {
62         // Get a color for the contrast
63         basegfx::BColor aHslLine = basegfx::utils::rgb2hsl(aColor);
64         double nLuminance = aHslLine.getZ();
65         nLuminance += (1.0 - nLuminance) * 0.75;
66         if (aHslLine.getZ() > 0.7)
67             nLuminance = aHslLine.getZ() * 0.7;
68         aHslLine.setZ(nLuminance);
69         const basegfx::BColor aOtherColor = basegfx::utils::hsl2rgb(aHslLine);
70 
71         // Compute the plain line
72         aSeq[0] = new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(aPolygon, aOtherColor);
73 
74         // Dashed line in twips
75         aStrokePattern.push_back(3);
76         aStrokePattern.push_back(3);
77 
78         aSeq.resize(2);
79     }
80 
81     // Compute the dashed line primitive
82     aSeq[aSeq.size() - 1] =
83             new drawinglayer::primitive2d::PolyPolygonStrokePrimitive2D(
84                 basegfx::B2DPolyPolygon(aPolygon),
85                 drawinglayer::attribute::LineAttribute(m_pColorFn().getBColor()),
86                 drawinglayer::attribute::StrokeAttribute(aStrokePattern));
87 
88     pProcessor->process(aSeq);
89 }
90 
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
92