merge the formfield patch from ooo-build
[ooovba.git] / toolkit / source / awt / vclxdevice.cxx
blobdbf5e36e322ec84d6927c9522c89823601f1f015
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxdevice.cxx,v $
10 * $Revision: 1.8 $
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>
45 #include <rtl/uuid.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 // ----------------------------------------------------
56 // class VCLXDevice
57 // ----------------------------------------------------
58 VCLXDevice::VCLXDevice() : mrMutex( Application::GetSolarMutex() )
60 mpOutputDevice = NULL;
61 nFlags = 0;
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() )
69 // {
70 // delete mpOutputDevice;
71 // }
74 void VCLXDevice::DestroyOutputDevice()
76 delete mpOutputDevice;
77 mpOutputDevice = NULL;
80 void VCLXDevice::SetCreatedWithToolkit( sal_Bool bCreatedWithToolkit )
82 if ( bCreatedWithToolkit )
83 nFlags |= FLAGS_CREATEDWITHTOOLKIT;
84 else
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();
124 return xRef;
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 );
138 xRef = pVDev;
140 return xRef;
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;
149 if( mpOutputDevice )
151 Size aDevSz;
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();
171 aInfo.LeftInset = 0;
172 aInfo.TopInset = 0;
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;
191 return aInfo;
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;
199 if( mpOutputDevice )
201 int nFonts = mpOutputDevice->GetDevFontCount();
202 if ( nFonts )
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 ) );
210 return aFonts;
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;
218 if( mpOutputDevice )
220 VCLXFont* pMetric = new VCLXFont;
221 pMetric->Init( *this, VCLUnoHelper::CreateFont( rDescriptor, mpOutputDevice->GetFont() ) );
222 xRef = pMetric;
224 return xRef;
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;
232 if( mpOutputDevice )
234 Bitmap aBmp = mpOutputDevice->GetBitmap( Point( nX, nY ), Size( nWidth, nHeight ) );
236 VCLXBitmap* pBmp = new VCLXBitmap;
237 pBmp->SetBitmap( BitmapEx( aBmp ) );
238 xBmp = pBmp;
240 return xBmp;
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;
251 return xDBmp;
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)
267 // {
268 // ::vos::OGuard aGuard( GetMutex() );
269 // if (Text.getLength() == 0)
270 // {
271 // return 0;
272 // }
274 // return 1;
275 // }
277 // ::sal_Int32 SAL_CALL VCLXDevice::getTextHeight( ) throw (::com::sun::star::uno::RuntimeException)
278 // {
279 // ::vos::OGuard aGuard( GetMutex() );
280 // return 1;
281 // }
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)
290 (void)aPoint;
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);
299 // X,Y
301 if( mpOutputDevice )
303 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
304 ::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
305 ::Point aDevPoint = mpOutputDevice->PixelToLogic(aVCLPoint, aMode );
306 aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
309 return aAWTPoint;
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)
315 (void)aPoint;
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);
326 if( mpOutputDevice )
328 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
329 ::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
330 ::Point aDevPoint = mpOutputDevice->LogicToPixel(aVCLPoint, aMode );
331 aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
334 return aAWTPoint;
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)
339 (void)aSize;
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);
348 // Width, Height
351 if( mpOutputDevice )
353 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
354 ::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
355 ::Size aDevSz = mpOutputDevice->PixelToLogic(aVCLSize, aMode );
356 aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
359 return aAWTSize;
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)
364 (void)aSize;
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);
374 // Width, Height
375 if( mpOutputDevice )
377 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
378 ::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
379 ::Size aDevSz = mpOutputDevice->LogicToPixel(aVCLSize, aMode );
380 aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
383 return aAWTSize;