1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
56 // -----------------------------------------------------------------------
58 void ImageControl::Resize()
63 // -----------------------------------------------------------------------
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;
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
) )
94 nStyle
|= IMAGE_DRAW_DISABLE
;
97 const Image
& rImage( GetModeImage() );
98 const Image
* pImage
= &rImage
;
99 const Rectangle
aDrawRect( rPos
, rSize
);
102 String
sText( GetText() );
106 WinBits nWinStyle
= GetStyle();
107 sal_uInt16 nTextStyle
= FixedText::ImplGetTextStyle( nWinStyle
);
108 if ( !(nDrawFlags
& WINDOW_DRAW_NODISABLE
) )
110 nTextStyle
|= TEXT_DRAW_DISABLE
;
112 rDev
.DrawText( aDrawRect
, sText
, nTextStyle
);
116 const Size
& rBitmapSize
= pImage
->GetSizePixel();
118 switch ( mnScaleMode
)
120 case ImageScaleMode::None
:
122 rDev
.DrawImage( lcl_centerWithin( aDrawRect
, rBitmapSize
), *pImage
, nStyle
);
126 case ImageScaleMode::Isotropic
:
128 const Size aPaintSize
= lcl_calcPaintSize( aDrawRect
, rBitmapSize
);
130 lcl_centerWithin( aDrawRect
, aPaintSize
),
136 case ImageScaleMode::Anisotropic
:
146 OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
149 } // switch ( mnScaleMode )
152 // -----------------------------------------------------------------------
154 void ImageControl::Paint( const Rectangle
& /*rRect*/ )
156 ImplDraw( *this, 0, Point(), GetOutputSizePixel() );
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
);
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
);
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() );
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: */