1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <toolkit/awt/vclxgraphics.hxx>
31 #include <toolkit/awt/vclxdevice.hxx>
32 #include <toolkit/helper/macros.hxx>
33 #include <toolkit/helper/vclunohelper.hxx>
34 #include <cppuhelper/typeprovider.hxx>
35 #include <rtl/memory.h>
38 #include <vcl/svapp.hxx>
39 #include <vcl/outdev.hxx>
40 #include <vcl/gradient.hxx>
41 #include <tools/debug.hxx>
44 // ----------------------------------------------------
46 // ----------------------------------------------------
48 // ::com::sun::star::uno::XInterface
49 ::com::sun::star::uno::Any
VCLXGraphics::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
51 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
52 (static_cast< ::com::sun::star::awt::XGraphics
* >(this)),
53 (static_cast< ::com::sun::star::lang::XTypeProvider
* >(this)),
54 (static_cast< ::com::sun::star::lang::XUnoTunnel
* >(this)) );
55 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
58 // ::com::sun::star::lang::XUnoTunnel
59 IMPL_XUNOTUNNEL( VCLXGraphics
)
61 // ::com::sun::star::lang::XTypeProvider
62 IMPL_XTYPEPROVIDER_START( VCLXGraphics
)
63 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
>* ) NULL
)
64 IMPL_XTYPEPROVIDER_END
66 VCLXGraphics::VCLXGraphics()
68 mpOutputDevice
= NULL
;
72 VCLXGraphics::~VCLXGraphics()
74 VCLXGraphicsList_impl
* pLst
= mpOutputDevice
? mpOutputDevice
->GetUnoGraphicsList() : NULL
;
77 for( VCLXGraphicsList_impl::iterator it
= pLst
->begin(); it
!= pLst
->end(); ++it
)
89 void VCLXGraphics::SetOutputDevice( OutputDevice
* pOutDev
)
91 mpOutputDevice
= pOutDev
;
95 void VCLXGraphics::Init( OutputDevice
* pOutDev
)
97 DBG_ASSERT( !mpOutputDevice
, "VCLXGraphics::Init allready has pOutDev !" );
98 mpOutputDevice
= pOutDev
;
100 maFont
= mpOutputDevice
->GetFont();
101 maTextColor
= COL_BLACK
;
102 maTextFillColor
= COL_TRANSPARENT
;
103 maLineColor
= COL_BLACK
;
104 maFillColor
= COL_WHITE
;
105 meRasterOp
= ROP_OVERPAINT
;
108 // Register at OutputDevice
109 VCLXGraphicsList_impl
* pLst
= mpOutputDevice
->GetUnoGraphicsList();
111 pLst
= mpOutputDevice
->CreateUnoGraphicsList();
112 pLst
->push_back( this );
115 void VCLXGraphics::InitOutputDevice( sal_uInt16 nFlags
)
119 SolarMutexGuard aVclGuard
;
121 if ( nFlags
& INITOUTDEV_FONT
)
123 mpOutputDevice
->SetFont( maFont
);
124 mpOutputDevice
->SetTextColor( maTextColor
);
125 mpOutputDevice
->SetTextFillColor( maTextFillColor
);
128 if ( nFlags
& INITOUTDEV_COLORS
)
130 mpOutputDevice
->SetLineColor( maLineColor
);
131 mpOutputDevice
->SetFillColor( maFillColor
);
134 if ( nFlags
& INITOUTDEV_RASTEROP
)
136 mpOutputDevice
->SetRasterOp( meRasterOp
);
139 if ( nFlags
& INITOUTDEV_CLIPREGION
)
142 mpOutputDevice
->SetClipRegion( *mpClipRegion
);
144 mpOutputDevice
->SetClipRegion();
149 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXGraphics::getDevice() throw(::com::sun::star::uno::RuntimeException
)
151 SolarMutexGuard aGuard
;
153 if( !mxDevice
.is() && mpOutputDevice
)
155 VCLXDevice
* pDev
= new VCLXDevice
;
156 pDev
->SetOutputDevice( mpOutputDevice
);
162 ::com::sun::star::awt::SimpleFontMetric
VCLXGraphics::getFontMetric() throw(::com::sun::star::uno::RuntimeException
)
164 SolarMutexGuard aGuard
;
166 ::com::sun::star::awt::SimpleFontMetric aM
;
169 mpOutputDevice
->SetFont( maFont
);
170 aM
= VCLUnoHelper::CreateFontMetric( mpOutputDevice
->GetFontMetric() );
175 void VCLXGraphics::setFont( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XFont
>& rxFont
) throw(::com::sun::star::uno::RuntimeException
)
177 SolarMutexGuard aGuard
;
179 maFont
= VCLUnoHelper::CreateFont( rxFont
);
182 void VCLXGraphics::selectFont( const ::com::sun::star::awt::FontDescriptor
& rDescription
) throw(::com::sun::star::uno::RuntimeException
)
184 SolarMutexGuard aGuard
;
186 maFont
= VCLUnoHelper::CreateFont( rDescription
, Font() );
189 void VCLXGraphics::setTextColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
191 SolarMutexGuard aGuard
;
193 maTextColor
= Color( (sal_uInt32
)nColor
);
196 void VCLXGraphics::setTextFillColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
198 SolarMutexGuard aGuard
;
200 maTextFillColor
= Color( (sal_uInt32
)nColor
);
203 void VCLXGraphics::setLineColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
205 SolarMutexGuard aGuard
;
207 maLineColor
= Color( (sal_uInt32
)nColor
);
210 void VCLXGraphics::setFillColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
212 SolarMutexGuard aGuard
;
214 maFillColor
= Color( (sal_uInt32
)nColor
);
217 void VCLXGraphics::setRasterOp( ::com::sun::star::awt::RasterOperation eROP
) throw(::com::sun::star::uno::RuntimeException
)
219 SolarMutexGuard aGuard
;
221 meRasterOp
= (RasterOp
)eROP
;
224 void VCLXGraphics::setClipRegion( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XRegion
>& rxRegion
) throw(::com::sun::star::uno::RuntimeException
)
226 SolarMutexGuard aGuard
;
230 mpClipRegion
= new Region( VCLUnoHelper::GetRegion( rxRegion
) );
235 void VCLXGraphics::intersectClipRegion( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XRegion
>& rxRegion
) throw(::com::sun::star::uno::RuntimeException
)
237 SolarMutexGuard aGuard
;
241 Region
aRegion( VCLUnoHelper::GetRegion( rxRegion
) );
243 mpClipRegion
= new Region( aRegion
);
245 mpClipRegion
->Intersect( aRegion
);
249 void VCLXGraphics::push( ) throw(::com::sun::star::uno::RuntimeException
)
251 SolarMutexGuard aGuard
;
255 mpOutputDevice
->Push();
258 void VCLXGraphics::pop( ) throw(::com::sun::star::uno::RuntimeException
)
260 SolarMutexGuard aGuard
;
264 mpOutputDevice
->Pop();
267 void VCLXGraphics::copy( const ::com::sun::star::uno::Reference
< ::com::sun::star::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(::com::sun::star::uno::RuntimeException
)
269 SolarMutexGuard aGuard
;
271 if ( mpOutputDevice
)
273 VCLXDevice
* pFromDev
= VCLXDevice::GetImplementation( rxSource
);
274 DBG_ASSERT( pFromDev
, "VCLXGraphics::copy - invalid device" );
277 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
);
278 mpOutputDevice
->DrawOutDev( Point( nDestX
, nDestY
), Size( nDestWidth
, nDestHeight
),
279 Point( nSourceX
, nSourceY
), Size( nSourceWidth
, nSourceHeight
), *pFromDev
->GetOutputDevice() );
284 void VCLXGraphics::draw( const ::com::sun::star::uno::Reference
< ::com::sun::star::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(::com::sun::star::uno::RuntimeException
)
286 SolarMutexGuard aGuard
;
290 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
);
291 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
> xBitmap( rxBitmapHandle
, ::com::sun::star::uno::UNO_QUERY
);
292 BitmapEx aBmpEx
= VCLUnoHelper::GetBitmap( xBitmap
);
294 Point
aPos(nDestX
- nSourceX
, nDestY
- nSourceY
);
295 Size aSz
= aBmpEx
.GetSizePixel();
297 if(nDestWidth
!= nSourceWidth
)
299 float zoomX
= (float)nDestWidth
/ (float)nSourceWidth
;
300 aSz
.Width() = (long) ((float)aSz
.Width() * zoomX
);
303 if(nDestHeight
!= nSourceHeight
)
305 float zoomY
= (float)nDestHeight
/ (float)nSourceHeight
;
306 aSz
.Height() = (long) ((float)aSz
.Height() * zoomY
);
309 if(nSourceX
|| nSourceY
|| aSz
.Width() != nSourceWidth
|| aSz
.Height() != nSourceHeight
)
310 mpOutputDevice
->IntersectClipRegion(Region(Rectangle(nDestX
, nDestY
, nDestX
+ nDestWidth
- 1, nDestY
+ nDestHeight
- 1)));
312 mpOutputDevice
->DrawBitmapEx( aPos
, aSz
, aBmpEx
);
316 void VCLXGraphics::drawPixel( sal_Int32 x
, sal_Int32 y
) throw(::com::sun::star::uno::RuntimeException
)
318 SolarMutexGuard aGuard
;
322 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
323 mpOutputDevice
->DrawPixel( Point( x
, y
) );
327 void VCLXGraphics::drawLine( sal_Int32 x1
, sal_Int32 y1
, sal_Int32 x2
, sal_Int32 y2
) throw(::com::sun::star::uno::RuntimeException
)
329 SolarMutexGuard aGuard
;
333 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
334 mpOutputDevice
->DrawLine( Point( x1
, y1
), Point( x2
, y2
) );
338 void VCLXGraphics::drawRect( sal_Int32 x
, sal_Int32 y
, sal_Int32 width
, sal_Int32 height
) throw(::com::sun::star::uno::RuntimeException
)
340 SolarMutexGuard aGuard
;
344 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
345 mpOutputDevice
->DrawRect( Rectangle( Point( x
, y
), Size( width
, height
) ) );
349 void VCLXGraphics::drawRoundedRect( sal_Int32 x
, sal_Int32 y
, sal_Int32 width
, sal_Int32 height
, sal_Int32 nHorzRound
, sal_Int32 nVertRound
) throw(::com::sun::star::uno::RuntimeException
)
351 SolarMutexGuard aGuard
;
355 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
356 mpOutputDevice
->DrawRect( Rectangle( Point( x
, y
), Size( width
, height
) ), nHorzRound
, nVertRound
);
360 void VCLXGraphics::drawPolyLine( const ::com::sun::star::uno::Sequence
< sal_Int32
>& DataX
, const ::com::sun::star::uno::Sequence
< sal_Int32
>& DataY
) throw(::com::sun::star::uno::RuntimeException
)
362 SolarMutexGuard aGuard
;
366 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
367 mpOutputDevice
->DrawPolyLine( VCLUnoHelper::CreatePolygon( DataX
, DataY
) );
371 void VCLXGraphics::drawPolygon( const ::com::sun::star::uno::Sequence
< sal_Int32
>& DataX
, const ::com::sun::star::uno::Sequence
< sal_Int32
>& DataY
) throw(::com::sun::star::uno::RuntimeException
)
373 SolarMutexGuard aGuard
;
377 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
378 mpOutputDevice
->DrawPolygon( VCLUnoHelper::CreatePolygon( DataX
, DataY
) );
382 void VCLXGraphics::drawPolyPolygon( const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< sal_Int32
> >& DataX
, const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< sal_Int32
> >& DataY
) throw(::com::sun::star::uno::RuntimeException
)
384 SolarMutexGuard aGuard
;
388 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
389 sal_uInt16 nPolys
= (sal_uInt16
) DataX
.getLength();
390 PolyPolygon
aPolyPoly( nPolys
);
391 for ( sal_uInt16 n
= 0; n
< nPolys
; n
++ )
392 aPolyPoly
[n
] = VCLUnoHelper::CreatePolygon( DataX
.getConstArray()[n
], DataY
.getConstArray()[n
] );
394 mpOutputDevice
->DrawPolyPolygon( aPolyPoly
);
398 void VCLXGraphics::drawEllipse( sal_Int32 x
, sal_Int32 y
, sal_Int32 width
, sal_Int32 height
) throw(::com::sun::star::uno::RuntimeException
)
400 SolarMutexGuard aGuard
;
404 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
405 mpOutputDevice
->DrawEllipse( Rectangle( Point( x
, y
), Size( width
, height
) ) );
409 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(::com::sun::star::uno::RuntimeException
)
411 SolarMutexGuard aGuard
;
415 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
416 mpOutputDevice
->DrawArc( Rectangle( Point( x
, y
), Size( width
, height
) ), Point( x1
, y1
), Point( x2
, y2
) );
420 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(::com::sun::star::uno::RuntimeException
)
422 SolarMutexGuard aGuard
;
426 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
427 mpOutputDevice
->DrawPie( Rectangle( Point( x
, y
), Size( width
, height
) ), Point( x1
, y1
), Point( x2
, y2
) );
431 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(::com::sun::star::uno::RuntimeException
)
433 SolarMutexGuard aGuard
;
437 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
438 mpOutputDevice
->DrawChord( Rectangle( Point( x
, y
), Size( width
, height
) ), Point( x1
, y1
), Point( x2
, y2
) );
442 void VCLXGraphics::drawGradient( sal_Int32 x
, sal_Int32 y
, sal_Int32 width
, sal_Int32 height
, const ::com::sun::star::awt::Gradient
& rGradient
) throw(::com::sun::star::uno::RuntimeException
)
444 SolarMutexGuard aGuard
;
448 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
449 Gradient
aGradient((GradientStyle
)rGradient
.Style
, rGradient
.StartColor
, rGradient
.EndColor
);
450 aGradient
.SetAngle(rGradient
.Angle
);
451 aGradient
.SetBorder(rGradient
.Border
);
452 aGradient
.SetOfsX(rGradient
.XOffset
);
453 aGradient
.SetOfsY(rGradient
.YOffset
);
454 aGradient
.SetStartIntensity(rGradient
.StartIntensity
);
455 aGradient
.SetEndIntensity(rGradient
.EndIntensity
);
456 aGradient
.SetSteps(rGradient
.StepCount
);
457 mpOutputDevice
->DrawGradient( Rectangle( Point( x
, y
), Size( width
, height
) ), aGradient
);
461 void VCLXGraphics::drawText( sal_Int32 x
, sal_Int32 y
, const ::rtl::OUString
& rText
) throw(::com::sun::star::uno::RuntimeException
)
463 SolarMutexGuard aGuard
;
467 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
|INITOUTDEV_FONT
);
468 mpOutputDevice
->DrawText( Point( x
, y
), rText
);
472 void VCLXGraphics::drawTextArray( sal_Int32 x
, sal_Int32 y
, const ::rtl::OUString
& rText
, const ::com::sun::star::uno::Sequence
< sal_Int32
>& rLongs
) throw(::com::sun::star::uno::RuntimeException
)
474 SolarMutexGuard aGuard
;
478 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
|INITOUTDEV_FONT
);
479 mpOutputDevice
->DrawTextArray( Point( x
, y
), rText
, rLongs
.getConstArray() );
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */