Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / toolkit / source / awt / vclxgraphics.cxx
blob64115931fe51c8d811bb23bcacc6f1b3bf81c12b
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 <toolkit/awt/vclxgraphics.hxx>
22 #include <toolkit/awt/vclxdevice.hxx>
23 #include <toolkit/awt/vclxfont.hxx>
24 #include <toolkit/helper/macros.hxx>
25 #include <toolkit/helper/vclunohelper.hxx>
26 #include <cppuhelper/typeprovider.hxx>
27 #include <cppuhelper/queryinterface.hxx>
28 #include <rtl/uuid.h>
30 #include <vcl/svapp.hxx>
31 #include <vcl/outdev.hxx>
32 #include <vcl/image.hxx>
33 #include <vcl/gradient.hxx>
34 #include <tools/debug.hxx>
36 using namespace com::sun::star;
39 // class VCLXGraphics
42 // uno::XInterface
43 uno::Any VCLXGraphics::queryInterface( const uno::Type & rType ) throw(uno::RuntimeException, std::exception)
45 uno::Any aRet = ::cppu::queryInterface( rType,
46 (static_cast< css::awt::XGraphics* >(this)),
47 (static_cast< css::lang::XTypeProvider* >(this)),
48 (static_cast< css::lang::XUnoTunnel* >(this)) );
49 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
52 // lang::XUnoTunnel
53 IMPL_XUNOTUNNEL( VCLXGraphics )
55 // lang::XTypeProvider
56 IMPL_XTYPEPROVIDER_START( VCLXGraphics )
57 cppu::UnoType<awt::XGraphics>::get()
58 IMPL_XTYPEPROVIDER_END
60 VCLXGraphics::VCLXGraphics()
61 : mpOutputDevice(nullptr)
62 , meRasterOp(ROP_OVERPAINT)
63 , mpClipRegion(nullptr)
67 VCLXGraphics::~VCLXGraphics()
69 std::vector< VCLXGraphics* > *pLst = mpOutputDevice ? mpOutputDevice->GetUnoGraphicsList() : nullptr;
70 if ( pLst )
72 for( std::vector< VCLXGraphics* >::iterator it = pLst->begin(); it != pLst->end(); ++it )
74 if( *it == this ) {
75 pLst->erase( it );
76 break;
81 delete mpClipRegion;
83 SolarMutexGuard g;
84 mpOutputDevice.reset();
87 void VCLXGraphics::SetOutputDevice( OutputDevice* pOutDev )
89 mpOutputDevice = pOutDev;
90 mxDevice = nullptr;
91 initAttrs();
94 void VCLXGraphics::Init( OutputDevice* pOutDev )
96 DBG_ASSERT( !mpOutputDevice, "VCLXGraphics::Init already has pOutDev !" );
97 mpOutputDevice = pOutDev;
99 initAttrs();
100 mpClipRegion = nullptr;
102 // Register at OutputDevice
103 std::vector< VCLXGraphics* > *pLst = mpOutputDevice->GetUnoGraphicsList();
104 if ( !pLst )
105 pLst = mpOutputDevice->CreateUnoGraphicsList();
106 pLst->push_back( this );
109 void VCLXGraphics::initAttrs()
111 if ( !mpOutputDevice )
112 return;
114 maFont = mpOutputDevice->GetFont();
115 maTextColor = mpOutputDevice->GetTextColor(); /* COL_BLACK */
116 maTextFillColor = mpOutputDevice->GetTextFillColor(); /* COL_TRANSPARENT */
117 maLineColor = mpOutputDevice->GetLineColor(); /* COL_BLACK */
118 maFillColor = mpOutputDevice->GetFillColor(); /* COL_WHITE */
119 meRasterOp = mpOutputDevice->GetRasterOp(); /* ROP_OVERPAINT */
122 void VCLXGraphics::InitOutputDevice( InitOutDevFlags nFlags )
124 if(mpOutputDevice)
126 SolarMutexGuard aVclGuard;
128 if ( nFlags & InitOutDevFlags::FONT )
130 mpOutputDevice->SetFont( maFont );
131 mpOutputDevice->SetTextColor( maTextColor );
132 mpOutputDevice->SetTextFillColor( maTextFillColor );
135 if ( nFlags & InitOutDevFlags::COLORS )
137 mpOutputDevice->SetLineColor( maLineColor );
138 mpOutputDevice->SetFillColor( maFillColor );
141 if ( nFlags & InitOutDevFlags::RASTEROP )
143 mpOutputDevice->SetRasterOp( meRasterOp );
146 if ( nFlags & InitOutDevFlags::CLIPREGION )
148 if( mpClipRegion )
149 mpOutputDevice->SetClipRegion( *mpClipRegion );
150 else
151 mpOutputDevice->SetClipRegion();
156 uno::Reference< awt::XDevice > VCLXGraphics::getDevice() throw(uno::RuntimeException, std::exception)
158 SolarMutexGuard aGuard;
160 if( !mxDevice.is() && mpOutputDevice )
162 VCLXDevice* pDev = new VCLXDevice;
163 pDev->SetOutputDevice( mpOutputDevice );
164 mxDevice = pDev;
166 return mxDevice;
169 awt::SimpleFontMetric VCLXGraphics::getFontMetric() throw(uno::RuntimeException, std::exception)
171 SolarMutexGuard aGuard;
173 awt::SimpleFontMetric aM;
174 if( mpOutputDevice )
176 mpOutputDevice->SetFont( maFont );
177 aM = VCLUnoHelper::CreateFontMetric( mpOutputDevice->GetFontMetric() );
179 return aM;
182 void VCLXGraphics::setFont( const uno::Reference< awt::XFont >& rxFont ) throw(uno::RuntimeException, std::exception)
184 SolarMutexGuard aGuard;
186 maFont = VCLUnoHelper::CreateFont( rxFont );
189 void VCLXGraphics::selectFont( const awt::FontDescriptor& rDescription ) throw(uno::RuntimeException, std::exception)
191 SolarMutexGuard aGuard;
193 maFont = VCLUnoHelper::CreateFont( rDescription, vcl::Font() );
196 void VCLXGraphics::setTextColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
198 SolarMutexGuard aGuard;
200 maTextColor = Color( (sal_uInt32)nColor );
203 void VCLXGraphics::setTextFillColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
205 SolarMutexGuard aGuard;
207 maTextFillColor = Color( (sal_uInt32)nColor );
210 void VCLXGraphics::setLineColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
212 SolarMutexGuard aGuard;
214 maLineColor = Color( (sal_uInt32)nColor );
217 void VCLXGraphics::setFillColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
219 SolarMutexGuard aGuard;
221 maFillColor = Color( (sal_uInt32)nColor );
224 void VCLXGraphics::setRasterOp( awt::RasterOperation eROP ) throw(uno::RuntimeException, std::exception)
226 SolarMutexGuard aGuard;
228 meRasterOp = (RasterOp)eROP;
231 void VCLXGraphics::setClipRegion( const uno::Reference< awt::XRegion >& rxRegion ) throw(uno::RuntimeException, std::exception)
233 SolarMutexGuard aGuard;
235 delete mpClipRegion;
236 if ( rxRegion.is() )
237 mpClipRegion = new vcl::Region( VCLUnoHelper::GetRegion( rxRegion ) );
238 else
239 mpClipRegion = nullptr;
242 void VCLXGraphics::intersectClipRegion( const uno::Reference< awt::XRegion >& rxRegion ) throw(uno::RuntimeException, std::exception)
244 SolarMutexGuard aGuard;
246 if ( rxRegion.is() )
248 vcl::Region aRegion( VCLUnoHelper::GetRegion( rxRegion ) );
249 if ( !mpClipRegion )
250 mpClipRegion = new vcl::Region( aRegion );
251 else
252 mpClipRegion->Intersect( aRegion );
256 void VCLXGraphics::push( ) throw(uno::RuntimeException, std::exception)
258 SolarMutexGuard aGuard;
261 if( mpOutputDevice )
262 mpOutputDevice->Push();
265 void VCLXGraphics::pop( ) throw(uno::RuntimeException, std::exception)
267 SolarMutexGuard aGuard;
270 if( mpOutputDevice )
271 mpOutputDevice->Pop();
274 void VCLXGraphics::clear(
275 const awt::Rectangle& aRect )
276 throw(uno::RuntimeException, std::exception)
278 SolarMutexGuard aGuard;
280 if( mpOutputDevice )
282 const ::Rectangle aVCLRect = VCLUnoHelper::ConvertToVCLRect( aRect );
283 mpOutputDevice->Erase( aVCLRect );
287 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 ) throw(uno::RuntimeException, std::exception)
289 SolarMutexGuard aGuard;
291 if ( mpOutputDevice )
293 VCLXDevice* pFromDev = VCLXDevice::GetImplementation( rxSource );
294 DBG_ASSERT( pFromDev, "VCLXGraphics::copy - invalid device" );
295 if ( pFromDev )
297 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP );
298 mpOutputDevice->DrawOutDev( Point( nDestX, nDestY ), Size( nDestWidth, nDestHeight ),
299 Point( nSourceX, nSourceY ), Size( nSourceWidth, nSourceHeight ), *pFromDev->GetOutputDevice() );
304 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 ) throw(uno::RuntimeException, std::exception)
306 SolarMutexGuard aGuard;
308 if( mpOutputDevice )
310 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP);
311 uno::Reference< awt::XBitmap > xBitmap( rxBitmapHandle, uno::UNO_QUERY );
312 BitmapEx aBmpEx = VCLUnoHelper::GetBitmap( xBitmap );
314 Point aPos(nDestX - nSourceX, nDestY - nSourceY);
315 Size aSz = aBmpEx.GetSizePixel();
317 if(nDestWidth != nSourceWidth)
319 float zoomX = (float)nDestWidth / (float)nSourceWidth;
320 aSz.Width() = (long) ((float)aSz.Width() * zoomX);
323 if(nDestHeight != nSourceHeight)
325 float zoomY = (float)nDestHeight / (float)nSourceHeight;
326 aSz.Height() = (long) ((float)aSz.Height() * zoomY);
329 if(nSourceX || nSourceY || aSz.Width() != nSourceWidth || aSz.Height() != nSourceHeight)
330 mpOutputDevice->IntersectClipRegion(vcl::Region(Rectangle(nDestX, nDestY, nDestX + nDestWidth - 1, nDestY + nDestHeight - 1)));
332 mpOutputDevice->DrawBitmapEx( aPos, aSz, aBmpEx );
336 void VCLXGraphics::drawPixel( sal_Int32 x, sal_Int32 y ) throw(uno::RuntimeException, std::exception)
338 SolarMutexGuard aGuard;
340 if( mpOutputDevice )
342 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
343 mpOutputDevice->DrawPixel( Point( x, y ) );
347 void VCLXGraphics::drawLine( sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 ) throw(uno::RuntimeException, std::exception)
349 SolarMutexGuard aGuard;
351 if( mpOutputDevice )
353 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
354 mpOutputDevice->DrawLine( Point( x1, y1 ), Point( x2, y2 ) );
358 void VCLXGraphics::drawRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height ) throw(uno::RuntimeException, std::exception)
360 SolarMutexGuard aGuard;
362 if( mpOutputDevice )
364 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
365 mpOutputDevice->DrawRect( Rectangle( Point( x, y ), Size( width, height ) ) );
369 void VCLXGraphics::drawRoundedRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 nHorzRound, sal_Int32 nVertRound ) throw(uno::RuntimeException, std::exception)
371 SolarMutexGuard aGuard;
373 if( mpOutputDevice )
375 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
376 mpOutputDevice->DrawRect( Rectangle( Point( x, y ), Size( width, height ) ), nHorzRound, nVertRound );
380 void VCLXGraphics::drawPolyLine( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY ) throw(uno::RuntimeException, std::exception)
382 SolarMutexGuard aGuard;
384 if( mpOutputDevice )
386 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
387 mpOutputDevice->DrawPolyLine( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
391 void VCLXGraphics::drawPolygon( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY ) throw(uno::RuntimeException, std::exception)
393 SolarMutexGuard aGuard;
395 if( mpOutputDevice )
397 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
398 mpOutputDevice->DrawPolygon( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
402 void VCLXGraphics::drawPolyPolygon( const uno::Sequence< uno::Sequence< sal_Int32 > >& DataX, const uno::Sequence< uno::Sequence< sal_Int32 > >& DataY ) throw(uno::RuntimeException, std::exception)
404 SolarMutexGuard aGuard;
406 if( mpOutputDevice )
408 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
409 sal_uInt16 nPolys = (sal_uInt16) DataX.getLength();
410 tools::PolyPolygon aPolyPoly( nPolys );
411 for ( sal_uInt16 n = 0; n < nPolys; n++ )
412 aPolyPoly[n] = VCLUnoHelper::CreatePolygon( DataX.getConstArray()[n], DataY.getConstArray()[n] );
414 mpOutputDevice->DrawPolyPolygon( aPolyPoly );
418 void VCLXGraphics::drawEllipse( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height ) throw(uno::RuntimeException, std::exception)
420 SolarMutexGuard aGuard;
422 if( mpOutputDevice )
424 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
425 mpOutputDevice->DrawEllipse( Rectangle( Point( x, y ), Size( width, height ) ) );
429 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 ) throw(uno::RuntimeException, std::exception)
431 SolarMutexGuard aGuard;
433 if( mpOutputDevice )
435 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
436 mpOutputDevice->DrawArc( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
440 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 ) throw(uno::RuntimeException, std::exception)
442 SolarMutexGuard aGuard;
444 if( mpOutputDevice )
446 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
447 mpOutputDevice->DrawPie( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
451 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 ) throw(uno::RuntimeException, std::exception)
453 SolarMutexGuard aGuard;
455 if( mpOutputDevice )
457 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
458 mpOutputDevice->DrawChord( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
462 void VCLXGraphics::drawGradient( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, const awt::Gradient& rGradient ) throw(uno::RuntimeException, std::exception)
464 SolarMutexGuard aGuard;
466 if( mpOutputDevice )
468 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
469 Gradient aGradient((GradientStyle)rGradient.Style, rGradient.StartColor, rGradient.EndColor);
470 aGradient.SetAngle(rGradient.Angle);
471 aGradient.SetBorder(rGradient.Border);
472 aGradient.SetOfsX(rGradient.XOffset);
473 aGradient.SetOfsY(rGradient.YOffset);
474 aGradient.SetStartIntensity(rGradient.StartIntensity);
475 aGradient.SetEndIntensity(rGradient.EndIntensity);
476 aGradient.SetSteps(rGradient.StepCount);
477 mpOutputDevice->DrawGradient( Rectangle( Point( x, y ), Size( width, height ) ), aGradient );
481 void VCLXGraphics::drawText( sal_Int32 x, sal_Int32 y, const OUString& rText ) throw(uno::RuntimeException, std::exception)
483 SolarMutexGuard aGuard;
485 if( mpOutputDevice )
487 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS |InitOutDevFlags::FONT);
488 mpOutputDevice->DrawText( Point( x, y ), rText );
492 void VCLXGraphics::drawTextArray( sal_Int32 x, sal_Int32 y, const OUString& rText, const uno::Sequence< sal_Int32 >& rLongs ) throw(uno::RuntimeException, std::exception)
494 SolarMutexGuard aGuard;
496 if( mpOutputDevice )
498 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS|InitOutDevFlags::FONT );
499 std::unique_ptr<long []> pDXA(new long[rText.getLength()]);
500 for(int i = 0; i < rText.getLength(); i++)
502 pDXA[i] = rLongs[i];
504 mpOutputDevice->DrawTextArray( Point( x, y ), rText, pDXA.get() );
509 void VCLXGraphics::drawImage( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int16 nStyle, const uno::Reference< graphic::XGraphic >& xGraphic ) throw(uno::RuntimeException, std::exception)
511 SolarMutexGuard aGuard;
513 if( mpOutputDevice && xGraphic.is() )
515 Image aImage( xGraphic );
516 if ( !!aImage )
518 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
519 mpOutputDevice->DrawImage( Point( x, y ), Size( width, height ), aImage, static_cast<DrawImageFlags>(nStyle) );
523 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */