Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / toolkit / source / awt / vclxgraphics.cxx
blob5bb70dbbb7a7341d293af78b261de56e16bf5c8b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <memory>
22 #include <toolkit/awt/vclxgraphics.hxx>
23 #include <toolkit/awt/vclxdevice.hxx>
24 #include <toolkit/awt/vclxfont.hxx>
25 #include <toolkit/helper/macros.hxx>
26 #include <toolkit/helper/vclunohelper.hxx>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <cppuhelper/queryinterface.hxx>
29 #include <rtl/uuid.h>
31 #include <vcl/svapp.hxx>
32 #include <vcl/outdev.hxx>
33 #include <vcl/image.hxx>
34 #include <vcl/gradient.hxx>
35 #include <tools/debug.hxx>
37 using namespace com::sun::star;
40 // class VCLXGraphics
43 // uno::XInterface
44 uno::Any VCLXGraphics::queryInterface( const uno::Type & rType )
46 uno::Any aRet = ::cppu::queryInterface( rType,
47 (static_cast< css::awt::XGraphics* >(this)),
48 (static_cast< css::lang::XTypeProvider* >(this)),
49 (static_cast< css::lang::XUnoTunnel* >(this)) );
50 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
53 // lang::XUnoTunnel
54 IMPL_XUNOTUNNEL( VCLXGraphics )
56 // lang::XTypeProvider
57 IMPL_XTYPEPROVIDER_START( VCLXGraphics )
58 cppu::UnoType<awt::XGraphics>::get()
59 IMPL_XTYPEPROVIDER_END
61 VCLXGraphics::VCLXGraphics()
62 : mpOutputDevice(nullptr)
63 , meRasterOp(RasterOp::OverPaint)
64 , mpClipRegion(nullptr)
68 VCLXGraphics::~VCLXGraphics()
70 std::vector< VCLXGraphics* > *pLst = mpOutputDevice ? mpOutputDevice->GetUnoGraphicsList() : nullptr;
71 if ( pLst )
73 for( std::vector< VCLXGraphics* >::iterator it = pLst->begin(); it != pLst->end(); ++it )
75 if( *it == this ) {
76 pLst->erase( it );
77 break;
82 delete mpClipRegion;
84 SolarMutexGuard g;
85 mpOutputDevice.reset();
88 void VCLXGraphics::SetOutputDevice( OutputDevice* pOutDev )
90 mpOutputDevice = pOutDev;
91 mxDevice = nullptr;
92 initAttrs();
95 void VCLXGraphics::Init( OutputDevice* pOutDev )
97 DBG_ASSERT( !mpOutputDevice, "VCLXGraphics::Init already has pOutDev !" );
98 mpOutputDevice = pOutDev;
100 initAttrs();
101 mpClipRegion = nullptr;
103 // Register at OutputDevice
104 std::vector< VCLXGraphics* > *pLst = mpOutputDevice->GetUnoGraphicsList();
105 if ( !pLst )
106 pLst = mpOutputDevice->CreateUnoGraphicsList();
107 pLst->push_back( this );
110 void VCLXGraphics::initAttrs()
112 if ( !mpOutputDevice )
113 return;
115 maFont = mpOutputDevice->GetFont();
116 maTextColor = mpOutputDevice->GetTextColor(); /* COL_BLACK */
117 maTextFillColor = mpOutputDevice->GetTextFillColor(); /* COL_TRANSPARENT */
118 maLineColor = mpOutputDevice->GetLineColor(); /* COL_BLACK */
119 maFillColor = mpOutputDevice->GetFillColor(); /* COL_WHITE */
120 meRasterOp = mpOutputDevice->GetRasterOp(); /* RasterOp::OverPaint */
123 void VCLXGraphics::InitOutputDevice( InitOutDevFlags nFlags )
125 if(mpOutputDevice)
127 SolarMutexGuard aVclGuard;
129 if ( nFlags & InitOutDevFlags::FONT )
131 mpOutputDevice->SetFont( maFont );
132 mpOutputDevice->SetTextColor( maTextColor );
133 mpOutputDevice->SetTextFillColor( maTextFillColor );
136 if ( nFlags & InitOutDevFlags::COLORS )
138 mpOutputDevice->SetLineColor( maLineColor );
139 mpOutputDevice->SetFillColor( maFillColor );
142 mpOutputDevice->SetRasterOp( meRasterOp );
144 if( mpClipRegion )
145 mpOutputDevice->SetClipRegion( *mpClipRegion );
146 else
147 mpOutputDevice->SetClipRegion();
151 uno::Reference< awt::XDevice > VCLXGraphics::getDevice()
153 SolarMutexGuard aGuard;
155 if( !mxDevice.is() && mpOutputDevice )
157 VCLXDevice* pDev = new VCLXDevice;
158 pDev->SetOutputDevice( mpOutputDevice );
159 mxDevice = pDev;
161 return mxDevice;
164 awt::SimpleFontMetric VCLXGraphics::getFontMetric()
166 SolarMutexGuard aGuard;
168 awt::SimpleFontMetric aM;
169 if( mpOutputDevice )
171 mpOutputDevice->SetFont( maFont );
172 aM = VCLUnoHelper::CreateFontMetric( mpOutputDevice->GetFontMetric() );
174 return aM;
177 void VCLXGraphics::setFont( const uno::Reference< awt::XFont >& rxFont )
179 SolarMutexGuard aGuard;
181 maFont = VCLUnoHelper::CreateFont( rxFont );
184 void VCLXGraphics::selectFont( const awt::FontDescriptor& rDescription )
186 SolarMutexGuard aGuard;
188 maFont = VCLUnoHelper::CreateFont( rDescription, vcl::Font() );
191 void VCLXGraphics::setTextColor( sal_Int32 nColor )
193 SolarMutexGuard aGuard;
195 maTextColor = Color( (sal_uInt32)nColor );
198 void VCLXGraphics::setTextFillColor( sal_Int32 nColor )
200 SolarMutexGuard aGuard;
202 maTextFillColor = Color( (sal_uInt32)nColor );
205 void VCLXGraphics::setLineColor( sal_Int32 nColor )
207 SolarMutexGuard aGuard;
209 maLineColor = Color( (sal_uInt32)nColor );
212 void VCLXGraphics::setFillColor( sal_Int32 nColor )
214 SolarMutexGuard aGuard;
216 maFillColor = Color( (sal_uInt32)nColor );
219 void VCLXGraphics::setRasterOp( awt::RasterOperation eROP )
221 SolarMutexGuard aGuard;
223 meRasterOp = (RasterOp)eROP;
226 void VCLXGraphics::setClipRegion( const uno::Reference< awt::XRegion >& rxRegion )
228 SolarMutexGuard aGuard;
230 delete mpClipRegion;
231 if ( rxRegion.is() )
232 mpClipRegion = new vcl::Region( VCLUnoHelper::GetRegion( rxRegion ) );
233 else
234 mpClipRegion = nullptr;
237 void VCLXGraphics::intersectClipRegion( const uno::Reference< awt::XRegion >& rxRegion )
239 SolarMutexGuard aGuard;
241 if ( rxRegion.is() )
243 vcl::Region aRegion( VCLUnoHelper::GetRegion( rxRegion ) );
244 if ( !mpClipRegion )
245 mpClipRegion = new vcl::Region( aRegion );
246 else
247 mpClipRegion->Intersect( aRegion );
251 void VCLXGraphics::push( )
253 SolarMutexGuard aGuard;
256 if( mpOutputDevice )
257 mpOutputDevice->Push();
260 void VCLXGraphics::pop( )
262 SolarMutexGuard aGuard;
265 if( mpOutputDevice )
266 mpOutputDevice->Pop();
269 void VCLXGraphics::clear(
270 const awt::Rectangle& aRect )
272 SolarMutexGuard aGuard;
274 if( mpOutputDevice )
276 const ::tools::Rectangle aVCLRect = VCLUnoHelper::ConvertToVCLRect( aRect );
277 mpOutputDevice->Erase( aVCLRect );
281 void VCLXGraphics::copy( const uno::Reference< awt::XDevice >& rxSource, sal_Int32 nSourceX, sal_Int32 nSourceY, sal_Int32 nSourceWidth, sal_Int32 nSourceHeight, sal_Int32 nDestX, sal_Int32 nDestY, sal_Int32 nDestWidth, sal_Int32 nDestHeight )
283 SolarMutexGuard aGuard;
285 if ( mpOutputDevice )
287 VCLXDevice* pFromDev = VCLXDevice::GetImplementation( rxSource );
288 DBG_ASSERT( pFromDev, "VCLXGraphics::copy - invalid device" );
289 if ( pFromDev )
291 InitOutputDevice( InitOutDevFlags::NONE );
292 mpOutputDevice->DrawOutDev( Point( nDestX, nDestY ), Size( nDestWidth, nDestHeight ),
293 Point( nSourceX, nSourceY ), Size( nSourceWidth, nSourceHeight ), *pFromDev->GetOutputDevice() );
298 void VCLXGraphics::draw( const uno::Reference< awt::XDisplayBitmap >& rxBitmapHandle, sal_Int32 nSourceX, sal_Int32 nSourceY, sal_Int32 nSourceWidth, sal_Int32 nSourceHeight, sal_Int32 nDestX, sal_Int32 nDestY, sal_Int32 nDestWidth, sal_Int32 nDestHeight )
300 SolarMutexGuard aGuard;
302 if( mpOutputDevice )
304 InitOutputDevice( InitOutDevFlags::NONE);
305 uno::Reference< awt::XBitmap > xBitmap( rxBitmapHandle, uno::UNO_QUERY );
306 BitmapEx aBmpEx = VCLUnoHelper::GetBitmap( xBitmap );
308 Point aPos(nDestX - nSourceX, nDestY - nSourceY);
309 Size aSz = aBmpEx.GetSizePixel();
311 if(nDestWidth != nSourceWidth)
313 float zoomX = (float)nDestWidth / (float)nSourceWidth;
314 aSz.Width() = (long) ((float)aSz.Width() * zoomX);
317 if(nDestHeight != nSourceHeight)
319 float zoomY = (float)nDestHeight / (float)nSourceHeight;
320 aSz.Height() = (long) ((float)aSz.Height() * zoomY);
323 if(nSourceX || nSourceY || aSz.Width() != nSourceWidth || aSz.Height() != nSourceHeight)
324 mpOutputDevice->IntersectClipRegion(vcl::Region(tools::Rectangle(nDestX, nDestY, nDestX + nDestWidth - 1, nDestY + nDestHeight - 1)));
326 mpOutputDevice->DrawBitmapEx( aPos, aSz, aBmpEx );
330 void VCLXGraphics::drawPixel( sal_Int32 x, sal_Int32 y )
332 SolarMutexGuard aGuard;
334 if( mpOutputDevice )
336 InitOutputDevice( InitOutDevFlags::COLORS );
337 mpOutputDevice->DrawPixel( Point( x, y ) );
341 void VCLXGraphics::drawLine( sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
343 SolarMutexGuard aGuard;
345 if( mpOutputDevice )
347 InitOutputDevice( InitOutDevFlags::COLORS );
348 mpOutputDevice->DrawLine( Point( x1, y1 ), Point( x2, y2 ) );
352 void VCLXGraphics::drawRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height )
354 SolarMutexGuard aGuard;
356 if( mpOutputDevice )
358 InitOutputDevice( InitOutDevFlags::COLORS );
359 mpOutputDevice->DrawRect( tools::Rectangle( Point( x, y ), Size( width, height ) ) );
363 void VCLXGraphics::drawRoundedRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 nHorzRound, sal_Int32 nVertRound )
365 SolarMutexGuard aGuard;
367 if( mpOutputDevice )
369 InitOutputDevice( InitOutDevFlags::COLORS );
370 mpOutputDevice->DrawRect( tools::Rectangle( Point( x, y ), Size( width, height ) ), nHorzRound, nVertRound );
374 void VCLXGraphics::drawPolyLine( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY )
376 SolarMutexGuard aGuard;
378 if( mpOutputDevice )
380 InitOutputDevice( InitOutDevFlags::COLORS );
381 mpOutputDevice->DrawPolyLine( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
385 void VCLXGraphics::drawPolygon( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY )
387 SolarMutexGuard aGuard;
389 if( mpOutputDevice )
391 InitOutputDevice( InitOutDevFlags::COLORS );
392 mpOutputDevice->DrawPolygon( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
396 void VCLXGraphics::drawPolyPolygon( const uno::Sequence< uno::Sequence< sal_Int32 > >& DataX, const uno::Sequence< uno::Sequence< sal_Int32 > >& DataY )
398 SolarMutexGuard aGuard;
400 if( mpOutputDevice )
402 InitOutputDevice( InitOutDevFlags::COLORS );
403 sal_uInt16 nPolys = (sal_uInt16) DataX.getLength();
404 tools::PolyPolygon aPolyPoly( nPolys );
405 for ( sal_uInt16 n = 0; n < nPolys; n++ )
406 aPolyPoly[n] = VCLUnoHelper::CreatePolygon( DataX.getConstArray()[n], DataY.getConstArray()[n] );
408 mpOutputDevice->DrawPolyPolygon( aPolyPoly );
412 void VCLXGraphics::drawEllipse( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height )
414 SolarMutexGuard aGuard;
416 if( mpOutputDevice )
418 InitOutputDevice( InitOutDevFlags::COLORS );
419 mpOutputDevice->DrawEllipse( tools::Rectangle( Point( x, y ), Size( width, height ) ) );
423 void VCLXGraphics::drawArc( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
425 SolarMutexGuard aGuard;
427 if( mpOutputDevice )
429 InitOutputDevice( InitOutDevFlags::COLORS );
430 mpOutputDevice->DrawArc( tools::Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
434 void VCLXGraphics::drawPie( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
436 SolarMutexGuard aGuard;
438 if( mpOutputDevice )
440 InitOutputDevice( InitOutDevFlags::COLORS );
441 mpOutputDevice->DrawPie( tools::Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
445 void VCLXGraphics::drawChord( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
447 SolarMutexGuard aGuard;
449 if( mpOutputDevice )
451 InitOutputDevice( InitOutDevFlags::COLORS );
452 mpOutputDevice->DrawChord( tools::Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
456 void VCLXGraphics::drawGradient( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, const awt::Gradient& rGradient )
458 SolarMutexGuard aGuard;
460 if( mpOutputDevice )
462 InitOutputDevice( InitOutDevFlags::COLORS );
463 Gradient aGradient((GradientStyle)rGradient.Style, rGradient.StartColor, rGradient.EndColor);
464 aGradient.SetAngle(rGradient.Angle);
465 aGradient.SetBorder(rGradient.Border);
466 aGradient.SetOfsX(rGradient.XOffset);
467 aGradient.SetOfsY(rGradient.YOffset);
468 aGradient.SetStartIntensity(rGradient.StartIntensity);
469 aGradient.SetEndIntensity(rGradient.EndIntensity);
470 aGradient.SetSteps(rGradient.StepCount);
471 mpOutputDevice->DrawGradient( tools::Rectangle( Point( x, y ), Size( width, height ) ), aGradient );
475 void VCLXGraphics::drawText( sal_Int32 x, sal_Int32 y, const OUString& rText )
477 SolarMutexGuard aGuard;
479 if( mpOutputDevice )
481 InitOutputDevice( InitOutDevFlags::COLORS |InitOutDevFlags::FONT);
482 mpOutputDevice->DrawText( Point( x, y ), rText );
486 void VCLXGraphics::drawTextArray( sal_Int32 x, sal_Int32 y, const OUString& rText, const uno::Sequence< sal_Int32 >& rLongs )
488 SolarMutexGuard aGuard;
490 if( mpOutputDevice )
492 InitOutputDevice( InitOutDevFlags::COLORS|InitOutDevFlags::FONT );
493 std::unique_ptr<long []> pDXA(new long[rText.getLength()]);
494 for(int i = 0; i < rText.getLength(); i++)
496 pDXA[i] = rLongs[i];
498 mpOutputDevice->DrawTextArray( Point( x, y ), rText, pDXA.get() );
503 void VCLXGraphics::drawImage( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int16 nStyle, const uno::Reference< graphic::XGraphic >& xGraphic )
505 SolarMutexGuard aGuard;
507 if( mpOutputDevice && xGraphic.is() )
509 Image aImage( xGraphic );
510 if ( !!aImage )
512 InitOutputDevice( InitOutDevFlags::COLORS );
513 mpOutputDevice->DrawImage( Point( x, y ), Size( width, height ), aImage, static_cast<DrawImageFlags>(nStyle) );
517 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */