LibreOffice Module vcl (master)  1
imgctrl.cxx
Go to the documentation of this file.
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/toolkit/imgctrl.hxx>
21 
22 #include <com/sun/star/awt/ImageScaleMode.hpp>
23 #include <osl/diagnose.h>
24 
25 namespace ImageScaleMode = css::awt::ImageScaleMode;
26 
28  :FixedImage( pParent, nStyle )
29  ,mnScaleMode( ImageScaleMode::ANISOTROPIC )
30 {
31 }
32 
33 void ImageControl::SetScaleMode( const ::sal_Int16 _nMode )
34 {
35  if ( _nMode != mnScaleMode )
36  {
37  mnScaleMode = _nMode;
38  Invalidate();
39  }
40 }
41 
43 {
44  Invalidate();
45 }
46 
47 namespace
48 {
49  Size lcl_calcPaintSize( const tools::Rectangle& _rPaintRect, const Size& _rBitmapSize )
50  {
51  const Size aPaintSize = _rPaintRect.GetSize();
52 
53  const double nRatioX = 1.0 * aPaintSize.Width() / _rBitmapSize.Width();
54  const double nRatioY = 1.0 * aPaintSize.Height() / _rBitmapSize.Height();
55  const double nRatioMin = ::std::min( nRatioX, nRatioY );
56 
57  return Size( long( _rBitmapSize.Width() * nRatioMin ), long( _rBitmapSize.Height() * nRatioMin ) );
58  }
59 
60  Point lcl_centerWithin( const tools::Rectangle& _rArea, const Size& _rObjectSize )
61  {
62  Point aPos( _rArea.TopLeft() );
63  aPos.AdjustX(( _rArea.GetWidth() - _rObjectSize.Width() ) / 2 );
64  aPos.AdjustY(( _rArea.GetHeight() - _rObjectSize.Height() ) / 2 );
65  return aPos;
66  }
67 }
68 
69 void ImageControl::ImplDraw(OutputDevice& rDev, const Point& rPos, const Size& rSize) const
70 {
72  if ( !IsEnabled() )
73  nStyle |= DrawImageFlags::Disable;
74 
75  const Image& rImage( GetModeImage() );
76  const tools::Rectangle aDrawRect( rPos, rSize );
77  if (!rImage)
78  {
79  OUString sText( GetText() );
80  if ( sText.isEmpty() )
81  return;
82 
83  WinBits nWinStyle = GetStyle();
84  DrawTextFlags nTextStyle = FixedText::ImplGetTextStyle( nWinStyle );
85  if ( !IsEnabled() )
86  nTextStyle |= DrawTextFlags::Disable;
87 
88  rDev.DrawText( aDrawRect, sText, nTextStyle );
89  return;
90  }
91 
92  const Size& rBitmapSize = rImage.GetSizePixel();
93 
94  switch ( mnScaleMode )
95  {
96  case ImageScaleMode::NONE:
97  {
98  rDev.DrawImage(lcl_centerWithin( aDrawRect, rBitmapSize ), rImage, nStyle);
99  }
100  break;
101 
102  case ImageScaleMode::ISOTROPIC:
103  {
104  const Size aPaintSize = lcl_calcPaintSize( aDrawRect, rBitmapSize );
105  rDev.DrawImage(lcl_centerWithin(aDrawRect, aPaintSize), aPaintSize, rImage, nStyle);
106  }
107  break;
108 
109  case ImageScaleMode::ANISOTROPIC:
110  {
111  rDev.DrawImage(
112  aDrawRect.TopLeft(),
113  aDrawRect.GetSize(),
114  rImage, nStyle );
115  }
116  break;
117 
118  default:
119  OSL_ENSURE( false, "ImageControl::ImplDraw: unhandled scale mode!" );
120  break;
121 
122  } // switch ( mnScaleMode )
123 }
124 
125 void ImageControl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
126 {
127  ImplDraw(rRenderContext, Point(), GetOutputSizePixel());
128 
129  if (!HasFocus())
130  return;
131 
132  vcl::Window* pBorderWindow = GetWindow(GetWindowType::Border);
133 
134  bool bFlat = (GetBorderStyle() == WindowBorderStyle::MONO);
135  tools::Rectangle aRect(Point(0,0), pBorderWindow->GetOutputSizePixel());
136  Color oldLineCol = pBorderWindow->GetLineColor();
137  Color oldFillCol = pBorderWindow->GetFillColor();
138  pBorderWindow->SetFillColor();
139  pBorderWindow->SetLineColor(bFlat ? COL_WHITE : COL_BLACK);
140  pBorderWindow->DrawRect(aRect);
141  aRect.AdjustLeft( 1 );
142  aRect.AdjustRight( -1 );
143  aRect.AdjustTop( 1 );
144  aRect.AdjustBottom( -1 );
145  pBorderWindow->SetLineColor(bFlat ? COL_BLACK : COL_WHITE);
146  pBorderWindow->DrawRect(aRect);
147  pBorderWindow->SetLineColor(oldLineCol);
148  pBorderWindow->SetFillColor(oldFillCol);
149 
150 }
151 
152 void ImageControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags )
153 {
154  const Point aPos = pDev->LogicToPixel( rPos );
155  const Size aSize = pDev->LogicToPixel( rSize );
156  tools::Rectangle aRect( aPos, aSize );
157 
158  pDev->Push();
159  pDev->SetMapMode();
160 
161  // Border
162  if ( GetStyle() & WB_BORDER )
163  {
164  ImplDrawFrame( pDev, aRect );
165  }
166  pDev->IntersectClipRegion( aRect );
167  ImplDraw( *pDev, aRect.TopLeft(), aRect.GetSize() );
168 
169  pDev->Pop();
170 }
171 
173 {
176 }
177 
179 {
182 }
183 
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetScaleMode(const ::sal_Int16 _nMode)
Definition: imgctrl.cxx:33
virtual void Draw(OutputDevice *pDev, const Point &rPos, const Size &rSize, DrawFlags nFlags) override
Definition: imgctrl.cxx:152
Point TopLeft() const
long Width() const
Size GetSizePixel() const
Definition: Image.cxx:86
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, MetricVector *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
Definition: text.cxx:780
long GetWidth() const
long GetHeight() const
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
This is an overloaded member function, provided for convenience. It differs from the above function o...
long AdjustX(long nHorzMove)
long Height() const
SAL_DLLPRIVATE void ImplDrawFrame(OutputDevice *pDev, tools::Rectangle &rRect)
draws a frame around the give rectangle, onto the given device
Definition: ctrl.cxx:336
void IntersectClipRegion(const tools::Rectangle &rRect)
void SetMapMode()
Definition: map.cxx:655
const Image & GetModeImage() const
Definition: fixed.hxx:174
sal_Int64 WinBits
WindowBorderStyle GetBorderStyle() const
Definition: window.cxx:1997
virtual OUString GetText() const
Definition: window.cxx:3059
virtual void Resize() override
Definition: imgctrl.cxx:42
void DrawRect(const tools::Rectangle &rRect)
Definition: rect.cxx:32
void SetLineColor()
DrawFlags
Definition: window.hxx:354
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1156
void SetFillColor()
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:304
const Color & GetLineColor() const
Definition: outdev.hxx:618
virtual void LoseFocus() override
Definition: imgctrl.cxx:178
void ImplDraw(OutputDevice &rDev, const Point &rPos, const Size &rSize) const
Definition: imgctrl.cxx:69
Size GetOutputSizePixel() const
Definition: outdev.hxx:441
DrawTextFlags
Definition: outdev.hxx:144
Size GetSize() const
virtual void GetFocus() override
Definition: imgctrl.cxx:172
vcl::Window * GetWindow(GetWindowType nType) const
Definition: stacking.cxx:1035
Point LogicToPixel(const Point &rLogicPt) const
Definition: map.cxx:940
::sal_Int16 mnScaleMode
Definition: imgctrl.hxx:33
ImageControl(vcl::Window *pParent, WinBits nStyle)
Definition: imgctrl.cxx:27
static SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle(WinBits nWinBits)
Definition: fixed.cxx:103
virtual void GetFocus()
Definition: window.cxx:1804
WinBits const WB_BORDER
Definition: image.hxx:40
constexpr::Color COL_WHITE(0xFF, 0xFF, 0xFF)
bool IsEnabled() const
Definition: window2.cxx:1111
WinBits GetStyle() const
Definition: window2.cxx:942
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
Definition: imgctrl.cxx:125
bool HasFocus() const
Definition: window.cxx:2985
void Push(PushFlags nFlags=PushFlags::ALL)
Definition: outdevstate.cxx:60
DrawImageFlags
Definition: outdev.hxx:172
const Color & GetFillColor() const
Definition: outdev.hxx:623