/src/libreoffice/sc/source/ui/view/hintwin.cxx
Line | Count | Source |
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 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <overlayobject.hxx> |
21 | | |
22 | | #include <drawinglayer/attribute/fontattribute.hxx> |
23 | | #include <drawinglayer/geometry/viewinformation2d.hxx> |
24 | | #include <drawinglayer/primitive2d/PolygonHairlinePrimitive2D.hxx> |
25 | | #include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx> |
26 | | #include <drawinglayer/primitive2d/textlayoutdevice.hxx> |
27 | | #include <drawinglayer/primitive2d/textprimitive2d.hxx> |
28 | | #include <basegfx/polygon/b2dpolygontools.hxx> |
29 | | #include <basegfx/matrix/b2dhommatrixtools.hxx> |
30 | | #include <tools/lineend.hxx> |
31 | | #include <utility> |
32 | | #include <vcl/outdev.hxx> |
33 | | #include <vcl/settings.hxx> |
34 | | #include <vcl/metric.hxx> |
35 | | #include <sal/log.hxx> |
36 | | |
37 | 0 | #define HINT_LINESPACE 2 |
38 | 0 | #define HINT_INDENT 3 |
39 | 0 | #define HINT_MARGIN 4 |
40 | | |
41 | | ScOverlayHint::ScOverlayHint(OUString aTit, const OUString& rMsg, |
42 | | const Color& rBackColor, const Color& rTextColor, |
43 | | vcl::Font aFont) |
44 | 0 | : OverlayObject(rBackColor) |
45 | 0 | , m_aTitle(std::move(aTit)) |
46 | 0 | , m_aMessage(convertLineEnd(rMsg, LINEEND_CR)) |
47 | 0 | , m_aTextFont(std::move(aFont)) |
48 | 0 | , m_aTextColor(rTextColor) |
49 | 0 | , m_aMapMode(MapUnit::MapPixel) |
50 | 0 | , m_nLeft(0) |
51 | 0 | , m_nTop(0) |
52 | 0 | { |
53 | 0 | } |
54 | | |
55 | | drawinglayer::primitive2d::Primitive2DContainer ScOverlayHint::createOverlaySequence(sal_Int32 nLeft, sal_Int32 nTop, |
56 | | const MapMode &rMapMode, |
57 | | basegfx::B2DRange &rRange) const |
58 | 0 | { |
59 | 0 | OutputDevice* pDefaultDev = Application::GetDefaultDevice(); |
60 | 0 | MapMode aOld = pDefaultDev->GetMapMode(); |
61 | 0 | pDefaultDev->SetMapMode(rMapMode); |
62 | |
|
63 | 0 | vcl::Font aTextFont = m_aTextFont; |
64 | 0 | aTextFont.SetFontSize(pDefaultDev->PixelToLogic(aTextFont.GetFontSize(), rMapMode)); |
65 | 0 | vcl::Font aHeadFont = aTextFont; |
66 | 0 | aHeadFont.SetWeight(WEIGHT_BOLD); |
67 | | |
68 | | // Create the text primitive for the title |
69 | 0 | basegfx::B2DVector aFontSize; |
70 | 0 | drawinglayer::attribute::FontAttribute aFontAttr = |
71 | 0 | drawinglayer::primitive2d::getFontAttributeFromVclFont(aFontSize, aHeadFont, false, false); |
72 | |
|
73 | 0 | FontMetric aFontMetric = pDefaultDev->GetFontMetric(aHeadFont); |
74 | 0 | Size aHintMargin = pDefaultDev->PixelToLogic(Size(HINT_MARGIN, HINT_MARGIN), rMapMode); |
75 | 0 | Size aIndent = pDefaultDev->PixelToLogic(Size(HINT_INDENT, HINT_LINESPACE), rMapMode); |
76 | 0 | double nTextOffsetY = nTop + aHintMargin.Height() + aFontMetric.GetAscent(); |
77 | 0 | Point aTextPos(nLeft + aHintMargin.Width() , nTextOffsetY); |
78 | 0 | rRange = basegfx::B2DRange(nLeft, nTop, nLeft + aHintMargin.Width(), nTop + aHintMargin.Height()); |
79 | |
|
80 | 0 | basegfx::B2DHomMatrix aTextMatrix(basegfx::utils::createScaleTranslateB2DHomMatrix( |
81 | 0 | aFontSize.getX(), aFontSize.getY(), |
82 | 0 | aTextPos.X(), aTextPos.Y())); |
83 | |
|
84 | 0 | rtl::Reference<drawinglayer::primitive2d::TextSimplePortionPrimitive2D> pTitle = |
85 | 0 | new drawinglayer::primitive2d::TextSimplePortionPrimitive2D( |
86 | 0 | aTextMatrix, m_aTitle, 0, m_aTitle.getLength(), |
87 | 0 | std::vector<double>(), {}, std::move(aFontAttr), css::lang::Locale(), |
88 | 0 | m_aTextColor.getBColor()); |
89 | |
|
90 | 0 | Point aTextStart(nLeft + aHintMargin.Width() + aIndent.Width(), |
91 | 0 | nTop + aHintMargin.Height() + aFontMetric.GetLineHeight() + aIndent.Height()); |
92 | |
|
93 | 0 | drawinglayer::geometry::ViewInformation2D aDummy; |
94 | 0 | rRange.expand(pTitle->getB2DRange(aDummy)); |
95 | | |
96 | | // insert two empty elements as placeholders for bg and border |
97 | 0 | drawinglayer::primitive2d::Primitive2DContainer aSeq { nullptr, nullptr, pTitle }; |
98 | |
|
99 | 0 | aFontMetric = pDefaultDev->GetFontMetric(aTextFont); |
100 | 0 | pDefaultDev->SetMapMode(aOld); |
101 | |
|
102 | 0 | nTextOffsetY = aFontMetric.GetAscent(); |
103 | 0 | sal_Int32 nLineHeight = aFontMetric.GetLineHeight(); |
104 | |
|
105 | 0 | aFontAttr = drawinglayer::primitive2d::getFontAttributeFromVclFont(aFontSize, aTextFont, false, false); |
106 | |
|
107 | 0 | sal_Int32 nIndex = 0; |
108 | 0 | Point aLineStart = aTextStart; |
109 | 0 | sal_Int32 nLineCount = 0; |
110 | 0 | while (nIndex != -1) |
111 | 0 | { |
112 | 0 | OUString aLine = m_aMessage.getToken( 0, '\r', nIndex ); |
113 | 0 | if (aLine.getLength() > 255) |
114 | 0 | { |
115 | | // prevent silliness higher up from hanging up the program |
116 | 0 | SAL_WARN("sc", "ridiculously long line, truncating, len=" << aLine.getLength()); |
117 | 0 | aLine = aLine.copy(0,255); |
118 | 0 | } |
119 | | |
120 | 0 | aTextMatrix = basegfx::utils::createScaleTranslateB2DHomMatrix( |
121 | 0 | aFontSize.getX(), aFontSize.getY(), |
122 | 0 | aLineStart.X(), aLineStart.Y() + nTextOffsetY); |
123 | | |
124 | | // Create the text primitive for each line of text |
125 | 0 | rtl::Reference<drawinglayer::primitive2d::TextSimplePortionPrimitive2D> pMessage = |
126 | 0 | new drawinglayer::primitive2d::TextSimplePortionPrimitive2D( |
127 | 0 | aTextMatrix, aLine, 0, aLine.getLength(), |
128 | 0 | std::vector<double>(), {}, aFontAttr, css::lang::Locale(), |
129 | 0 | m_aTextColor.getBColor()); |
130 | |
|
131 | 0 | rRange.expand(pMessage->getB2DRange(aDummy)); |
132 | |
|
133 | 0 | aSeq.push_back(pMessage); |
134 | |
|
135 | 0 | aLineStart.AdjustY(nLineHeight ); |
136 | 0 | nLineCount++; |
137 | 0 | if (nLineCount > 50) |
138 | 0 | { |
139 | | // prevent silliness higher up from hanging up the program |
140 | 0 | SAL_WARN("sc", "ridiculously long message, bailing out"); |
141 | 0 | break; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | 0 | rRange.expand(basegfx::B2DTuple(rRange.getMaxX() + aHintMargin.Width(), |
146 | 0 | rRange.getMaxY() + aHintMargin.Height())); |
147 | |
|
148 | 0 | basegfx::B2DPolygon aPoly(basegfx::utils::createPolygonFromRect(rRange)); |
149 | | |
150 | | // background |
151 | 0 | aSeq[0] = drawinglayer::primitive2d::Primitive2DReference( |
152 | 0 | new drawinglayer::primitive2d::PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aPoly), getBaseColor().getBColor())); |
153 | | // border |
154 | 0 | aSeq[1] = drawinglayer::primitive2d::Primitive2DReference( |
155 | 0 | new drawinglayer::primitive2d::PolygonHairlinePrimitive2D( |
156 | 0 | std::move(aPoly), basegfx::BColor(0.5, 0.5, 0.5))); |
157 | |
|
158 | 0 | return aSeq; |
159 | 0 | } |
160 | | |
161 | | drawinglayer::primitive2d::Primitive2DContainer ScOverlayHint::createOverlayObjectPrimitive2DSequence() |
162 | 0 | { |
163 | 0 | basegfx::B2DRange aRange; |
164 | 0 | return createOverlaySequence(m_nLeft, m_nTop, m_aMapMode, aRange); |
165 | 0 | } |
166 | | |
167 | | Size ScOverlayHint::GetSizePixel() const |
168 | 0 | { |
169 | 0 | basegfx::B2DRange aRange; |
170 | 0 | createOverlaySequence(0, 0, MapMode(MapUnit::MapPixel), aRange); |
171 | 0 | return Size(aRange.getWidth(), aRange.getHeight()); |
172 | 0 | } |
173 | | |
174 | | void ScOverlayHint::SetPos(const Point& rPos, const MapMode& rMode) |
175 | 0 | { |
176 | 0 | m_nLeft = rPos.X(); |
177 | 0 | m_nTop = rPos.Y(); |
178 | 0 | m_aMapMode = rMode; |
179 | 0 | } |
180 | | |
181 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |