Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/control/imgctrl.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 <vcl/toolkit/imgctrl.hxx>
21
22
#include <com/sun/star/awt/ImageScaleMode.hpp>
23
#include <osl/diagnose.h>
24
25
namespace ImageScaleMode = css::awt::ImageScaleMode;
26
27
ImageControl::ImageControl( vcl::Window* pParent, WinBits nStyle )
28
0
    :FixedImage( pParent, nStyle )
29
0
    ,mnScaleMode( ImageScaleMode::ANISOTROPIC )
30
0
{
31
0
}
Unexecuted instantiation: ImageControl::ImageControl(vcl::Window*, long)
Unexecuted instantiation: ImageControl::ImageControl(vcl::Window*, long)
32
33
void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
34
0
{
35
0
    if ( _nMode != mnScaleMode )
36
0
    {
37
0
        mnScaleMode = _nMode;
38
0
        Invalidate();
39
0
    }
40
0
}
41
42
void ImageControl::Resize()
43
0
{
44
0
    Invalidate();
45
0
}
46
47
namespace
48
{
49
    Size lcl_calcPaintSize( const tools::Rectangle& _rPaintRect, const Size& _rBitmapSize )
50
0
    {
51
0
        const Size aPaintSize = _rPaintRect.GetSize();
52
53
0
        const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
54
0
        const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
55
0
        const double nRatioMin = ::std::min( nRatioX, nRatioY );
56
57
0
        return Size( tools::Long( _rBitmapSize.Width() * nRatioMin ), tools::Long( _rBitmapSize.Height() * nRatioMin ) );
58
0
    }
59
60
    Point lcl_centerWithin( const tools::Rectangle& _rArea, const Size& _rObjectSize )
61
0
    {
62
0
        Point aPos( _rArea.TopLeft() );
63
0
        aPos.AdjustX(( _rArea.GetWidth() - _rObjectSize.Width() ) / 2 );
64
0
        aPos.AdjustY(( _rArea.GetHeight() - _rObjectSize.Height() ) / 2 );
65
0
        return aPos;
66
0
    }
67
}
68
69
void ImageControl::ImplDraw(OutputDevice& rDev, const Point& rPos, const Size& rSize) const
70
0
{
71
0
    DrawImageFlags nStyle = DrawImageFlags::NONE;
72
0
    if ( !IsEnabled() )
73
0
        nStyle |= DrawImageFlags::Disable;
74
75
0
    const Image& rImage( GetModeImage() );
76
0
    const tools::Rectangle aDrawRect( rPos, rSize );
77
0
    if (!rImage)
78
0
    {
79
0
        OUString  sText( GetText() );
80
0
        if ( sText.isEmpty() )
81
0
            return;
82
83
0
        WinBits nWinStyle = GetStyle();
84
0
        DrawTextFlags nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
85
0
        if ( !IsEnabled() )
86
0
            nTextStyle |= DrawTextFlags::Disable;
87
88
0
        rDev.DrawText( aDrawRect, sText, nTextStyle );
89
0
        return;
90
0
    }
91
92
0
    const Size aBitmapSize = rImage.GetSizePixel();
93
94
0
    switch ( mnScaleMode )
95
0
    {
96
0
    case ImageScaleMode::NONE:
97
0
    {
98
0
        rDev.DrawImage(lcl_centerWithin( aDrawRect, aBitmapSize ), rImage, nStyle);
99
0
    }
100
0
    break;
101
102
0
    case ImageScaleMode::ISOTROPIC:
103
0
    {
104
0
        const Size aPaintSize = lcl_calcPaintSize( aDrawRect, aBitmapSize );
105
0
        rDev.DrawImage(lcl_centerWithin(aDrawRect, aPaintSize), aPaintSize, rImage, nStyle);
106
0
    }
107
0
    break;
108
109
0
    case ImageScaleMode::ANISOTROPIC:
110
0
    {
111
0
        rDev.DrawImage(
112
0
            aDrawRect.TopLeft(),
113
0
            aDrawRect.GetSize(),
114
0
            rImage, nStyle );
115
0
    }
116
0
    break;
117
118
0
    default:
119
0
        OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
120
0
        break;
121
122
0
    }   // switch ( mnScaleMode )
123
0
}
124
125
void ImageControl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
126
0
{
127
0
    ImplDraw(rRenderContext, Point(), GetOutputSizePixel());
128
129
0
    if (!HasFocus())
130
0
        return;
131
132
0
    vcl::Window* pBorderWindow = GetWindow(GetWindowType::Border);
133
134
0
    bool bFlat = (GetBorderStyle() == WindowBorderStyle::MONO);
135
0
    tools::Rectangle aRect(Point(0,0), pBorderWindow->GetOutputSizePixel());
136
0
    auto popIt = pBorderWindow->GetOutDev()->ScopedPush(vcl::PushFlags::FILLCOLOR | vcl::PushFlags::LINECOLOR);
137
0
    pBorderWindow->GetOutDev()->SetFillColor();
138
0
    pBorderWindow->GetOutDev()->SetLineColor(bFlat ? COL_WHITE : COL_BLACK);
139
0
    pBorderWindow->GetOutDev()->DrawRect(aRect);
140
0
    aRect.AdjustLeft( 1 );
141
0
    aRect.AdjustRight( -1 );
142
0
    aRect.AdjustTop( 1 );
143
0
    aRect.AdjustBottom( -1 );
144
0
    pBorderWindow->GetOutDev()->SetLineColor(bFlat ? COL_BLACK : COL_WHITE);
145
0
    pBorderWindow->GetOutDev()->DrawRect(aRect);
146
0
}
147
148
void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, SystemTextColorFlags )
149
0
{
150
0
    const Point     aPos  = pDev->LogicToPixel( rPos );
151
0
    const Size      aSize = GetSizePixel();
152
0
    tools::Rectangle aRect( aPos, aSize );
153
154
0
    auto popIt = pDev->ScopedPush();
155
0
    pDev->SetMapMode();
156
157
    // Border
158
0
    if ( GetStyle() & WB_BORDER )
159
0
    {
160
0
        ImplDrawFrame( pDev, aRect );
161
0
    }
162
0
    pDev->IntersectClipRegion( aRect );
163
0
    ImplDraw( *pDev, aRect.TopLeft(), aRect.GetSize() );
164
0
}
165
166
void ImageControl::GetFocus()
167
0
{
168
0
    FixedImage::GetFocus();
169
0
    GetWindow( GetWindowType::Border )->Invalidate();
170
0
}
171
172
void ImageControl::LoseFocus()
173
0
{
174
0
    FixedImage::GetFocus();
175
0
    GetWindow( GetWindowType::Border )->Invalidate();
176
0
}
177
178
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */