bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / control / imgctrl.cxx
blob3c60d92a44291994b96235a20b6e3e36dcc28afc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/toolkit/imgctrl.hxx>
22 #include <com/sun/star/awt/ImageScaleMode.hpp>
23 #include <osl/diagnose.h>
25 namespace ImageScaleMode = css::awt::ImageScaleMode;
27 ImageControl::ImageControl( vcl::Window* pParent, WinBits nStyle )
28 :FixedImage( pParent, nStyle )
29 ,mnScaleMode( ImageScaleMode::ANISOTROPIC )
33 void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
35 if ( _nMode != mnScaleMode )
37 mnScaleMode = _nMode;
38 Invalidate();
42 void ImageControl::Resize()
44 Invalidate();
47 namespace
49 Size lcl_calcPaintSize( const tools::Rectangle& _rPaintRect, const Size& _rBitmapSize )
51 const Size aPaintSize = _rPaintRect.GetSize();
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 );
57 return Size( long( _rBitmapSize.Width() * nRatioMin ), long( _rBitmapSize.Height() * nRatioMin ) );
60 Point lcl_centerWithin( const tools::Rectangle& _rArea, const Size& _rObjectSize )
62 Point aPos( _rArea.TopLeft() );
63 aPos.AdjustX(( _rArea.GetWidth() - _rObjectSize.Width() ) / 2 );
64 aPos.AdjustY(( _rArea.GetHeight() - _rObjectSize.Height() ) / 2 );
65 return aPos;
69 void ImageControl::ImplDraw(OutputDevice& rDev, const Point& rPos, const Size& rSize) const
71 DrawImageFlags nStyle = DrawImageFlags::NONE;
72 if ( !IsEnabled() )
73 nStyle |= DrawImageFlags::Disable;
75 const Image& rImage( GetModeImage() );
76 const tools::Rectangle aDrawRect( rPos, rSize );
77 if (!rImage)
79 OUString sText( GetText() );
80 if ( sText.isEmpty() )
81 return;
83 WinBits nWinStyle = GetStyle();
84 DrawTextFlags nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
85 if ( !IsEnabled() )
86 nTextStyle |= DrawTextFlags::Disable;
88 rDev.DrawText( aDrawRect, sText, nTextStyle );
89 return;
92 const Size& rBitmapSize = rImage.GetSizePixel();
94 switch ( mnScaleMode )
96 case ImageScaleMode::NONE:
98 rDev.DrawImage(lcl_centerWithin( aDrawRect, rBitmapSize ), rImage, nStyle);
100 break;
102 case ImageScaleMode::ISOTROPIC:
104 const Size aPaintSize = lcl_calcPaintSize( aDrawRect, rBitmapSize );
105 rDev.DrawImage(lcl_centerWithin(aDrawRect, aPaintSize), aPaintSize, rImage, nStyle);
107 break;
109 case ImageScaleMode::ANISOTROPIC:
111 rDev.DrawImage(
112 aDrawRect.TopLeft(),
113 aDrawRect.GetSize(),
114 rImage, nStyle );
116 break;
118 default:
119 OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
120 break;
122 } // switch ( mnScaleMode )
125 void ImageControl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
127 ImplDraw(rRenderContext, Point(), GetOutputSizePixel());
129 if (!HasFocus())
130 return;
132 vcl::Window* pBorderWindow = GetWindow(GetWindowType::Border);
134 bool bFlat = (GetBorderStyle() == WindowBorderStyle::MONO);
135 tools::Rectangle aRect(Point(0,0), pBorderWindow->GetOutputSizePixel());
136 Color oldLineCol = pBorderWindow->GetLineColor();
137 Color oldFillCol = pBorderWindow->GetFillColor();
138 pBorderWindow->SetFillColor();
139 pBorderWindow->SetLineColor(bFlat ? COL_WHITE : COL_BLACK);
140 pBorderWindow->DrawRect(aRect);
141 aRect.AdjustLeft( 1 );
142 aRect.AdjustRight( -1 );
143 aRect.AdjustTop( 1 );
144 aRect.AdjustBottom( -1 );
145 pBorderWindow->SetLineColor(bFlat ? COL_BLACK : COL_WHITE);
146 pBorderWindow->DrawRect(aRect);
147 pBorderWindow->SetLineColor(oldLineCol);
148 pBorderWindow->SetFillColor(oldFillCol);
152 void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags )
154 const Point aPos = pDev->LogicToPixel( rPos );
155 const Size aSize = pDev->LogicToPixel( rSize );
156 tools::Rectangle aRect( aPos, aSize );
158 pDev->Push();
159 pDev->SetMapMode();
161 // Border
162 if ( GetStyle() & WB_BORDER )
164 ImplDrawFrame( pDev, aRect );
166 pDev->IntersectClipRegion( aRect );
167 ImplDraw( *pDev, aRect.TopLeft(), aRect.GetSize() );
169 pDev->Pop();
172 void ImageControl::GetFocus()
174 FixedImage::GetFocus();
175 GetWindow( GetWindowType::Border )->Invalidate();
178 void ImageControl::LoseFocus()
180 FixedImage::GetFocus();
181 GetWindow( GetWindowType::Border )->Invalidate();
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */