bump product version to 5.0.4.1
[LibreOffice.git] / toolkit / source / awt / vclxgraphics.cxx
blob482e29c4702123dca0179c23b87addc8c2c8ac3b
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>
29 #include <sal/alloca.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 ) throw(uno::RuntimeException, std::exception)
46 uno::Any aRet = ::cppu::queryInterface( rType,
47 (static_cast< ::com::sun::star::awt::XGraphics* >(this)),
48 (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)),
49 (static_cast< ::com::sun::star::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(NULL)
63 , meRasterOp(ROP_OVERPAINT)
64 , mpClipRegion(NULL)
68 VCLXGraphics::~VCLXGraphics()
70 std::vector< VCLXGraphics* > *pLst = mpOutputDevice ? mpOutputDevice->GetUnoGraphicsList() : NULL;
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 = NULL;
92 initAttrs();
95 void VCLXGraphics::Init( OutputDevice* pOutDev )
97 DBG_ASSERT( !mpOutputDevice, "VCLXGraphics::Init already has pOutDev !" );
98 mpOutputDevice = pOutDev;
100 initAttrs();
101 mpClipRegion = NULL;
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(); /* ROP_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 if ( nFlags & InitOutDevFlags::RASTEROP )
144 mpOutputDevice->SetRasterOp( meRasterOp );
147 if ( nFlags & InitOutDevFlags::CLIPREGION )
149 if( mpClipRegion )
150 mpOutputDevice->SetClipRegion( *mpClipRegion );
151 else
152 mpOutputDevice->SetClipRegion();
157 uno::Reference< awt::XDevice > VCLXGraphics::getDevice() throw(uno::RuntimeException, std::exception)
159 SolarMutexGuard aGuard;
161 if( !mxDevice.is() && mpOutputDevice )
163 VCLXDevice* pDev = new VCLXDevice;
164 pDev->SetOutputDevice( mpOutputDevice );
165 mxDevice = pDev;
167 return mxDevice;
170 awt::SimpleFontMetric VCLXGraphics::getFontMetric() throw(uno::RuntimeException, std::exception)
172 SolarMutexGuard aGuard;
174 awt::SimpleFontMetric aM;
175 if( mpOutputDevice )
177 mpOutputDevice->SetFont( maFont );
178 aM = VCLUnoHelper::CreateFontMetric( mpOutputDevice->GetFontMetric() );
180 return aM;
183 void VCLXGraphics::setFont( const uno::Reference< awt::XFont >& rxFont ) throw(uno::RuntimeException, std::exception)
185 SolarMutexGuard aGuard;
187 maFont = VCLUnoHelper::CreateFont( rxFont );
190 void VCLXGraphics::selectFont( const awt::FontDescriptor& rDescription ) throw(uno::RuntimeException, std::exception)
192 SolarMutexGuard aGuard;
194 maFont = VCLUnoHelper::CreateFont( rDescription, vcl::Font() );
197 void VCLXGraphics::setTextColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
199 SolarMutexGuard aGuard;
201 maTextColor = Color( (sal_uInt32)nColor );
204 void VCLXGraphics::setTextFillColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
206 SolarMutexGuard aGuard;
208 maTextFillColor = Color( (sal_uInt32)nColor );
211 void VCLXGraphics::setLineColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
213 SolarMutexGuard aGuard;
215 maLineColor = Color( (sal_uInt32)nColor );
218 void VCLXGraphics::setFillColor( sal_Int32 nColor ) throw(uno::RuntimeException, std::exception)
220 SolarMutexGuard aGuard;
222 maFillColor = Color( (sal_uInt32)nColor );
225 void VCLXGraphics::setRasterOp( awt::RasterOperation eROP ) throw(uno::RuntimeException, std::exception)
227 SolarMutexGuard aGuard;
229 meRasterOp = (RasterOp)eROP;
232 void VCLXGraphics::setClipRegion( const uno::Reference< awt::XRegion >& rxRegion ) throw(uno::RuntimeException, std::exception)
234 SolarMutexGuard aGuard;
236 delete mpClipRegion;
237 if ( rxRegion.is() )
238 mpClipRegion = new vcl::Region( VCLUnoHelper::GetRegion( rxRegion ) );
239 else
240 mpClipRegion = NULL;
243 void VCLXGraphics::intersectClipRegion( const uno::Reference< awt::XRegion >& rxRegion ) throw(uno::RuntimeException, std::exception)
245 SolarMutexGuard aGuard;
247 if ( rxRegion.is() )
249 vcl::Region aRegion( VCLUnoHelper::GetRegion( rxRegion ) );
250 if ( !mpClipRegion )
251 mpClipRegion = new vcl::Region( aRegion );
252 else
253 mpClipRegion->Intersect( aRegion );
257 void VCLXGraphics::push( ) throw(uno::RuntimeException, std::exception)
259 SolarMutexGuard aGuard;
262 if( mpOutputDevice )
263 mpOutputDevice->Push();
266 void VCLXGraphics::pop( ) throw(uno::RuntimeException, std::exception)
268 SolarMutexGuard aGuard;
271 if( mpOutputDevice )
272 mpOutputDevice->Pop();
275 void VCLXGraphics::clear(
276 const awt::Rectangle& aRect )
277 throw(uno::RuntimeException, std::exception)
279 SolarMutexGuard aGuard;
281 if( mpOutputDevice )
283 const ::Rectangle aVCLRect = VCLUnoHelper::ConvertToVCLRect( aRect );
284 mpOutputDevice->Erase( aVCLRect );
288 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)
290 SolarMutexGuard aGuard;
292 if ( mpOutputDevice )
294 VCLXDevice* pFromDev = VCLXDevice::GetImplementation( rxSource );
295 DBG_ASSERT( pFromDev, "VCLXGraphics::copy - invalid device" );
296 if ( pFromDev )
298 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP );
299 mpOutputDevice->DrawOutDev( Point( nDestX, nDestY ), Size( nDestWidth, nDestHeight ),
300 Point( nSourceX, nSourceY ), Size( nSourceWidth, nSourceHeight ), *pFromDev->GetOutputDevice() );
305 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)
307 SolarMutexGuard aGuard;
309 if( mpOutputDevice )
311 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP);
312 uno::Reference< awt::XBitmap > xBitmap( rxBitmapHandle, uno::UNO_QUERY );
313 BitmapEx aBmpEx = VCLUnoHelper::GetBitmap( xBitmap );
315 Point aPos(nDestX - nSourceX, nDestY - nSourceY);
316 Size aSz = aBmpEx.GetSizePixel();
318 if(nDestWidth != nSourceWidth)
320 float zoomX = (float)nDestWidth / (float)nSourceWidth;
321 aSz.Width() = (long) ((float)aSz.Width() * zoomX);
324 if(nDestHeight != nSourceHeight)
326 float zoomY = (float)nDestHeight / (float)nSourceHeight;
327 aSz.Height() = (long) ((float)aSz.Height() * zoomY);
330 if(nSourceX || nSourceY || aSz.Width() != nSourceWidth || aSz.Height() != nSourceHeight)
331 mpOutputDevice->IntersectClipRegion(vcl::Region(Rectangle(nDestX, nDestY, nDestX + nDestWidth - 1, nDestY + nDestHeight - 1)));
333 mpOutputDevice->DrawBitmapEx( aPos, aSz, aBmpEx );
337 void VCLXGraphics::drawPixel( sal_Int32 x, sal_Int32 y ) throw(uno::RuntimeException, std::exception)
339 SolarMutexGuard aGuard;
341 if( mpOutputDevice )
343 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
344 mpOutputDevice->DrawPixel( Point( x, y ) );
348 void VCLXGraphics::drawLine( sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 ) throw(uno::RuntimeException, std::exception)
350 SolarMutexGuard aGuard;
352 if( mpOutputDevice )
354 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
355 mpOutputDevice->DrawLine( Point( x1, y1 ), Point( x2, y2 ) );
359 void VCLXGraphics::drawRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height ) throw(uno::RuntimeException, std::exception)
361 SolarMutexGuard aGuard;
363 if( mpOutputDevice )
365 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
366 mpOutputDevice->DrawRect( Rectangle( Point( x, y ), Size( width, height ) ) );
370 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)
372 SolarMutexGuard aGuard;
374 if( mpOutputDevice )
376 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
377 mpOutputDevice->DrawRect( Rectangle( Point( x, y ), Size( width, height ) ), nHorzRound, nVertRound );
381 void VCLXGraphics::drawPolyLine( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY ) throw(uno::RuntimeException, std::exception)
383 SolarMutexGuard aGuard;
385 if( mpOutputDevice )
387 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
388 mpOutputDevice->DrawPolyLine( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
392 void VCLXGraphics::drawPolygon( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY ) throw(uno::RuntimeException, std::exception)
394 SolarMutexGuard aGuard;
396 if( mpOutputDevice )
398 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
399 mpOutputDevice->DrawPolygon( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
403 void VCLXGraphics::drawPolyPolygon( const uno::Sequence< uno::Sequence< sal_Int32 > >& DataX, const uno::Sequence< uno::Sequence< sal_Int32 > >& DataY ) throw(uno::RuntimeException, std::exception)
405 SolarMutexGuard aGuard;
407 if( mpOutputDevice )
409 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
410 sal_uInt16 nPolys = (sal_uInt16) DataX.getLength();
411 tools::PolyPolygon aPolyPoly( nPolys );
412 for ( sal_uInt16 n = 0; n < nPolys; n++ )
413 aPolyPoly[n] = VCLUnoHelper::CreatePolygon( DataX.getConstArray()[n], DataY.getConstArray()[n] );
415 mpOutputDevice->DrawPolyPolygon( aPolyPoly );
419 void VCLXGraphics::drawEllipse( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height ) throw(uno::RuntimeException, std::exception)
421 SolarMutexGuard aGuard;
423 if( mpOutputDevice )
425 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
426 mpOutputDevice->DrawEllipse( Rectangle( Point( x, y ), Size( width, height ) ) );
430 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)
432 SolarMutexGuard aGuard;
434 if( mpOutputDevice )
436 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
437 mpOutputDevice->DrawArc( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
441 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)
443 SolarMutexGuard aGuard;
445 if( mpOutputDevice )
447 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
448 mpOutputDevice->DrawPie( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
452 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)
454 SolarMutexGuard aGuard;
456 if( mpOutputDevice )
458 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
459 mpOutputDevice->DrawChord( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
463 void VCLXGraphics::drawGradient( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, const awt::Gradient& rGradient ) throw(uno::RuntimeException, std::exception)
465 SolarMutexGuard aGuard;
467 if( mpOutputDevice )
469 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
470 Gradient aGradient((GradientStyle)rGradient.Style, rGradient.StartColor, rGradient.EndColor);
471 aGradient.SetAngle(rGradient.Angle);
472 aGradient.SetBorder(rGradient.Border);
473 aGradient.SetOfsX(rGradient.XOffset);
474 aGradient.SetOfsY(rGradient.YOffset);
475 aGradient.SetStartIntensity(rGradient.StartIntensity);
476 aGradient.SetEndIntensity(rGradient.EndIntensity);
477 aGradient.SetSteps(rGradient.StepCount);
478 mpOutputDevice->DrawGradient( Rectangle( Point( x, y ), Size( width, height ) ), aGradient );
482 void VCLXGraphics::drawText( sal_Int32 x, sal_Int32 y, const OUString& rText ) throw(uno::RuntimeException, std::exception)
484 SolarMutexGuard aGuard;
486 if( mpOutputDevice )
488 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS |InitOutDevFlags::FONT);
489 mpOutputDevice->DrawText( Point( x, y ), rText );
493 void VCLXGraphics::drawTextArray( sal_Int32 x, sal_Int32 y, const OUString& rText, const uno::Sequence< sal_Int32 >& rLongs ) throw(uno::RuntimeException, std::exception)
495 SolarMutexGuard aGuard;
497 if( mpOutputDevice )
499 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS|InitOutDevFlags::FONT );
500 long* pDXA = static_cast<long*>(alloca(rText.getLength() * sizeof(long)));
501 for(int i = 0; i < rText.getLength(); i++)
503 pDXA[i] = rLongs[i];
505 mpOutputDevice->DrawTextArray( Point( x, y ), rText, pDXA );
510 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)
512 SolarMutexGuard aGuard;
514 if( mpOutputDevice && xGraphic.is() )
516 Image aImage( xGraphic );
517 if ( !!aImage )
519 InitOutputDevice( InitOutDevFlags::CLIPREGION|InitOutDevFlags::RASTEROP|InitOutDevFlags::COLORS );
520 mpOutputDevice->DrawImage( Point( x, y ), Size( width, height ), aImage, static_cast<DrawImageFlags>(nStyle) );
524 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */