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