1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxgraphics.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
34 #include <toolkit/awt/vclxgraphics.hxx>
35 #include <toolkit/awt/vclxdevice.hxx>
36 #include <toolkit/helper/macros.hxx>
37 #include <toolkit/helper/vclunohelper.hxx>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <rtl/memory.h>
42 #include <vcl/svapp.hxx>
43 #include <vcl/outdev.hxx>
44 #include <vcl/gradient.hxx>
45 #include <tools/debug.hxx>
48 // ----------------------------------------------------
50 // ----------------------------------------------------
52 // ::com::sun::star::uno::XInterface
53 ::com::sun::star::uno::Any
VCLXGraphics::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
55 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
56 SAL_STATIC_CAST( ::com::sun::star::awt::XGraphics
*, this ),
57 SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider
*, this ),
58 SAL_STATIC_CAST( ::com::sun::star::lang::XUnoTunnel
*, this ) );
59 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
62 // ::com::sun::star::lang::XUnoTunnel
63 IMPL_XUNOTUNNEL( VCLXGraphics
)
65 // ::com::sun::star::lang::XTypeProvider
66 IMPL_XTYPEPROVIDER_START( VCLXGraphics
)
67 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
>* ) NULL
)
68 IMPL_XTYPEPROVIDER_END
70 VCLXGraphics::VCLXGraphics() : mrMutex( Application::GetSolarMutex() )
72 mpOutputDevice
= NULL
;
76 VCLXGraphics::~VCLXGraphics()
78 List
* pLst
= mpOutputDevice
? mpOutputDevice
->GetUnoGraphicsList() : NULL
;
85 void VCLXGraphics::SetOutputDevice( OutputDevice
* pOutDev
)
87 mpOutputDevice
= pOutDev
;
91 void VCLXGraphics::Init( OutputDevice
* pOutDev
)
93 DBG_ASSERT( !mpOutputDevice
, "VCLXGraphics::Init allready has pOutDev !" );
94 mpOutputDevice
= pOutDev
;
96 maFont
= mpOutputDevice
->GetFont();
97 maTextColor
= COL_BLACK
;
98 maTextFillColor
= COL_TRANSPARENT
;
99 maLineColor
= COL_BLACK
;
100 maFillColor
= COL_WHITE
;
101 meRasterOp
= ROP_OVERPAINT
;
104 // Register at OutputDevice
105 List
* pLst
= mpOutputDevice
->GetUnoGraphicsList();
107 pLst
= mpOutputDevice
->CreateUnoGraphicsList();
108 pLst
->Insert( this, LIST_APPEND
);
111 void VCLXGraphics::InitOutputDevice( sal_uInt16 nFlags
)
115 NAMESPACE_VOS(OGuard
) aVclGuard( Application::GetSolarMutex() );
117 if ( nFlags
& INITOUTDEV_FONT
)
119 mpOutputDevice
->SetFont( maFont
);
120 mpOutputDevice
->SetTextColor( maTextColor
);
121 mpOutputDevice
->SetTextFillColor( maTextFillColor
);
124 if ( nFlags
& INITOUTDEV_COLORS
)
126 mpOutputDevice
->SetLineColor( maLineColor
);
127 mpOutputDevice
->SetFillColor( maFillColor
);
130 if ( nFlags
& INITOUTDEV_RASTEROP
)
132 mpOutputDevice
->SetRasterOp( meRasterOp
);
135 if ( nFlags
& INITOUTDEV_CLIPREGION
)
138 mpOutputDevice
->SetClipRegion( *mpClipRegion
);
140 mpOutputDevice
->SetClipRegion();
145 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXGraphics::getDevice() throw(::com::sun::star::uno::RuntimeException
)
147 ::vos::OGuard
aGuard( GetMutex() );
149 if( !mxDevice
.is() && mpOutputDevice
)
151 VCLXDevice
* pDev
= new VCLXDevice
;
152 pDev
->SetOutputDevice( mpOutputDevice
);
158 ::com::sun::star::awt::SimpleFontMetric
VCLXGraphics::getFontMetric() throw(::com::sun::star::uno::RuntimeException
)
160 ::vos::OGuard
aGuard( GetMutex() );
162 ::com::sun::star::awt::SimpleFontMetric aM
;
165 mpOutputDevice
->SetFont( maFont
);
166 aM
= VCLUnoHelper::CreateFontMetric( mpOutputDevice
->GetFontMetric() );
171 void VCLXGraphics::setFont( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XFont
>& rxFont
) throw(::com::sun::star::uno::RuntimeException
)
173 ::vos::OGuard
aGuard( GetMutex() );
175 maFont
= VCLUnoHelper::CreateFont( rxFont
);
178 void VCLXGraphics::selectFont( const ::com::sun::star::awt::FontDescriptor
& rDescription
) throw(::com::sun::star::uno::RuntimeException
)
180 ::vos::OGuard
aGuard( GetMutex() );
182 maFont
= VCLUnoHelper::CreateFont( rDescription
, Font() );
185 void VCLXGraphics::setTextColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
187 ::vos::OGuard
aGuard( GetMutex() );
189 maTextColor
= Color( (sal_uInt32
)nColor
);
192 void VCLXGraphics::setTextFillColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
194 ::vos::OGuard
aGuard( GetMutex() );
196 maTextFillColor
= Color( (sal_uInt32
)nColor
);
199 void VCLXGraphics::setLineColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
201 ::vos::OGuard
aGuard( GetMutex() );
203 maLineColor
= Color( (sal_uInt32
)nColor
);
206 void VCLXGraphics::setFillColor( sal_Int32 nColor
) throw(::com::sun::star::uno::RuntimeException
)
208 ::vos::OGuard
aGuard( GetMutex() );
210 maFillColor
= Color( (sal_uInt32
)nColor
);
213 void VCLXGraphics::setRasterOp( ::com::sun::star::awt::RasterOperation eROP
) throw(::com::sun::star::uno::RuntimeException
)
215 ::vos::OGuard
aGuard( GetMutex() );
217 meRasterOp
= (RasterOp
)eROP
;
220 void VCLXGraphics::setClipRegion( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XRegion
>& rxRegion
) throw(::com::sun::star::uno::RuntimeException
)
222 ::vos::OGuard
aGuard( GetMutex() );
226 mpClipRegion
= new Region( VCLUnoHelper::GetRegion( rxRegion
) );
231 void VCLXGraphics::intersectClipRegion( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XRegion
>& rxRegion
) throw(::com::sun::star::uno::RuntimeException
)
233 ::vos::OGuard
aGuard( GetMutex() );
237 Region
aRegion( VCLUnoHelper::GetRegion( rxRegion
) );
239 mpClipRegion
= new Region( aRegion
);
241 mpClipRegion
->Intersect( aRegion
);
245 void VCLXGraphics::push( ) throw(::com::sun::star::uno::RuntimeException
)
247 ::vos::OGuard
aGuard( GetMutex() );
251 mpOutputDevice
->Push();
254 void VCLXGraphics::pop( ) throw(::com::sun::star::uno::RuntimeException
)
256 ::vos::OGuard
aGuard( GetMutex() );
260 mpOutputDevice
->Pop();
263 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
)
265 ::vos::OGuard
aGuard( GetMutex() );
267 if ( mpOutputDevice
)
269 VCLXDevice
* pFromDev
= VCLXDevice::GetImplementation( rxSource
);
270 DBG_ASSERT( pFromDev
, "VCLXGraphics::copy - invalid device" );
273 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
);
274 mpOutputDevice
->DrawOutDev( Point( nDestX
, nDestY
), Size( nDestWidth
, nDestHeight
),
275 Point( nSourceX
, nSourceY
), Size( nSourceWidth
, nSourceHeight
), *pFromDev
->GetOutputDevice() );
280 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
)
282 ::vos::OGuard
aGuard( GetMutex() );
286 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
);
287 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
> xBitmap( rxBitmapHandle
, ::com::sun::star::uno::UNO_QUERY
);
288 BitmapEx aBmpEx
= VCLUnoHelper::GetBitmap( xBitmap
);
290 Point
aPos(nDestX
- nSourceX
, nDestY
- nSourceY
);
291 Size aSz
= aBmpEx
.GetSizePixel();
293 if(nDestWidth
!= nSourceWidth
)
295 float zoomX
= (float)nDestWidth
/ (float)nSourceWidth
;
296 aSz
.Width() = (long) ((float)aSz
.Width() * zoomX
);
299 if(nDestHeight
!= nSourceHeight
)
301 float zoomY
= (float)nDestHeight
/ (float)nSourceHeight
;
302 aSz
.Height() = (long) ((float)aSz
.Height() * zoomY
);
305 if(nSourceX
|| nSourceY
|| aSz
.Width() != nSourceWidth
|| aSz
.Height() != nSourceHeight
)
306 mpOutputDevice
->IntersectClipRegion(Region(Rectangle(nDestX
, nDestY
, nDestX
+ nDestWidth
- 1, nDestY
+ nDestHeight
- 1)));
308 mpOutputDevice
->DrawBitmapEx( aPos
, aSz
, aBmpEx
);
312 void VCLXGraphics::drawPixel( sal_Int32 x
, sal_Int32 y
) throw(::com::sun::star::uno::RuntimeException
)
314 ::vos::OGuard
aGuard( GetMutex() );
318 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
319 mpOutputDevice
->DrawPixel( Point( x
, y
) );
323 void VCLXGraphics::drawLine( sal_Int32 x1
, sal_Int32 y1
, sal_Int32 x2
, sal_Int32 y2
) throw(::com::sun::star::uno::RuntimeException
)
325 ::vos::OGuard
aGuard( GetMutex() );
329 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
330 mpOutputDevice
->DrawLine( Point( x1
, y1
), Point( x2
, y2
) );
334 void VCLXGraphics::drawRect( sal_Int32 x
, sal_Int32 y
, sal_Int32 width
, sal_Int32 height
) throw(::com::sun::star::uno::RuntimeException
)
336 ::vos::OGuard
aGuard( GetMutex() );
340 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
341 mpOutputDevice
->DrawRect( Rectangle( Point( x
, y
), Size( width
, height
) ) );
345 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
)
347 ::vos::OGuard
aGuard( GetMutex() );
351 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
352 mpOutputDevice
->DrawRect( Rectangle( Point( x
, y
), Size( width
, height
) ), nHorzRound
, nVertRound
);
356 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
)
358 ::vos::OGuard
aGuard( GetMutex() );
362 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
363 mpOutputDevice
->DrawPolyLine( VCLUnoHelper::CreatePolygon( DataX
, DataY
) );
367 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
)
369 ::vos::OGuard
aGuard( GetMutex() );
373 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
374 mpOutputDevice
->DrawPolygon( VCLUnoHelper::CreatePolygon( DataX
, DataY
) );
378 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
)
380 ::vos::OGuard
aGuard( GetMutex() );
384 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
385 sal_uInt16 nPolys
= (sal_uInt16
) DataX
.getLength();
386 PolyPolygon
aPolyPoly( nPolys
);
387 for ( sal_uInt16 n
= 0; n
< nPolys
; n
++ )
388 aPolyPoly
[n
] = VCLUnoHelper::CreatePolygon( DataX
.getConstArray()[n
], DataY
.getConstArray()[n
] );
390 mpOutputDevice
->DrawPolyPolygon( aPolyPoly
);
394 void VCLXGraphics::drawEllipse( sal_Int32 x
, sal_Int32 y
, sal_Int32 width
, sal_Int32 height
) throw(::com::sun::star::uno::RuntimeException
)
396 ::vos::OGuard
aGuard( GetMutex() );
400 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
401 mpOutputDevice
->DrawEllipse( Rectangle( Point( x
, y
), Size( width
, height
) ) );
405 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
)
407 ::vos::OGuard
aGuard( GetMutex() );
411 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
412 mpOutputDevice
->DrawArc( Rectangle( Point( x
, y
), Size( width
, height
) ), Point( x1
, y1
), Point( x2
, y2
) );
416 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
)
418 ::vos::OGuard
aGuard( GetMutex() );
422 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
423 mpOutputDevice
->DrawPie( Rectangle( Point( x
, y
), Size( width
, height
) ), Point( x1
, y1
), Point( x2
, y2
) );
427 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
)
429 ::vos::OGuard
aGuard( GetMutex() );
433 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
434 mpOutputDevice
->DrawChord( Rectangle( Point( x
, y
), Size( width
, height
) ), Point( x1
, y1
), Point( x2
, y2
) );
438 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
)
440 ::vos::OGuard
aGuard( GetMutex() );
444 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
);
445 Gradient
aGradient((GradientStyle
)rGradient
.Style
, rGradient
.StartColor
, rGradient
.EndColor
);
446 aGradient
.SetAngle(rGradient
.Angle
);
447 aGradient
.SetBorder(rGradient
.Border
);
448 aGradient
.SetOfsX(rGradient
.XOffset
);
449 aGradient
.SetOfsY(rGradient
.YOffset
);
450 aGradient
.SetStartIntensity(rGradient
.StartIntensity
);
451 aGradient
.SetEndIntensity(rGradient
.EndIntensity
);
452 aGradient
.SetSteps(rGradient
.StepCount
);
453 mpOutputDevice
->DrawGradient( Rectangle( Point( x
, y
), Size( width
, height
) ), aGradient
);
457 void VCLXGraphics::drawText( sal_Int32 x
, sal_Int32 y
, const ::rtl::OUString
& rText
) throw(::com::sun::star::uno::RuntimeException
)
459 ::vos::OGuard
aGuard( GetMutex() );
463 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
|INITOUTDEV_FONT
);
464 mpOutputDevice
->DrawText( Point( x
, y
), rText
);
468 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
)
470 ::vos::OGuard
aGuard( GetMutex() );
474 InitOutputDevice( INITOUTDEV_CLIPREGION
|INITOUTDEV_RASTEROP
|INITOUTDEV_COLORS
|INITOUTDEV_FONT
);
475 mpOutputDevice
->DrawTextArray( Point( x
, y
), rText
, rLongs
.getConstArray() );