update dev300-m58
[ooovba.git] / vcl / source / control / imgctrl.cxx
blobfa4ce94daafc33fb514a45834df389cceb1d95ad
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: imgctrl.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include <vcl/event.hxx>
35 #include <vcl/imgctrl.hxx>
37 #include <com/sun/star/awt/ImageScaleMode.hdl>
39 namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
41 // -----------------------------------------------------------------------
43 ImageControl::ImageControl( Window* pParent, WinBits nStyle ) :
44 FixedImage( pParent, nStyle )
46 mnScaleMode = ImageScaleMode::Anisotropic;
49 // -----------------------------------------------------------------------
51 void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
53 if ( _nMode != mnScaleMode )
55 mnScaleMode = _nMode;
56 Invalidate();
60 // -----------------------------------------------------------------------
62 void ImageControl::Resize()
64 Invalidate();
67 // -----------------------------------------------------------------------
68 namespace
70 static Size lcl_calcPaintSize( const Rectangle& _rPaintRect, const Size& _rBitmapSize )
72 const Size aPaintSize = _rPaintRect.GetSize();
74 const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
75 const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
76 const double nRatioMin = ::std::min( nRatioX, nRatioY );
78 return Size( long( _rBitmapSize.Width() * nRatioMin ), long( _rBitmapSize.Height() * nRatioMin ) );
81 static Point lcl_centerWithin( const Rectangle& _rArea, const Size& _rObjectSize )
83 Point aPos( _rArea.TopLeft() );
84 aPos.X() += ( _rArea.GetWidth() - _rObjectSize.Width() ) / 2;
85 aPos.Y() += ( _rArea.GetHeight() - _rObjectSize.Height() ) / 2;
86 return aPos;
90 // -----------------------------------------------------------------------
92 void ImageControl::UserDraw( const UserDrawEvent& rUDEvt )
94 USHORT nStyle = 0;
95 BitmapEx* pBitmap = &maBmp;
96 Color aCol;
97 if( !!maBmpHC && ImplGetCurrentBackgroundColor( aCol ) )
99 if( aCol.IsDark() )
100 pBitmap = &maBmpHC;
101 // #99902 no col transform required
102 //if( aCol.IsBright() )
103 // nStyle |= IMAGE_DRAW_COLORTRANSFORM;
106 if ( !*pBitmap )
108 String sText( GetText() );
109 if ( !sText.Len() )
110 return;
112 WinBits nWinStyle = GetStyle();
113 USHORT nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
114 if ( !IsEnabled() )
115 nTextStyle |= TEXT_DRAW_DISABLE;
117 DrawText( rUDEvt.GetRect(), sText, nTextStyle );
118 return;
121 const Rectangle& rPaintRect = rUDEvt.GetRect();
122 const Size& rBitmapSize = maBmp.GetSizePixel();
124 if( nStyle & IMAGE_DRAW_COLORTRANSFORM )
126 // only images support IMAGE_DRAW_COLORTRANSFORM
127 Image aImage( *pBitmap );
128 if ( !!aImage )
130 switch ( mnScaleMode )
132 case ImageScaleMode::None:
134 rUDEvt.GetDevice()->DrawImage(
135 lcl_centerWithin( rPaintRect, rBitmapSize ), aImage, nStyle );
137 break;
139 case ImageScaleMode::Isotropic:
141 const Size aPaintSize = lcl_calcPaintSize( rPaintRect, rBitmapSize );
142 rUDEvt.GetDevice()->DrawImage(
143 lcl_centerWithin( rPaintRect, aPaintSize ),
144 aPaintSize,
145 aImage, nStyle );
147 break;
149 case ImageScaleMode::Anisotropic:
151 rUDEvt.GetDevice()->DrawImage(
152 rPaintRect.TopLeft(),
153 rPaintRect.GetSize(),
154 aImage, nStyle );
156 break;
158 default:
159 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
160 break;
162 } // switch ( mnScaleMode )
165 else
167 switch ( mnScaleMode )
169 case ImageScaleMode::None:
171 pBitmap->Draw( rUDEvt.GetDevice(), lcl_centerWithin( rPaintRect, rBitmapSize ) );
173 break;
175 case ImageScaleMode::Isotropic:
177 const Size aPaintSize = lcl_calcPaintSize( rPaintRect, rBitmapSize );
178 pBitmap->Draw( rUDEvt.GetDevice(),
179 lcl_centerWithin( rPaintRect, aPaintSize ),
180 aPaintSize );
182 break;
184 case ImageScaleMode::Anisotropic:
186 pBitmap->Draw( rUDEvt.GetDevice(),
187 rPaintRect.TopLeft(),
188 rPaintRect.GetSize() );
190 break;
192 default:
193 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
194 break;
196 } // switch ( mnScaleMode )
200 // -----------------------------------------------------------------------
202 void ImageControl::SetBitmap( const BitmapEx& rBmp )
204 maBmp = rBmp;
205 StateChanged( STATE_CHANGE_DATA );
208 // -----------------------------------------------------------------------
210 BOOL ImageControl::SetModeBitmap( const BitmapEx& rBitmap, BmpColorMode eMode )
212 if( eMode == BMP_COLOR_NORMAL )
213 SetBitmap( rBitmap );
214 else if( eMode == BMP_COLOR_HIGHCONTRAST )
216 maBmpHC = rBitmap;
217 StateChanged( STATE_CHANGE_DATA );
219 else
220 return FALSE;
221 return TRUE;
224 // -----------------------------------------------------------------------
226 const BitmapEx& ImageControl::GetModeBitmap( BmpColorMode eMode ) const
228 if( eMode == BMP_COLOR_HIGHCONTRAST )
229 return maBmpHC;
230 else
231 return maBmp;
234 // -----------------------------------------------------------------------
236 void ImageControl::Paint( const Rectangle& rRect )
238 FixedImage::Paint( rRect );
239 if( HasFocus() )
241 Window *pWin = GetWindow( WINDOW_BORDER );
243 BOOL bFlat = (GetBorderStyle() == 2);
244 Rectangle aRect( Point(0,0), pWin->GetOutputSizePixel() );
245 Color oldLineCol = pWin->GetLineColor();
246 Color oldFillCol = pWin->GetFillColor();
247 pWin->SetFillColor();
248 pWin->SetLineColor( bFlat ? COL_WHITE : COL_BLACK );
249 pWin->DrawRect( aRect );
250 aRect.nLeft++;
251 aRect.nRight--;
252 aRect.nTop++;
253 aRect.nBottom--;
254 pWin->SetLineColor( bFlat ? COL_BLACK : COL_WHITE );
255 pWin->DrawRect( aRect );
256 pWin->SetLineColor( oldLineCol );
257 pWin->SetFillColor( oldFillCol );
261 // -----------------------------------------------------------------------
263 void ImageControl::GetFocus()
265 FixedImage::GetFocus();
266 GetWindow( WINDOW_BORDER )->Invalidate();
269 // -----------------------------------------------------------------------
271 void ImageControl::LoseFocus()
273 FixedImage::GetFocus();
274 GetWindow( WINDOW_BORDER )->Invalidate();