Updated core
[LibreOffice.git] / toolkit / source / awt / vclxgraphics.cxx
blob3e7461fdd30547bf6db59b6c66dc4749675db180
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 <rtl/uuid.h>
29 #include <vcl/svapp.hxx>
30 #include <vcl/outdev.hxx>
31 #include <vcl/image.hxx>
32 #include <vcl/gradient.hxx>
33 #include <tools/debug.hxx>
35 using namespace com::sun::star;
37 // ----------------------------------------------------
38 // class VCLXGraphics
39 // ----------------------------------------------------
41 // uno::XInterface
42 uno::Any VCLXGraphics::queryInterface( const uno::Type & rType ) throw(uno::RuntimeException)
44 uno::Any aRet = ::cppu::queryInterface( rType,
45 (static_cast< ::com::sun::star::awt::XGraphics* >(this)),
46 (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)),
47 (static_cast< ::com::sun::star::lang::XUnoTunnel* >(this)) );
48 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
51 // lang::XUnoTunnel
52 IMPL_XUNOTUNNEL( VCLXGraphics )
54 // lang::XTypeProvider
55 IMPL_XTYPEPROVIDER_START( VCLXGraphics )
56 getCppuType( ( uno::Reference< awt::XGraphics>* ) NULL )
57 IMPL_XTYPEPROVIDER_END
59 VCLXGraphics::VCLXGraphics()
61 mpOutputDevice = NULL;
62 mpClipRegion = NULL;
65 VCLXGraphics::~VCLXGraphics()
67 VCLXGraphicsList_impl* pLst = mpOutputDevice ? mpOutputDevice->GetUnoGraphicsList() : NULL;
68 if ( pLst )
70 for( VCLXGraphicsList_impl::iterator it = pLst->begin(); it != pLst->end(); ++it )
72 if( *it == this ) {
73 pLst->erase( it );
74 break;
79 delete mpClipRegion;
82 void VCLXGraphics::SetOutputDevice( OutputDevice* pOutDev )
84 mpOutputDevice = pOutDev;
85 mxDevice = NULL;
86 initAttrs();
89 void VCLXGraphics::Init( OutputDevice* pOutDev )
91 DBG_ASSERT( !mpOutputDevice, "VCLXGraphics::Init already has pOutDev !" );
92 mpOutputDevice = pOutDev;
94 initAttrs();
95 mpClipRegion = NULL;
97 // Register at OutputDevice
98 VCLXGraphicsList_impl* pLst = mpOutputDevice->GetUnoGraphicsList();
99 if ( !pLst )
100 pLst = mpOutputDevice->CreateUnoGraphicsList();
101 pLst->push_back( this );
104 void VCLXGraphics::initAttrs()
106 if ( !mpOutputDevice )
107 return;
109 maFont = mpOutputDevice->GetFont();
110 maTextColor = mpOutputDevice->GetTextColor(); /* COL_BLACK */
111 maTextFillColor = mpOutputDevice->GetTextFillColor(); /* COL_TRANSPARENT */
112 maLineColor = mpOutputDevice->GetLineColor(); /* COL_BLACK */
113 maFillColor = mpOutputDevice->GetFillColor(); /* COL_WHITE */
114 meRasterOp = mpOutputDevice->GetRasterOp(); /* ROP_OVERPAINT */
117 void VCLXGraphics::InitOutputDevice( sal_uInt16 nFlags )
119 if(mpOutputDevice)
121 SolarMutexGuard aVclGuard;
123 if ( nFlags & INITOUTDEV_FONT )
125 mpOutputDevice->SetFont( maFont );
126 mpOutputDevice->SetTextColor( maTextColor );
127 mpOutputDevice->SetTextFillColor( maTextFillColor );
130 if ( nFlags & INITOUTDEV_COLORS )
132 mpOutputDevice->SetLineColor( maLineColor );
133 mpOutputDevice->SetFillColor( maFillColor );
136 if ( nFlags & INITOUTDEV_RASTEROP )
138 mpOutputDevice->SetRasterOp( meRasterOp );
141 if ( nFlags & INITOUTDEV_CLIPREGION )
143 if( mpClipRegion )
144 mpOutputDevice->SetClipRegion( *mpClipRegion );
145 else
146 mpOutputDevice->SetClipRegion();
151 uno::Reference< awt::XDevice > VCLXGraphics::getDevice() throw(uno::RuntimeException)
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() throw(uno::RuntimeException)
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 ) throw(uno::RuntimeException)
179 SolarMutexGuard aGuard;
181 maFont = VCLUnoHelper::CreateFont( rxFont );
184 uno::Reference< awt::XFont > VCLXGraphics::getFont() throw(uno::RuntimeException)
186 uno::Reference< awt::XFont > xFont;
187 uno::Reference< awt::XDevice > xDevice( getDevice() );
189 SolarMutexGuard aGuard;
191 if ( xDevice.is() )
193 VCLXFont *pFont = new VCLXFont;
194 pFont->Init( *xDevice.get(), maFont );
195 xFont.set( static_cast< ::cppu::OWeakObject* >( pFont ), uno::UNO_QUERY );
198 return xFont;
201 void VCLXGraphics::selectFont( const awt::FontDescriptor& rDescription ) throw(uno::RuntimeException)
203 SolarMutexGuard aGuard;
205 maFont = VCLUnoHelper::CreateFont( rDescription, Font() );
208 void VCLXGraphics::setTextColor( sal_Int32 nColor ) throw(uno::RuntimeException)
210 SolarMutexGuard aGuard;
212 maTextColor = Color( (sal_uInt32)nColor );
215 ::sal_Int32 VCLXGraphics::getTextColor() throw(uno::RuntimeException)
217 SolarMutexGuard aGuard;
218 return maTextColor.GetColor();
221 void VCLXGraphics::setTextFillColor( sal_Int32 nColor ) throw(uno::RuntimeException)
223 SolarMutexGuard aGuard;
225 maTextFillColor = Color( (sal_uInt32)nColor );
228 ::sal_Int32 VCLXGraphics::getTextFillColor() throw(uno::RuntimeException)
230 SolarMutexGuard aGuard;
231 return maTextFillColor.GetColor();
234 void VCLXGraphics::setLineColor( sal_Int32 nColor ) throw(uno::RuntimeException)
236 SolarMutexGuard aGuard;
238 maLineColor = Color( (sal_uInt32)nColor );
241 ::sal_Int32 VCLXGraphics::getLineColor() throw(uno::RuntimeException)
243 SolarMutexGuard aGuard;
244 return maLineColor.GetColor();
247 void VCLXGraphics::setFillColor( sal_Int32 nColor ) throw(uno::RuntimeException)
249 SolarMutexGuard aGuard;
251 maFillColor = Color( (sal_uInt32)nColor );
254 ::sal_Int32 VCLXGraphics::getFillColor() throw(uno::RuntimeException)
256 SolarMutexGuard aGuard;
257 return maFillColor.GetColor();
260 void VCLXGraphics::setRasterOp( awt::RasterOperation eROP ) throw(uno::RuntimeException)
262 SolarMutexGuard aGuard;
264 meRasterOp = (RasterOp)eROP;
267 awt::RasterOperation VCLXGraphics::getRasterOp()
268 throw(uno::RuntimeException)
270 SolarMutexGuard aGuard;
271 return (awt::RasterOperation) meRasterOp;
274 void VCLXGraphics::setClipRegion( const uno::Reference< awt::XRegion >& rxRegion ) throw(uno::RuntimeException)
276 SolarMutexGuard aGuard;
278 delete mpClipRegion;
279 if ( rxRegion.is() )
280 mpClipRegion = new Region( VCLUnoHelper::GetRegion( rxRegion ) );
281 else
282 mpClipRegion = NULL;
285 void VCLXGraphics::intersectClipRegion( const uno::Reference< awt::XRegion >& rxRegion ) throw(uno::RuntimeException)
287 SolarMutexGuard aGuard;
289 if ( rxRegion.is() )
291 Region aRegion( VCLUnoHelper::GetRegion( rxRegion ) );
292 if ( !mpClipRegion )
293 mpClipRegion = new Region( aRegion );
294 else
295 mpClipRegion->Intersect( aRegion );
299 void VCLXGraphics::push( ) throw(uno::RuntimeException)
301 SolarMutexGuard aGuard;
304 if( mpOutputDevice )
305 mpOutputDevice->Push();
308 void VCLXGraphics::pop( ) throw(uno::RuntimeException)
310 SolarMutexGuard aGuard;
313 if( mpOutputDevice )
314 mpOutputDevice->Pop();
317 void VCLXGraphics::clear(
318 const awt::Rectangle& aRect )
319 throw(uno::RuntimeException)
321 SolarMutexGuard aGuard;
323 if( mpOutputDevice )
325 const ::Rectangle aVCLRect = VCLUnoHelper::ConvertToVCLRect( aRect );
326 mpOutputDevice->Erase( aVCLRect );
330 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)
332 SolarMutexGuard aGuard;
334 if ( mpOutputDevice )
336 VCLXDevice* pFromDev = VCLXDevice::GetImplementation( rxSource );
337 DBG_ASSERT( pFromDev, "VCLXGraphics::copy - invalid device" );
338 if ( pFromDev )
340 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP );
341 mpOutputDevice->DrawOutDev( Point( nDestX, nDestY ), Size( nDestWidth, nDestHeight ),
342 Point( nSourceX, nSourceY ), Size( nSourceWidth, nSourceHeight ), *pFromDev->GetOutputDevice() );
347 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)
349 SolarMutexGuard aGuard;
351 if( mpOutputDevice )
353 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP);
354 uno::Reference< awt::XBitmap > xBitmap( rxBitmapHandle, uno::UNO_QUERY );
355 BitmapEx aBmpEx = VCLUnoHelper::GetBitmap( xBitmap );
357 Point aPos(nDestX - nSourceX, nDestY - nSourceY);
358 Size aSz = aBmpEx.GetSizePixel();
360 if(nDestWidth != nSourceWidth)
362 float zoomX = (float)nDestWidth / (float)nSourceWidth;
363 aSz.Width() = (long) ((float)aSz.Width() * zoomX);
366 if(nDestHeight != nSourceHeight)
368 float zoomY = (float)nDestHeight / (float)nSourceHeight;
369 aSz.Height() = (long) ((float)aSz.Height() * zoomY);
372 if(nSourceX || nSourceY || aSz.Width() != nSourceWidth || aSz.Height() != nSourceHeight)
373 mpOutputDevice->IntersectClipRegion(Region(Rectangle(nDestX, nDestY, nDestX + nDestWidth - 1, nDestY + nDestHeight - 1)));
375 mpOutputDevice->DrawBitmapEx( aPos, aSz, aBmpEx );
379 void VCLXGraphics::drawPixel( sal_Int32 x, sal_Int32 y ) throw(uno::RuntimeException)
381 SolarMutexGuard aGuard;
383 if( mpOutputDevice )
385 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
386 mpOutputDevice->DrawPixel( Point( x, y ) );
390 void VCLXGraphics::drawLine( sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 ) throw(uno::RuntimeException)
392 SolarMutexGuard aGuard;
394 if( mpOutputDevice )
396 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
397 mpOutputDevice->DrawLine( Point( x1, y1 ), Point( x2, y2 ) );
401 void VCLXGraphics::drawRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height ) throw(uno::RuntimeException)
403 SolarMutexGuard aGuard;
405 if( mpOutputDevice )
407 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
408 mpOutputDevice->DrawRect( Rectangle( Point( x, y ), Size( width, height ) ) );
412 void VCLXGraphics::drawRoundedRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 nHorzRound, sal_Int32 nVertRound ) throw(uno::RuntimeException)
414 SolarMutexGuard aGuard;
416 if( mpOutputDevice )
418 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
419 mpOutputDevice->DrawRect( Rectangle( Point( x, y ), Size( width, height ) ), nHorzRound, nVertRound );
423 void VCLXGraphics::drawPolyLine( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY ) throw(uno::RuntimeException)
425 SolarMutexGuard aGuard;
427 if( mpOutputDevice )
429 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
430 mpOutputDevice->DrawPolyLine( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
434 void VCLXGraphics::drawPolygon( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY ) throw(uno::RuntimeException)
436 SolarMutexGuard aGuard;
438 if( mpOutputDevice )
440 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
441 mpOutputDevice->DrawPolygon( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
445 void VCLXGraphics::drawPolyPolygon( const uno::Sequence< uno::Sequence< sal_Int32 > >& DataX, const uno::Sequence< uno::Sequence< sal_Int32 > >& DataY ) throw(uno::RuntimeException)
447 SolarMutexGuard aGuard;
449 if( mpOutputDevice )
451 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
452 sal_uInt16 nPolys = (sal_uInt16) DataX.getLength();
453 PolyPolygon aPolyPoly( nPolys );
454 for ( sal_uInt16 n = 0; n < nPolys; n++ )
455 aPolyPoly[n] = VCLUnoHelper::CreatePolygon( DataX.getConstArray()[n], DataY.getConstArray()[n] );
457 mpOutputDevice->DrawPolyPolygon( aPolyPoly );
461 void VCLXGraphics::drawEllipse( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height ) throw(uno::RuntimeException)
463 SolarMutexGuard aGuard;
465 if( mpOutputDevice )
467 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
468 mpOutputDevice->DrawEllipse( Rectangle( Point( x, y ), Size( width, height ) ) );
472 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)
474 SolarMutexGuard aGuard;
476 if( mpOutputDevice )
478 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
479 mpOutputDevice->DrawArc( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
483 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)
485 SolarMutexGuard aGuard;
487 if( mpOutputDevice )
489 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
490 mpOutputDevice->DrawPie( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
494 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)
496 SolarMutexGuard aGuard;
498 if( mpOutputDevice )
500 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
501 mpOutputDevice->DrawChord( Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
505 void VCLXGraphics::drawGradient( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, const awt::Gradient& rGradient ) throw(uno::RuntimeException)
507 SolarMutexGuard aGuard;
509 if( mpOutputDevice )
511 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
512 Gradient aGradient((GradientStyle)rGradient.Style, rGradient.StartColor, rGradient.EndColor);
513 aGradient.SetAngle(rGradient.Angle);
514 aGradient.SetBorder(rGradient.Border);
515 aGradient.SetOfsX(rGradient.XOffset);
516 aGradient.SetOfsY(rGradient.YOffset);
517 aGradient.SetStartIntensity(rGradient.StartIntensity);
518 aGradient.SetEndIntensity(rGradient.EndIntensity);
519 aGradient.SetSteps(rGradient.StepCount);
520 mpOutputDevice->DrawGradient( Rectangle( Point( x, y ), Size( width, height ) ), aGradient );
524 void VCLXGraphics::drawText( sal_Int32 x, sal_Int32 y, const OUString& rText ) throw(uno::RuntimeException)
526 SolarMutexGuard aGuard;
528 if( mpOutputDevice )
530 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS |INITOUTDEV_FONT);
531 mpOutputDevice->DrawText( Point( x, y ), rText );
535 void VCLXGraphics::drawTextArray( sal_Int32 x, sal_Int32 y, const OUString& rText, const uno::Sequence< sal_Int32 >& rLongs ) throw(uno::RuntimeException)
537 SolarMutexGuard aGuard;
539 if( mpOutputDevice )
541 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS|INITOUTDEV_FONT );
542 mpOutputDevice->DrawTextArray( Point( x, y ), rText, rLongs.getConstArray() );
547 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)
549 SolarMutexGuard aGuard;
551 if( mpOutputDevice && xGraphic.is() )
553 Image aImage( xGraphic );
554 if ( !!aImage )
556 InitOutputDevice( INITOUTDEV_CLIPREGION|INITOUTDEV_RASTEROP|INITOUTDEV_COLORS );
557 mpOutputDevice->DrawImage( Point( x, y ), Size( width, height ), aImage, nStyle );
561 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */