xref: /trunk/main/vcl/source/control/imgctrl.cxx (revision b6dc695e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <vcl/event.hxx>
28 #include <vcl/imgctrl.hxx>
29 #include <tools/rcid.h>
30 
31 #include <com/sun/star/awt/ImageScaleMode.hdl>
32 
33 namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
34 
35 // -----------------------------------------------------------------------
36 
ImageControl(Window * pParent,WinBits nStyle)37 ImageControl::ImageControl( Window* pParent, WinBits nStyle )
38     :FixedImage( pParent, nStyle )
39     ,mnScaleMode( ImageScaleMode::ANISOTROPIC )
40 {
41 }
42 
43 // -----------------------------------------------------------------------
44 
ImageControl(Window * pParent,const ResId & rResId)45 ImageControl::ImageControl( Window* pParent, const ResId& rResId )
46     :FixedImage( pParent, rResId )
47     ,mnScaleMode( ImageScaleMode::ANISOTROPIC )
48 {
49 }
50 
51 // -----------------------------------------------------------------------
52 
SetScaleMode(const::sal_Int16 _nMode)53 void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
54 {
55     if ( _nMode != mnScaleMode )
56     {
57         mnScaleMode = _nMode;
58         Invalidate();
59     }
60 }
61 
62 // -----------------------------------------------------------------------
63 
Resize()64 void ImageControl::Resize()
65 {
66     Invalidate();
67 }
68 
69 // -----------------------------------------------------------------------
70 namespace
71 {
lcl_calcPaintSize(const Rectangle & _rPaintRect,const Size & _rBitmapSize)72     static Size lcl_calcPaintSize( const Rectangle& _rPaintRect, const Size& _rBitmapSize )
73     {
74         const Size aPaintSize = _rPaintRect.GetSize();
75 
76         const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
77         const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
78         const double nRatioMin = ::std::min( nRatioX, nRatioY );
79 
80         return Size( long( _rBitmapSize.Width() * nRatioMin ), long( _rBitmapSize.Height() * nRatioMin ) );
81     }
82 
lcl_centerWithin(const Rectangle & _rArea,const Size & _rObjectSize)83     static Point lcl_centerWithin( const Rectangle& _rArea, const Size& _rObjectSize )
84     {
85         Point aPos( _rArea.TopLeft() );
86         aPos.X() += ( _rArea.GetWidth() - _rObjectSize.Width() ) / 2;
87         aPos.Y() += ( _rArea.GetHeight() - _rObjectSize.Height() ) / 2;
88         return aPos;
89     }
90 }
91 
92 // -----------------------------------------------------------------------
93 
ImplDraw(OutputDevice & rDev,sal_uLong nDrawFlags,const Point & rPos,const Size & rSize) const94 void ImageControl::ImplDraw( OutputDevice& rDev, sal_uLong nDrawFlags, const Point& rPos, const Size& rSize ) const
95 {
96 	sal_uInt16 nStyle = 0;
97 	if ( !(nDrawFlags & WINDOW_DRAW_NODISABLE) )
98 	{
99 		if ( !IsEnabled() )
100 			nStyle |= IMAGE_DRAW_DISABLE;
101 	}
102 
103     const Image& rImage( GetModeImage( BMP_COLOR_NORMAL ) );
104     const Image& rImageHC( GetModeImage( BMP_COLOR_HIGHCONTRAST ) );
105 
106     const Image* pImage = &rImage;
107     if ( !!rImageHC && GetSettings().GetStyleSettings().GetHighContrastMode() )
108 		pImage = &rImageHC;
109 
110     const Rectangle aDrawRect( rPos, rSize );
111     if ( !*pImage )
112     {
113         String  sText( GetText() );
114         if ( !sText.Len() )
115             return;
116 
117         WinBits nWinStyle = GetStyle();
118 	    sal_uInt16 nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
119 	    if ( !(nDrawFlags & WINDOW_DRAW_NODISABLE) )
120 		    if ( !IsEnabled() )
121 			    nTextStyle |= TEXT_DRAW_DISABLE;
122 
123         rDev.DrawText( aDrawRect, sText, nTextStyle );
124         return;
125     }
126 
127     const Size&      rBitmapSize = pImage->GetSizePixel();
128 
129     switch ( mnScaleMode )
130     {
131     case ImageScaleMode::NONE:
132     {
133 	    rDev.DrawImage( lcl_centerWithin( aDrawRect, rBitmapSize ), *pImage, nStyle );
134     }
135     break;
136 
137     case ImageScaleMode::ISOTROPIC:
138     {
139         const Size aPaintSize = lcl_calcPaintSize( aDrawRect, rBitmapSize );
140 	    rDev.DrawImage(
141             lcl_centerWithin( aDrawRect, aPaintSize ),
142             aPaintSize,
143             *pImage, nStyle );
144     }
145     break;
146 
147     case ImageScaleMode::ANISOTROPIC:
148     {
149 	    rDev.DrawImage(
150             aDrawRect.TopLeft(),
151             aDrawRect.GetSize(),
152             *pImage, nStyle );
153     }
154     break;
155 
156     default:
157         OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
158         break;
159 
160     }   // switch ( mnScaleMode )
161 }
162 
163 // -----------------------------------------------------------------------
164 
Paint(const Rectangle &)165 void ImageControl::Paint( const Rectangle& /*rRect*/ )
166 {
167 	ImplDraw( *this, 0, Point(), GetOutputSizePixel() );
168 
169     if( HasFocus() )
170     {
171         Window *pWin = GetWindow( WINDOW_BORDER );
172 
173         sal_Bool bFlat = (GetBorderStyle() == 2);
174         Rectangle aRect( Point(0,0), pWin->GetOutputSizePixel() );
175         Color oldLineCol = pWin->GetLineColor();
176         Color oldFillCol = pWin->GetFillColor();
177         pWin->SetFillColor();
178         pWin->SetLineColor( bFlat ? COL_WHITE : COL_BLACK );
179         pWin->DrawRect( aRect );
180         aRect.nLeft++;
181         aRect.nRight--;
182         aRect.nTop++;
183         aRect.nBottom--;
184         pWin->SetLineColor( bFlat ? COL_BLACK : COL_WHITE );
185         pWin->DrawRect( aRect );
186         pWin->SetLineColor( oldLineCol );
187         pWin->SetFillColor( oldFillCol );
188     }
189 }
190 
191 // -----------------------------------------------------------------------
Draw(OutputDevice * pDev,const Point & rPos,const Size & rSize,sal_uLong nFlags)192 void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, sal_uLong nFlags )
193 {
194     const Point     aPos  = pDev->LogicToPixel( rPos );
195     const Size      aSize = pDev->LogicToPixel( rSize );
196           Rectangle aRect( aPos, aSize );
197 
198 	pDev->Push();
199 	pDev->SetMapMode();
200 
201 	// Border
202 	if ( !(nFlags & WINDOW_DRAW_NOBORDER) && (GetStyle() & WB_BORDER) )
203 	{
204 		ImplDrawFrame( pDev, aRect );
205 	}
206 	pDev->IntersectClipRegion( aRect );
207 	ImplDraw( *pDev, nFlags, aRect.TopLeft(), aRect.GetSize() );
208 
209 	pDev->Pop();
210 }
211 
212 // -----------------------------------------------------------------------
213 
GetFocus()214 void ImageControl::GetFocus()
215 {
216     FixedImage::GetFocus();
217     GetWindow( WINDOW_BORDER )->Invalidate();
218 }
219 
220 // -----------------------------------------------------------------------
221 
LoseFocus()222 void ImageControl::LoseFocus()
223 {
224     FixedImage::GetFocus();
225     GetWindow( WINDOW_BORDER )->Invalidate();
226 }
227 
228