LibreOffice Module vcl (master) 1
imgctrl.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/awt/ImageScaleMode.hpp>
23#include <osl/diagnose.h>
24
25namespace ImageScaleMode = css::awt::ImageScaleMode;
26
27ImageControl::ImageControl( vcl::Window* pParent, WinBits nStyle )
28 :FixedImage( pParent, nStyle )
29 ,mnScaleMode( ImageScaleMode::ANISOTROPIC )
30{
31}
32
33void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
34{
35 if ( _nMode != mnScaleMode )
36 {
37 mnScaleMode = _nMode;
38 Invalidate();
39 }
40}
41
42void ImageControl::Resize()
43{
44 Invalidate();
45}
46
47namespace
48{
49 Size lcl_calcPaintSize( const tools::Rectangle& _rPaintRect, const Size& _rBitmapSize )
50 {
51 const Size aPaintSize = _rPaintRect.GetSize();
52
53 const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
54 const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
55 const double nRatioMin = ::std::min( nRatioX, nRatioY );
56
57 return Size( tools::Long( _rBitmapSize.Width() * nRatioMin ), tools::Long( _rBitmapSize.Height() * nRatioMin ) );
58 }
59
60 Point lcl_centerWithin( const tools::Rectangle& _rArea, const Size& _rObjectSize )
61 {
62 Point aPos( _rArea.TopLeft() );
63 aPos.AdjustX(( _rArea.GetWidth() - _rObjectSize.Width() ) / 2 );
64 aPos.AdjustY(( _rArea.GetHeight() - _rObjectSize.Height() ) / 2 );
65 return aPos;
66 }
67}
68
69void ImageControl::ImplDraw(OutputDevice& rDev, const Point& rPos, const Size& rSize) const
70{
72 if ( !IsEnabled() )
74
75 const Image& rImage( GetModeImage() );
76 const tools::Rectangle aDrawRect( rPos, rSize );
77 if (!rImage)
78 {
79 OUString sText( GetText() );
80 if ( sText.isEmpty() )
81 return;
82
83 WinBits nWinStyle = GetStyle();
84 DrawTextFlags nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
85 if ( !IsEnabled() )
86 nTextStyle |= DrawTextFlags::Disable;
87
88 rDev.DrawText( aDrawRect, sText, nTextStyle );
89 return;
90 }
91
92 const Size& rBitmapSize = rImage.GetSizePixel();
93
94 switch ( mnScaleMode )
95 {
96 case ImageScaleMode::NONE:
97 {
98 rDev.DrawImage(lcl_centerWithin( aDrawRect, rBitmapSize ), rImage, nStyle);
99 }
100 break;
101
102 case ImageScaleMode::ISOTROPIC:
103 {
104 const Size aPaintSize = lcl_calcPaintSize( aDrawRect, rBitmapSize );
105 rDev.DrawImage(lcl_centerWithin(aDrawRect, aPaintSize), aPaintSize, rImage, nStyle);
106 }
107 break;
108
109 case ImageScaleMode::ANISOTROPIC:
110 {
111 rDev.DrawImage(
112 aDrawRect.TopLeft(),
113 aDrawRect.GetSize(),
114 rImage, nStyle );
115 }
116 break;
117
118 default:
119 OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
120 break;
121
122 } // switch ( mnScaleMode )
123}
124
125void ImageControl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
126{
127 ImplDraw(rRenderContext, Point(), GetOutputSizePixel());
128
129 if (!HasFocus())
130 return;
131
132 vcl::Window* pBorderWindow = GetWindow(GetWindowType::Border);
133
134 bool bFlat = (GetBorderStyle() == WindowBorderStyle::MONO);
135 tools::Rectangle aRect(Point(0,0), pBorderWindow->GetOutputSizePixel());
136 Color oldLineCol = pBorderWindow->GetOutDev()->GetLineColor();
137 Color oldFillCol = pBorderWindow->GetOutDev()->GetFillColor();
138 pBorderWindow->GetOutDev()->SetFillColor();
139 pBorderWindow->GetOutDev()->SetLineColor(bFlat ? COL_WHITE : COL_BLACK);
140 pBorderWindow->GetOutDev()->DrawRect(aRect);
141 aRect.AdjustLeft( 1 );
142 aRect.AdjustRight( -1 );
143 aRect.AdjustTop( 1 );
144 aRect.AdjustBottom( -1 );
145 pBorderWindow->GetOutDev()->SetLineColor(bFlat ? COL_BLACK : COL_WHITE);
146 pBorderWindow->GetOutDev()->DrawRect(aRect);
147 pBorderWindow->GetOutDev()->SetLineColor(oldLineCol);
148 pBorderWindow->GetOutDev()->SetFillColor(oldFillCol);
149
150}
151
152void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, SystemTextColorFlags )
153{
154 const Point aPos = pDev->LogicToPixel( rPos );
155 const Size aSize = GetSizePixel();
156 tools::Rectangle aRect( aPos, aSize );
157
158 pDev->Push();
159 pDev->SetMapMode();
160
161 // Border
162 if ( GetStyle() & WB_BORDER )
163 {
164 ImplDrawFrame( pDev, aRect );
165 }
166 pDev->IntersectClipRegion( aRect );
167 ImplDraw( *pDev, aRect.TopLeft(), aRect.GetSize() );
168
169 pDev->Pop();
170}
171
172void ImageControl::GetFocus()
173{
175 GetWindow( GetWindowType::Border )->Invalidate();
176}
177
178void ImageControl::LoseFocus()
179{
181 GetWindow( GetWindowType::Border )->Invalidate();
182}
183
184/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
DrawImageFlags
DrawTextFlags
SystemTextColorFlags
static SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle(WinBits nWinBits)
Definition: fixed.cxx:106
Definition: image.hxx:40
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:170
void DrawRect(const tools::Rectangle &rRect)
Definition: rect.cxx:51
void SetLineColor()
Definition: line.cxx:37
void SetMapMode()
Definition: map.cxx:649
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void SetFillColor()
Definition: fill.cxx:29
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
Definition: map.cxx:933
const Color & GetLineColor() const
Definition: outdev.hxx:515
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
Definition: stack.cxx:33
void Pop()
Definition: stack.cxx:92
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, std::vector< tools::Rectangle > *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
Definition: text.cxx:796
void IntersectClipRegion(const tools::Rectangle &rRect)
const Color & GetFillColor() const
Definition: outdev.hxx:520
constexpr tools::Long Height() const
constexpr tools::Long Width() const
constexpr tools::Long GetWidth() const
constexpr Point TopLeft() const
constexpr Size GetSize() const
constexpr tools::Long GetHeight() const
virtual void GetFocus()
Definition: window.cxx:1843
::OutputDevice const * GetOutDev() const
Definition: window.cxx:567
Size GetOutputSizePixel() const
Definition: window3.cxx:89
long Long
sal_Int64 WinBits
Definition: wintypes.hxx:109
WinBits const WB_BORDER
Definition: wintypes.hxx:115