merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / control / imgctrl.cxx
blobb354f8e944a4b3e25865f1eeddfa0073c4513356
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 if( !!maBmpHC )
98 if( GetSettings().GetStyleSettings().GetHighContrastMode() )
99 pBitmap = &maBmpHC;
102 if ( !*pBitmap )
104 String sText( GetText() );
105 if ( !sText.Len() )
106 return;
108 WinBits nWinStyle = GetStyle();
109 USHORT nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
110 if ( !IsEnabled() )
111 nTextStyle |= TEXT_DRAW_DISABLE;
113 DrawText( rUDEvt.GetRect(), sText, nTextStyle );
114 return;
117 const Rectangle& rPaintRect = rUDEvt.GetRect();
118 const Size& rBitmapSize = maBmp.GetSizePixel();
120 if( nStyle & IMAGE_DRAW_COLORTRANSFORM )
122 // only images support IMAGE_DRAW_COLORTRANSFORM
123 Image aImage( *pBitmap );
124 if ( !!aImage )
126 switch ( mnScaleMode )
128 case ImageScaleMode::None:
130 rUDEvt.GetDevice()->DrawImage(
131 lcl_centerWithin( rPaintRect, rBitmapSize ), aImage, nStyle );
133 break;
135 case ImageScaleMode::Isotropic:
137 const Size aPaintSize = lcl_calcPaintSize( rPaintRect, rBitmapSize );
138 rUDEvt.GetDevice()->DrawImage(
139 lcl_centerWithin( rPaintRect, aPaintSize ),
140 aPaintSize,
141 aImage, nStyle );
143 break;
145 case ImageScaleMode::Anisotropic:
147 rUDEvt.GetDevice()->DrawImage(
148 rPaintRect.TopLeft(),
149 rPaintRect.GetSize(),
150 aImage, nStyle );
152 break;
154 default:
155 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
156 break;
158 } // switch ( mnScaleMode )
161 else
163 switch ( mnScaleMode )
165 case ImageScaleMode::None:
167 pBitmap->Draw( rUDEvt.GetDevice(), lcl_centerWithin( rPaintRect, rBitmapSize ) );
169 break;
171 case ImageScaleMode::Isotropic:
173 const Size aPaintSize = lcl_calcPaintSize( rPaintRect, rBitmapSize );
174 pBitmap->Draw( rUDEvt.GetDevice(),
175 lcl_centerWithin( rPaintRect, aPaintSize ),
176 aPaintSize );
178 break;
180 case ImageScaleMode::Anisotropic:
182 pBitmap->Draw( rUDEvt.GetDevice(),
183 rPaintRect.TopLeft(),
184 rPaintRect.GetSize() );
186 break;
188 default:
189 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
190 break;
192 } // switch ( mnScaleMode )
196 // -----------------------------------------------------------------------
198 void ImageControl::SetBitmap( const BitmapEx& rBmp )
200 maBmp = rBmp;
201 StateChanged( STATE_CHANGE_DATA );
204 // -----------------------------------------------------------------------
206 BOOL ImageControl::SetModeBitmap( const BitmapEx& rBitmap, BmpColorMode eMode )
208 if( eMode == BMP_COLOR_NORMAL )
209 SetBitmap( rBitmap );
210 else if( eMode == BMP_COLOR_HIGHCONTRAST )
212 maBmpHC = rBitmap;
213 StateChanged( STATE_CHANGE_DATA );
215 else
216 return FALSE;
217 return TRUE;
220 // -----------------------------------------------------------------------
222 const BitmapEx& ImageControl::GetModeBitmap( BmpColorMode eMode ) const
224 if( eMode == BMP_COLOR_HIGHCONTRAST )
225 return maBmpHC;
226 else
227 return maBmp;
230 // -----------------------------------------------------------------------
232 void ImageControl::Paint( const Rectangle& rRect )
234 FixedImage::Paint( rRect );
235 if( HasFocus() )
237 Window *pWin = GetWindow( WINDOW_BORDER );
239 BOOL bFlat = (GetBorderStyle() == 2);
240 Rectangle aRect( Point(0,0), pWin->GetOutputSizePixel() );
241 Color oldLineCol = pWin->GetLineColor();
242 Color oldFillCol = pWin->GetFillColor();
243 pWin->SetFillColor();
244 pWin->SetLineColor( bFlat ? COL_WHITE : COL_BLACK );
245 pWin->DrawRect( aRect );
246 aRect.nLeft++;
247 aRect.nRight--;
248 aRect.nTop++;
249 aRect.nBottom--;
250 pWin->SetLineColor( bFlat ? COL_BLACK : COL_WHITE );
251 pWin->DrawRect( aRect );
252 pWin->SetLineColor( oldLineCol );
253 pWin->SetFillColor( oldFillCol );
257 // -----------------------------------------------------------------------
259 void ImageControl::GetFocus()
261 FixedImage::GetFocus();
262 GetWindow( WINDOW_BORDER )->Invalidate();
265 // -----------------------------------------------------------------------
267 void ImageControl::LoseFocus()
269 FixedImage::GetFocus();
270 GetWindow( WINDOW_BORDER )->Invalidate();