1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: imgctrl.cxx,v $
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
)
60 // -----------------------------------------------------------------------
62 void ImageControl::Resize()
67 // -----------------------------------------------------------------------
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;
90 // -----------------------------------------------------------------------
92 void ImageControl::UserDraw( const UserDrawEvent
& rUDEvt
)
95 BitmapEx
* pBitmap
= &maBmp
;
98 if( GetSettings().GetStyleSettings().GetHighContrastMode() )
104 String
sText( GetText() );
108 WinBits nWinStyle
= GetStyle();
109 USHORT nTextStyle
= FixedText::ImplGetTextStyle( nWinStyle
);
111 nTextStyle
|= TEXT_DRAW_DISABLE
;
113 DrawText( rUDEvt
.GetRect(), sText
, nTextStyle
);
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
);
126 switch ( mnScaleMode
)
128 case ImageScaleMode::None
:
130 rUDEvt
.GetDevice()->DrawImage(
131 lcl_centerWithin( rPaintRect
, rBitmapSize
), aImage
, nStyle
);
135 case ImageScaleMode::Isotropic
:
137 const Size aPaintSize
= lcl_calcPaintSize( rPaintRect
, rBitmapSize
);
138 rUDEvt
.GetDevice()->DrawImage(
139 lcl_centerWithin( rPaintRect
, aPaintSize
),
145 case ImageScaleMode::Anisotropic
:
147 rUDEvt
.GetDevice()->DrawImage(
148 rPaintRect
.TopLeft(),
149 rPaintRect
.GetSize(),
155 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
158 } // switch ( mnScaleMode )
163 switch ( mnScaleMode
)
165 case ImageScaleMode::None
:
167 pBitmap
->Draw( rUDEvt
.GetDevice(), lcl_centerWithin( rPaintRect
, rBitmapSize
) );
171 case ImageScaleMode::Isotropic
:
173 const Size aPaintSize
= lcl_calcPaintSize( rPaintRect
, rBitmapSize
);
174 pBitmap
->Draw( rUDEvt
.GetDevice(),
175 lcl_centerWithin( rPaintRect
, aPaintSize
),
180 case ImageScaleMode::Anisotropic
:
182 pBitmap
->Draw( rUDEvt
.GetDevice(),
183 rPaintRect
.TopLeft(),
184 rPaintRect
.GetSize() );
189 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
192 } // switch ( mnScaleMode )
196 // -----------------------------------------------------------------------
198 void ImageControl::SetBitmap( const BitmapEx
& 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
)
213 StateChanged( STATE_CHANGE_DATA
);
220 // -----------------------------------------------------------------------
222 const BitmapEx
& ImageControl::GetModeBitmap( BmpColorMode eMode
) const
224 if( eMode
== BMP_COLOR_HIGHCONTRAST
)
230 // -----------------------------------------------------------------------
232 void ImageControl::Paint( const Rectangle
& rRect
)
234 FixedImage::Paint( rRect
);
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
);
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();