LCOV - code coverage report
Current view: top level - vcl/source/control - imgctrl.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 32 91 35.2 %
Date: 2015-06-13 12:38:46 Functions: 7 12 58.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <vcl/event.hxx>
      21             : #include <vcl/imgctrl.hxx>
      22             : #include <tools/rcid.h>
      23             : 
      24             : #include <com/sun/star/awt/ImageScaleMode.hpp>
      25             : 
      26             : namespace ImageScaleMode = css::awt::ImageScaleMode;
      27             : 
      28          11 : ImageControl::ImageControl( vcl::Window* pParent, WinBits nStyle )
      29             :     :FixedImage( pParent, nStyle )
      30          11 :     ,mnScaleMode( ImageScaleMode::ANISOTROPIC )
      31             : {
      32          11 : }
      33             : 
      34          55 : void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
      35             : {
      36          55 :     if ( _nMode != mnScaleMode )
      37             :     {
      38          24 :         mnScaleMode = _nMode;
      39          24 :         Invalidate();
      40             :     }
      41          55 : }
      42             : 
      43          11 : void ImageControl::Resize()
      44             : {
      45          11 :     Invalidate();
      46          11 : }
      47             : 
      48             : namespace
      49             : {
      50           0 :     static Size lcl_calcPaintSize( const Rectangle& _rPaintRect, const Size& _rBitmapSize )
      51             :     {
      52           0 :         const Size aPaintSize = _rPaintRect.GetSize();
      53             : 
      54           0 :         const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
      55           0 :         const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
      56           0 :         const double nRatioMin = ::std::min( nRatioX, nRatioY );
      57             : 
      58           0 :         return Size( long( _rBitmapSize.Width() * nRatioMin ), long( _rBitmapSize.Height() * nRatioMin ) );
      59             :     }
      60             : 
      61           0 :     static Point lcl_centerWithin( const Rectangle& _rArea, const Size& _rObjectSize )
      62             :     {
      63           0 :         Point aPos( _rArea.TopLeft() );
      64           0 :         aPos.X() += ( _rArea.GetWidth() - _rObjectSize.Width() ) / 2;
      65           0 :         aPos.Y() += ( _rArea.GetHeight() - _rObjectSize.Height() ) / 2;
      66           0 :         return aPos;
      67             :     }
      68             : }
      69             : 
      70         307 : void ImageControl::ImplDraw(OutputDevice& rDev, DrawFlags nDrawFlags, const Point& rPos, const Size& rSize) const
      71             : {
      72         307 :     DrawImageFlags nStyle = DrawImageFlags::NONE;
      73         307 :     if ( !(nDrawFlags & DrawFlags::NoDisable) )
      74             :     {
      75         307 :         if ( !IsEnabled() )
      76          60 :             nStyle |= DrawImageFlags::Disable;
      77             :     }
      78             : 
      79         307 :     const Image& rImage( GetModeImage() );
      80         307 :     const Image* pImage = &rImage;
      81         307 :     const Rectangle aDrawRect( rPos, rSize );
      82         307 :     if ( !*pImage )
      83             :     {
      84         304 :         OUString  sText( GetText() );
      85         304 :         if ( sText.isEmpty() )
      86         304 :             return;
      87             : 
      88           0 :         WinBits nWinStyle = GetStyle();
      89           0 :         DrawTextFlags nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
      90           0 :         if ( !(nDrawFlags & DrawFlags::NoDisable) )
      91           0 :             if ( !IsEnabled() )
      92           0 :                 nTextStyle |= DrawTextFlags::Disable;
      93             : 
      94           0 :         rDev.DrawText( aDrawRect, sText, nTextStyle );
      95           0 :         return;
      96             :     }
      97             : 
      98           3 :     const Size&      rBitmapSize = pImage->GetSizePixel();
      99             : 
     100           3 :     switch ( mnScaleMode )
     101             :     {
     102             :     case ImageScaleMode::NONE:
     103             :     {
     104           0 :         rDev.DrawImage( lcl_centerWithin( aDrawRect, rBitmapSize ), *pImage, nStyle );
     105             :     }
     106           0 :     break;
     107             : 
     108             :     case ImageScaleMode::ISOTROPIC:
     109             :     {
     110           0 :         const Size aPaintSize = lcl_calcPaintSize( aDrawRect, rBitmapSize );
     111             :         rDev.DrawImage(
     112             :             lcl_centerWithin( aDrawRect, aPaintSize ),
     113             :             aPaintSize,
     114           0 :             *pImage, nStyle );
     115             :     }
     116           0 :     break;
     117             : 
     118             :     case ImageScaleMode::ANISOTROPIC:
     119             :     {
     120             :         rDev.DrawImage(
     121             :             aDrawRect.TopLeft(),
     122             :             aDrawRect.GetSize(),
     123           3 :             *pImage, nStyle );
     124             :     }
     125           3 :     break;
     126             : 
     127             :     default:
     128             :         OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
     129           0 :         break;
     130             : 
     131             :     }   // switch ( mnScaleMode )
     132             : }
     133             : 
     134         307 : void ImageControl::Paint(vcl::RenderContext& rRenderContext, const Rectangle& /*rRect*/)
     135             : {
     136         307 :     ImplDraw(rRenderContext, DrawFlags::NONE, Point(), GetOutputSizePixel());
     137             : 
     138         307 :     if (HasFocus())
     139             :     {
     140           0 :         vcl::Window* pBorderWindow = GetWindow(GetWindowType::Border);
     141             : 
     142           0 :         bool bFlat = (GetBorderStyle() == WindowBorderStyle::MONO);
     143           0 :         Rectangle aRect(Point(0,0), pBorderWindow->GetOutputSizePixel());
     144           0 :         Color oldLineCol = pBorderWindow->GetLineColor();
     145           0 :         Color oldFillCol = pBorderWindow->GetFillColor();
     146           0 :         pBorderWindow->SetFillColor();
     147           0 :         pBorderWindow->SetLineColor(bFlat ? COL_WHITE : COL_BLACK);
     148           0 :         pBorderWindow->DrawRect(aRect);
     149           0 :         ++aRect.Left();
     150           0 :         --aRect.Right();
     151           0 :         ++aRect.Top();
     152           0 :         --aRect.Bottom();
     153           0 :         pBorderWindow->SetLineColor(bFlat ? COL_BLACK : COL_WHITE);
     154           0 :         pBorderWindow->DrawRect(aRect);
     155           0 :         pBorderWindow->SetLineColor(oldLineCol);
     156           0 :         pBorderWindow->SetFillColor(oldFillCol);
     157             :     }
     158         307 : }
     159             : 
     160           0 : void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags )
     161             : {
     162           0 :     const Point     aPos  = pDev->LogicToPixel( rPos );
     163           0 :     const Size      aSize = pDev->LogicToPixel( rSize );
     164           0 :           Rectangle aRect( aPos, aSize );
     165             : 
     166           0 :     pDev->Push();
     167           0 :     pDev->SetMapMode();
     168             : 
     169             :     // Border
     170           0 :     if ( !(nFlags & DrawFlags::NoBorder) && (GetStyle() & WB_BORDER) )
     171             :     {
     172           0 :         ImplDrawFrame( pDev, aRect );
     173             :     }
     174           0 :     pDev->IntersectClipRegion( aRect );
     175           0 :     ImplDraw( *pDev, nFlags, aRect.TopLeft(), aRect.GetSize() );
     176             : 
     177           0 :     pDev->Pop();
     178           0 : }
     179             : 
     180           0 : void ImageControl::GetFocus()
     181             : {
     182           0 :     FixedImage::GetFocus();
     183           0 :     GetWindow( GetWindowType::Border )->Invalidate();
     184           0 : }
     185             : 
     186           0 : void ImageControl::LoseFocus()
     187             : {
     188           0 :     FixedImage::GetFocus();
     189           0 :     GetWindow( GetWindowType::Border )->Invalidate();
     190         801 : }
     191             : 
     192             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11