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