update credits
[LibreOffice.git] / vcl / source / control / imgctrl.cxx
blob40c5d23577770989d8928ee2e544e960dcddfc36
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 .
21 #include <vcl/event.hxx>
22 #include <vcl/imgctrl.hxx>
23 #include <tools/rcid.h>
25 #include <com/sun/star/awt/ImageScaleMode.hpp>
27 namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
29 // -----------------------------------------------------------------------
31 ImageControl::ImageControl( Window* pParent, WinBits nStyle )
32 :FixedImage( pParent, nStyle )
33 ,mnScaleMode( ImageScaleMode::Anisotropic )
37 // -----------------------------------------------------------------------
39 ImageControl::ImageControl( Window* pParent, const ResId& rResId )
40 :FixedImage( pParent, rResId )
41 ,mnScaleMode( ImageScaleMode::Anisotropic )
45 // -----------------------------------------------------------------------
47 void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
49 if ( _nMode != mnScaleMode )
51 mnScaleMode = _nMode;
52 Invalidate();
56 // -----------------------------------------------------------------------
58 void ImageControl::Resize()
60 Invalidate();
63 // -----------------------------------------------------------------------
64 namespace
66 static Size lcl_calcPaintSize( const Rectangle& _rPaintRect, const Size& _rBitmapSize )
68 const Size aPaintSize = _rPaintRect.GetSize();
70 const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
71 const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
72 const double nRatioMin = ::std::min( nRatioX, nRatioY );
74 return Size( long( _rBitmapSize.Width() * nRatioMin ), long( _rBitmapSize.Height() * nRatioMin ) );
77 static Point lcl_centerWithin( const Rectangle& _rArea, const Size& _rObjectSize )
79 Point aPos( _rArea.TopLeft() );
80 aPos.X() += ( _rArea.GetWidth() - _rObjectSize.Width() ) / 2;
81 aPos.Y() += ( _rArea.GetHeight() - _rObjectSize.Height() ) / 2;
82 return aPos;
86 // -----------------------------------------------------------------------
88 void ImageControl::ImplDraw( OutputDevice& rDev, sal_uLong nDrawFlags, const Point& rPos, const Size& rSize ) const
90 sal_uInt16 nStyle = 0;
91 if ( !(nDrawFlags & WINDOW_DRAW_NODISABLE) )
93 if ( !IsEnabled() )
94 nStyle |= IMAGE_DRAW_DISABLE;
97 const Image& rImage( GetModeImage() );
98 const Image* pImage = &rImage;
99 const Rectangle aDrawRect( rPos, rSize );
100 if ( !*pImage )
102 String sText( GetText() );
103 if ( !sText.Len() )
104 return;
106 WinBits nWinStyle = GetStyle();
107 sal_uInt16 nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
108 if ( !(nDrawFlags & WINDOW_DRAW_NODISABLE) )
109 if ( !IsEnabled() )
110 nTextStyle |= TEXT_DRAW_DISABLE;
112 rDev.DrawText( aDrawRect, sText, nTextStyle );
113 return;
116 const Size& rBitmapSize = pImage->GetSizePixel();
118 switch ( mnScaleMode )
120 case ImageScaleMode::None:
122 rDev.DrawImage( lcl_centerWithin( aDrawRect, rBitmapSize ), *pImage, nStyle );
124 break;
126 case ImageScaleMode::Isotropic:
128 const Size aPaintSize = lcl_calcPaintSize( aDrawRect, rBitmapSize );
129 rDev.DrawImage(
130 lcl_centerWithin( aDrawRect, aPaintSize ),
131 aPaintSize,
132 *pImage, nStyle );
134 break;
136 case ImageScaleMode::Anisotropic:
138 rDev.DrawImage(
139 aDrawRect.TopLeft(),
140 aDrawRect.GetSize(),
141 *pImage, nStyle );
143 break;
145 default:
146 OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
147 break;
149 } // switch ( mnScaleMode )
152 // -----------------------------------------------------------------------
154 void ImageControl::Paint( const Rectangle& /*rRect*/ )
156 ImplDraw( *this, 0, Point(), GetOutputSizePixel() );
158 if( HasFocus() )
160 Window *pWin = GetWindow( WINDOW_BORDER );
162 bool bFlat = (GetBorderStyle() == 2);
163 Rectangle aRect( Point(0,0), pWin->GetOutputSizePixel() );
164 Color oldLineCol = pWin->GetLineColor();
165 Color oldFillCol = pWin->GetFillColor();
166 pWin->SetFillColor();
167 pWin->SetLineColor( bFlat ? COL_WHITE : COL_BLACK );
168 pWin->DrawRect( aRect );
169 ++aRect.Left();
170 --aRect.Right();
171 ++aRect.Top();
172 --aRect.Bottom();
173 pWin->SetLineColor( bFlat ? COL_BLACK : COL_WHITE );
174 pWin->DrawRect( aRect );
175 pWin->SetLineColor( oldLineCol );
176 pWin->SetFillColor( oldFillCol );
180 // -----------------------------------------------------------------------
181 void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, sal_uLong nFlags )
183 const Point aPos = pDev->LogicToPixel( rPos );
184 const Size aSize = pDev->LogicToPixel( rSize );
185 Rectangle aRect( aPos, aSize );
187 pDev->Push();
188 pDev->SetMapMode();
190 // Border
191 if ( !(nFlags & WINDOW_DRAW_NOBORDER) && (GetStyle() & WB_BORDER) )
193 ImplDrawFrame( pDev, aRect );
195 pDev->IntersectClipRegion( aRect );
196 ImplDraw( *pDev, nFlags, aRect.TopLeft(), aRect.GetSize() );
198 pDev->Pop();
201 // -----------------------------------------------------------------------
203 void ImageControl::GetFocus()
205 FixedImage::GetFocus();
206 GetWindow( WINDOW_BORDER )->Invalidate();
209 // -----------------------------------------------------------------------
211 void ImageControl::LoseFocus()
213 FixedImage::GetFocus();
214 GetWindow( WINDOW_BORDER )->Invalidate();
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */