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
;
97 if( !!maBmpHC
&& ImplGetCurrentBackgroundColor( aCol
) )
101 // #99902 no col transform required
102 //if( aCol.IsBright() )
103 // nStyle |= IMAGE_DRAW_COLORTRANSFORM;
108 String
sText( GetText() );
112 WinBits nWinStyle
= GetStyle();
113 USHORT nTextStyle
= FixedText::ImplGetTextStyle( nWinStyle
);
115 nTextStyle
|= TEXT_DRAW_DISABLE
;
117 DrawText( rUDEvt
.GetRect(), sText
, nTextStyle
);
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
);
130 switch ( mnScaleMode
)
132 case ImageScaleMode::None
:
134 rUDEvt
.GetDevice()->DrawImage(
135 lcl_centerWithin( rPaintRect
, rBitmapSize
), aImage
, nStyle
);
139 case ImageScaleMode::Isotropic
:
141 const Size aPaintSize
= lcl_calcPaintSize( rPaintRect
, rBitmapSize
);
142 rUDEvt
.GetDevice()->DrawImage(
143 lcl_centerWithin( rPaintRect
, aPaintSize
),
149 case ImageScaleMode::Anisotropic
:
151 rUDEvt
.GetDevice()->DrawImage(
152 rPaintRect
.TopLeft(),
153 rPaintRect
.GetSize(),
159 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
162 } // switch ( mnScaleMode )
167 switch ( mnScaleMode
)
169 case ImageScaleMode::None
:
171 pBitmap
->Draw( rUDEvt
.GetDevice(), lcl_centerWithin( rPaintRect
, rBitmapSize
) );
175 case ImageScaleMode::Isotropic
:
177 const Size aPaintSize
= lcl_calcPaintSize( rPaintRect
, rBitmapSize
);
178 pBitmap
->Draw( rUDEvt
.GetDevice(),
179 lcl_centerWithin( rPaintRect
, aPaintSize
),
184 case ImageScaleMode::Anisotropic
:
186 pBitmap
->Draw( rUDEvt
.GetDevice(),
187 rPaintRect
.TopLeft(),
188 rPaintRect
.GetSize() );
193 OSL_ENSURE( false, "ImageControl::UserDraw: unhandled scale mode!" );
196 } // switch ( mnScaleMode )
200 // -----------------------------------------------------------------------
202 void ImageControl::SetBitmap( const BitmapEx
& 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
)
217 StateChanged( STATE_CHANGE_DATA
);
224 // -----------------------------------------------------------------------
226 const BitmapEx
& ImageControl::GetModeBitmap( BmpColorMode eMode
) const
228 if( eMode
== BMP_COLOR_HIGHCONTRAST
)
234 // -----------------------------------------------------------------------
236 void ImageControl::Paint( const Rectangle
& rRect
)
238 FixedImage::Paint( rRect
);
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
);
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();