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: vclxdevice.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"
33 #include <com/sun/star/awt/DeviceCapability.hpp>
35 #include <com/sun/star/util/MeasureUnit.hpp>
37 #include <toolkit/awt/vclxdevice.hxx>
38 #include <toolkit/awt/vclxfont.hxx>
39 #include <toolkit/awt/vclxbitmap.hxx>
40 #include <toolkit/helper/vclunohelper.hxx>
41 #include <toolkit/helper/macros.hxx>
42 #include <cppuhelper/typeprovider.hxx>
44 #include <rtl/memory.h>
47 #include <vcl/svapp.hxx>
48 #include <vcl/outdev.hxx>
49 #include <vcl/window.hxx>
50 #include <vcl/print.hxx>
51 #include <vcl/virdev.hxx>
52 #include <vcl/bitmapex.hxx>
53 #include <vcl/font.hxx>
55 // ----------------------------------------------------
57 // ----------------------------------------------------
58 VCLXDevice::VCLXDevice() : mrMutex( Application::GetSolarMutex() )
60 mpOutputDevice
= NULL
;
64 VCLXDevice::~VCLXDevice()
66 // Was thought for #88347#, but didn't help, because the interface will not be released
67 // But would be a good idea anyway, check after 6.0, it's a little bit dangerous now
68 // if( mpOutputDevice && IsCreatedWithToolkit() )
70 // delete mpOutputDevice;
74 void VCLXDevice::DestroyOutputDevice()
76 delete mpOutputDevice
;
77 mpOutputDevice
= NULL
;
80 void VCLXDevice::SetCreatedWithToolkit( sal_Bool bCreatedWithToolkit
)
82 if ( bCreatedWithToolkit
)
83 nFlags
|= FLAGS_CREATEDWITHTOOLKIT
;
85 nFlags
&= ~FLAGS_CREATEDWITHTOOLKIT
;
88 sal_Bool
VCLXDevice::IsCreatedWithToolkit() const
90 return ( nFlags
& FLAGS_CREATEDWITHTOOLKIT
) != 0;
93 // ::com::sun::star::uno::XInterface
94 ::com::sun::star::uno::Any
VCLXDevice::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
96 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
97 SAL_STATIC_CAST( ::com::sun::star::awt::XDevice
*, this ),
98 SAL_STATIC_CAST( ::com::sun::star::lang::XUnoTunnel
*, this ),
99 SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider
*, this ),
100 SAL_STATIC_CAST( ::com::sun::star::awt::XUnitConversion
*, this ) );
101 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
104 // ::com::sun::star::lang::XUnoTunnel
105 IMPL_XUNOTUNNEL( VCLXDevice
)
107 // ::com::sun::star::lang::XTypeProvider
108 IMPL_XTYPEPROVIDER_START( VCLXDevice
)
109 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
>* ) NULL
),
110 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XUnitConversion
>* ) NULL
)
111 IMPL_XTYPEPROVIDER_END
114 // ::com::sun::star::awt::XDevice,
115 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
> VCLXDevice::createGraphics( ) throw(::com::sun::star::uno::RuntimeException
)
117 ::vos::OGuard
aGuard( GetMutex() );
119 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
> xRef
;
121 if ( mpOutputDevice
)
122 xRef
= mpOutputDevice
->CreateUnoGraphics();
127 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXDevice::createDevice( sal_Int32 nWidth
, sal_Int32 nHeight
) throw(::com::sun::star::uno::RuntimeException
)
129 ::vos::OGuard
aGuard( GetMutex() );
131 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> xRef
;
132 if ( GetOutputDevice() )
134 VCLXVirtualDevice
* pVDev
= new VCLXVirtualDevice
;
135 VirtualDevice
* pVclVDev
= new VirtualDevice( *GetOutputDevice() );
136 pVclVDev
->SetOutputSizePixel( Size( nWidth
, nHeight
) );
137 pVDev
->SetVirtualDevice( pVclVDev
);
143 ::com::sun::star::awt::DeviceInfo
VCLXDevice::getInfo() throw(::com::sun::star::uno::RuntimeException
)
145 ::vos::OGuard
aGuard( GetMutex() );
147 ::com::sun::star::awt::DeviceInfo aInfo
;
152 OutDevType eDevType
= mpOutputDevice
->GetOutDevType();
153 if ( eDevType
== OUTDEV_WINDOW
)
155 aDevSz
= ((Window
*)mpOutputDevice
)->GetSizePixel();
156 ((Window
*)mpOutputDevice
)->GetBorder( aInfo
.LeftInset
, aInfo
.TopInset
, aInfo
.RightInset
, aInfo
.BottomInset
);
158 else if ( eDevType
== OUTDEV_PRINTER
)
160 aDevSz
= ((Printer
*)mpOutputDevice
)->GetPaperSizePixel();
161 Size aOutSz
= mpOutputDevice
->GetOutputSizePixel();
162 Point aOffset
= ((Printer
*)mpOutputDevice
)->GetPageOffset();
163 aInfo
.LeftInset
= aOffset
.X();
164 aInfo
.TopInset
= aOffset
.Y();
165 aInfo
.RightInset
= aDevSz
.Width() - aOutSz
.Width() - aOffset
.X();
166 aInfo
.BottomInset
= aDevSz
.Height() - aOutSz
.Height() - aOffset
.Y();
168 else // VirtualDevice
170 aDevSz
= mpOutputDevice
->GetOutputSizePixel();
173 aInfo
.RightInset
= 0;
174 aInfo
.BottomInset
= 0;
177 aInfo
.Width
= aDevSz
.Width();
178 aInfo
.Height
= aDevSz
.Height();
180 Size aTmpSz
= mpOutputDevice
->LogicToPixel( Size( 1000, 1000 ), MapMode( MAP_CM
) );
181 aInfo
.PixelPerMeterX
= aTmpSz
.Width()/10;
182 aInfo
.PixelPerMeterY
= aTmpSz
.Height()/10;
184 aInfo
.BitsPerPixel
= mpOutputDevice
->GetBitCount();
186 aInfo
.Capabilities
= 0;
187 if ( mpOutputDevice
->GetOutDevType() != OUTDEV_PRINTER
)
188 aInfo
.Capabilities
= ::com::sun::star::awt::DeviceCapability::RASTEROPERATIONS
|::com::sun::star::awt::DeviceCapability::GETBITS
;
194 ::com::sun::star::uno::Sequence
< ::com::sun::star::awt::FontDescriptor
> VCLXDevice::getFontDescriptors( ) throw(::com::sun::star::uno::RuntimeException
)
196 ::vos::OGuard
aGuard( GetMutex() );
198 ::com::sun::star::uno::Sequence
< ::com::sun::star::awt::FontDescriptor
> aFonts
;
201 int nFonts
= mpOutputDevice
->GetDevFontCount();
204 aFonts
= ::com::sun::star::uno::Sequence
< ::com::sun::star::awt::FontDescriptor
>( nFonts
);
205 ::com::sun::star::awt::FontDescriptor
* pFonts
= aFonts
.getArray();
206 for ( int n
= 0; n
< nFonts
; n
++ )
207 pFonts
[n
] = VCLUnoHelper::CreateFontDescriptor( mpOutputDevice
->GetDevFont( n
) );
213 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XFont
> VCLXDevice::getFont( const ::com::sun::star::awt::FontDescriptor
& rDescriptor
) throw(::com::sun::star::uno::RuntimeException
)
215 ::vos::OGuard
aGuard( GetMutex() );
217 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XFont
> xRef
;
220 VCLXFont
* pMetric
= new VCLXFont
;
221 pMetric
->Init( *this, VCLUnoHelper::CreateFont( rDescriptor
, mpOutputDevice
->GetFont() ) );
227 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
> VCLXDevice::createBitmap( sal_Int32 nX
, sal_Int32 nY
, sal_Int32 nWidth
, sal_Int32 nHeight
) throw(::com::sun::star::uno::RuntimeException
)
229 ::vos::OGuard
aGuard( GetMutex() );
231 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
> xBmp
;
234 Bitmap aBmp
= mpOutputDevice
->GetBitmap( Point( nX
, nY
), Size( nWidth
, nHeight
) );
236 VCLXBitmap
* pBmp
= new VCLXBitmap
;
237 pBmp
->SetBitmap( BitmapEx( aBmp
) );
243 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDisplayBitmap
> VCLXDevice::createDisplayBitmap( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
>& rxBitmap
) throw(::com::sun::star::uno::RuntimeException
)
245 ::vos::OGuard
aGuard( GetMutex() );
247 BitmapEx aBmp
= VCLUnoHelper::GetBitmap( rxBitmap
);
248 VCLXBitmap
* pBmp
= new VCLXBitmap
;
249 pBmp
->SetBitmap( aBmp
);
250 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDisplayBitmap
> xDBmp
= pBmp
;
255 VCLXVirtualDevice::~VCLXVirtualDevice()
257 ::vos::OGuard
aGuard( GetMutex() );
259 DestroyOutputDevice();
263 // -----------------------------------------------------------------------------
264 // ::com::sun::star::awt::XTextConstraints
265 // -----------------------------------------------------------------------------
266 // ::sal_Int32 SAL_CALL VCLXDevice::getTextWidth( const ::rtl::OUString& Text ) throw (::com::sun::star::uno::RuntimeException)
268 // ::vos::OGuard aGuard( GetMutex() );
269 // if (Text.getLength() == 0)
277 // ::sal_Int32 SAL_CALL VCLXDevice::getTextHeight( ) throw (::com::sun::star::uno::RuntimeException)
279 // ::vos::OGuard aGuard( GetMutex() );
284 // -----------------------------------------------------------------------------
285 // Interface implementation of ::com::sun::star::awt::XUnitConversion
286 // -----------------------------------------------------------------------------
288 ::com::sun::star::awt::Point SAL_CALL
VCLXDevice::convertPointToLogic( const ::com::sun::star::awt::Point
& aPoint
, ::sal_Int16 TargetUnit
) throw (::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
)
291 ::vos::OGuard
aGuard( GetMutex() );
292 if (TargetUnit
== com::sun::star::util::MeasureUnit::PERCENT
)
294 // percentage not allowed here
295 throw ::com::sun::star::lang::IllegalArgumentException();
298 ::com::sun::star::awt::Point
aAWTPoint(0,0);
303 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit
));
304 ::Point aVCLPoint
= VCLUnoHelper::ConvertToVCLPoint(aPoint
);
305 ::Point aDevPoint
= mpOutputDevice
->PixelToLogic(aVCLPoint
, aMode
);
306 aAWTPoint
= VCLUnoHelper::ConvertToAWTPoint(aDevPoint
);
313 ::com::sun::star::awt::Point SAL_CALL
VCLXDevice::convertPointToPixel( const ::com::sun::star::awt::Point
& aPoint
, ::sal_Int16 SourceUnit
) throw (::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
)
316 ::vos::OGuard
aGuard( GetMutex() );
317 if (SourceUnit
== com::sun::star::util::MeasureUnit::PERCENT
||
318 SourceUnit
== com::sun::star::util::MeasureUnit::PIXEL
)
320 // pixel or percentage not allowed here
321 throw ::com::sun::star::lang::IllegalArgumentException();
324 ::com::sun::star::awt::Point
aAWTPoint(0,0);
328 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit
));
329 ::Point aVCLPoint
= VCLUnoHelper::ConvertToVCLPoint(aPoint
);
330 ::Point aDevPoint
= mpOutputDevice
->LogicToPixel(aVCLPoint
, aMode
);
331 aAWTPoint
= VCLUnoHelper::ConvertToAWTPoint(aDevPoint
);
337 ::com::sun::star::awt::Size SAL_CALL
VCLXDevice::convertSizeToLogic( const ::com::sun::star::awt::Size
& aSize
, ::sal_Int16 TargetUnit
) throw (::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
)
340 ::vos::OGuard
aGuard( GetMutex() );
341 if (TargetUnit
== com::sun::star::util::MeasureUnit::PERCENT
)
343 // percentage not allowed here
344 throw ::com::sun::star::lang::IllegalArgumentException();
347 ::com::sun::star::awt::Size
aAWTSize(0,0);
353 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit
));
354 ::Size aVCLSize
= VCLUnoHelper::ConvertToVCLSize(aSize
);
355 ::Size aDevSz
= mpOutputDevice
->PixelToLogic(aVCLSize
, aMode
);
356 aAWTSize
= VCLUnoHelper::ConvertToAWTSize(aDevSz
);
362 ::com::sun::star::awt::Size SAL_CALL
VCLXDevice::convertSizeToPixel( const ::com::sun::star::awt::Size
& aSize
, ::sal_Int16 SourceUnit
) throw (::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
)
365 ::vos::OGuard
aGuard( GetMutex() );
366 if (SourceUnit
== com::sun::star::util::MeasureUnit::PERCENT
||
367 SourceUnit
== com::sun::star::util::MeasureUnit::PIXEL
)
369 // pixel or percentage not allowed here
370 throw ::com::sun::star::lang::IllegalArgumentException();
373 ::com::sun::star::awt::Size
aAWTSize(0,0);
377 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit
));
378 ::Size aVCLSize
= VCLUnoHelper::ConvertToVCLSize(aSize
);
379 ::Size aDevSz
= mpOutputDevice
->LogicToPixel(aVCLSize
, aMode
);
380 aAWTSize
= VCLUnoHelper::ConvertToAWTSize(aDevSz
);