Coverage Report

Created: 2026-09-28 10:59

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