xref: /aoo41x/main/vcl/source/control/imgctrl.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include <vcl/event.hxx>
32 #include <vcl/imgctrl.hxx>
33 #include <tools/rcid.h>
34 
35 #include <com/sun/star/awt/ImageScaleMode.hdl>
36 
37 namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
38 
39 // -----------------------------------------------------------------------
40 
41 ImageControl::ImageControl( Window* pParent, WinBits nStyle )
42     :FixedImage( pParent, nStyle )
43     ,mnScaleMode( ImageScaleMode::Anisotropic )
44 {
45 }
46 
47 // -----------------------------------------------------------------------
48 
49 ImageControl::ImageControl( Window* pParent, const ResId& rResId )
50     :FixedImage( pParent, rResId )
51     ,mnScaleMode( ImageScaleMode::Anisotropic )
52 {
53 }
54 
55 // -----------------------------------------------------------------------
56 
57 void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
58 {
59     if ( _nMode != mnScaleMode )
60     {
61         mnScaleMode = _nMode;
62         Invalidate();
63     }
64 }
65 
66 // -----------------------------------------------------------------------
67 
68 void ImageControl::Resize()
69 {
70     Invalidate();
71 }
72 
73 // -----------------------------------------------------------------------
74 namespace
75 {
76     static Size lcl_calcPaintSize( const Rectangle& _rPaintRect, const Size& _rBitmapSize )
77     {
78         const Size aPaintSize = _rPaintRect.GetSize();
79 
80         const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
81         const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
82         const double nRatioMin = ::std::min( nRatioX, nRatioY );
83 
84         return Size( long( _rBitmapSize.Width() * nRatioMin ), long( _rBitmapSize.Height() * nRatioMin ) );
85     }
86 
87     static Point lcl_centerWithin( const Rectangle& _rArea, const Size& _rObjectSize )
88     {
89         Point aPos( _rArea.TopLeft() );
90         aPos.X() += ( _rArea.GetWidth() - _rObjectSize.Width() ) / 2;
91         aPos.Y() += ( _rArea.GetHeight() - _rObjectSize.Height() ) / 2;
92         return aPos;
93     }
94 }
95 
96 // -----------------------------------------------------------------------
97 
98 void ImageControl::ImplDraw( OutputDevice& rDev, sal_uLong nDrawFlags, const Point& rPos, const Size& rSize ) const
99 {
100 	sal_uInt16 nStyle = 0;
101 	if ( !(nDrawFlags & WINDOW_DRAW_NODISABLE) )
102 	{
103 		if ( !IsEnabled() )
104 			nStyle |= IMAGE_DRAW_DISABLE;
105 	}
106 
107     const Image& rImage( GetModeImage( BMP_COLOR_NORMAL ) );
108     const Image& rImageHC( GetModeImage( BMP_COLOR_HIGHCONTRAST ) );
109 
110     const Image* pImage = &rImage;
111     if ( !!rImageHC && GetSettings().GetStyleSettings().GetHighContrastMode() )
112 		pImage = &rImageHC;
113 
114     const Rectangle aDrawRect( rPos, rSize );
115     if ( !*pImage )
116     {
117         String  sText( GetText() );
118         if ( !sText.Len() )
119             return;
120 
121         WinBits nWinStyle = GetStyle();
122 	    sal_uInt16 nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
123 	    if ( !(nDrawFlags & WINDOW_DRAW_NODISABLE) )
124 		    if ( !IsEnabled() )
125 			    nTextStyle |= TEXT_DRAW_DISABLE;
126 
127         rDev.DrawText( aDrawRect, sText, nTextStyle );
128         return;
129     }
130 
131     const Size&      rBitmapSize = pImage->GetSizePixel();
132 
133     switch ( mnScaleMode )
134     {
135     case ImageScaleMode::None:
136     {
137 	    rDev.DrawImage( lcl_centerWithin( aDrawRect, rBitmapSize ), *pImage, nStyle );
138     }
139     break;
140 
141     case ImageScaleMode::Isotropic:
142     {
143         const Size aPaintSize = lcl_calcPaintSize( aDrawRect, rBitmapSize );
144 	    rDev.DrawImage(
145             lcl_centerWithin( aDrawRect, aPaintSize ),
146             aPaintSize,
147             *pImage, nStyle );
148     }
149     break;
150 
151     case ImageScaleMode::Anisotropic:
152     {
153 	    rDev.DrawImage(
154             aDrawRect.TopLeft(),
155             aDrawRect.GetSize(),
156             *pImage, nStyle );
157     }
158     break;
159 
160     default:
161         OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
162         break;
163 
164     }   // switch ( mnScaleMode )
165 }
166 
167 // -----------------------------------------------------------------------
168 
169 void ImageControl::Paint( const Rectangle& /*rRect*/ )
170 {
171 	ImplDraw( *this, 0, Point(), GetOutputSizePixel() );
172 
173     if( HasFocus() )
174     {
175         Window *pWin = GetWindow( WINDOW_BORDER );
176 
177         sal_Bool bFlat = (GetBorderStyle() == 2);
178         Rectangle aRect( Point(0,0), pWin->GetOutputSizePixel() );
179         Color oldLineCol = pWin->GetLineColor();
180         Color oldFillCol = pWin->GetFillColor();
181         pWin->SetFillColor();
182         pWin->SetLineColor( bFlat ? COL_WHITE : COL_BLACK );
183         pWin->DrawRect( aRect );
184         aRect.nLeft++;
185         aRect.nRight--;
186         aRect.nTop++;
187         aRect.nBottom--;
188         pWin->SetLineColor( bFlat ? COL_BLACK : COL_WHITE );
189         pWin->DrawRect( aRect );
190         pWin->SetLineColor( oldLineCol );
191         pWin->SetFillColor( oldFillCol );
192     }
193 }
194 
195 // -----------------------------------------------------------------------
196 void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, sal_uLong nFlags )
197 {
198     const Point     aPos  = pDev->LogicToPixel( rPos );
199     const Size      aSize = pDev->LogicToPixel( rSize );
200           Rectangle aRect( aPos, aSize );
201 
202 	pDev->Push();
203 	pDev->SetMapMode();
204 
205 	// Border
206 	if ( !(nFlags & WINDOW_DRAW_NOBORDER) && (GetStyle() & WB_BORDER) )
207 	{
208 		ImplDrawFrame( pDev, aRect );
209 	}
210 	pDev->IntersectClipRegion( aRect );
211 	ImplDraw( *pDev, nFlags, aRect.TopLeft(), aRect.GetSize() );
212 
213 	pDev->Pop();
214 }
215 
216 // -----------------------------------------------------------------------
217 
218 void ImageControl::GetFocus()
219 {
220     FixedImage::GetFocus();
221     GetWindow( WINDOW_BORDER )->Invalidate();
222 }
223 
224 // -----------------------------------------------------------------------
225 
226 void ImageControl::LoseFocus()
227 {
228     FixedImage::GetFocus();
229     GetWindow( WINDOW_BORDER )->Invalidate();
230 }
231 
232