bump product version to 5.0.4.1
[LibreOffice.git] / toolkit / source / awt / vclxdevice.cxx
blobaff22613f286ace5ab0547cdb1653b2b392f836b
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 .
20 #include <com/sun/star/awt/DeviceCapability.hpp>
22 #include <com/sun/star/util/MeasureUnit.hpp>
24 #include <toolkit/awt/vclxdevice.hxx>
25 #include <toolkit/awt/vclxfont.hxx>
26 #include <toolkit/awt/vclxbitmap.hxx>
27 #include <toolkit/helper/vclunohelper.hxx>
28 #include <toolkit/helper/macros.hxx>
29 #include <cppuhelper/typeprovider.hxx>
30 #include <cppuhelper/queryinterface.hxx>
32 #include <rtl/uuid.h>
34 #include <vcl/svapp.hxx>
35 #include <vcl/outdev.hxx>
36 #include <vcl/window.hxx>
37 #include <vcl/print.hxx>
38 #include <vcl/virdev.hxx>
39 #include <vcl/bitmapex.hxx>
40 #include <vcl/font.hxx>
43 // class VCLXDevice
45 VCLXDevice::VCLXDevice()
46 : pDummy(NULL)
47 , nFlags(0)
51 VCLXDevice::~VCLXDevice()
53 //TODO: why was this empty, and everything done in ~VCLXVirtualDevice?
54 SolarMutexGuard g;
55 mpOutputDevice.reset();
58 void VCLXDevice::SetCreatedWithToolkit( bool bCreatedWithToolkit )
60 if ( bCreatedWithToolkit )
61 nFlags |= FLAGS_CREATEDWITHTOOLKIT;
62 else
63 nFlags &= ~FLAGS_CREATEDWITHTOOLKIT;
66 // ::com::sun::star::uno::XInterface
67 ::com::sun::star::uno::Any VCLXDevice::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException, std::exception)
69 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
70 (static_cast< ::com::sun::star::awt::XDevice* >(this)),
71 (static_cast< ::com::sun::star::lang::XUnoTunnel* >(this)),
72 (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)),
73 (static_cast< ::com::sun::star::awt::XUnitConversion* >(this)) );
74 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
77 // ::com::sun::star::lang::XUnoTunnel
78 IMPL_XUNOTUNNEL( VCLXDevice )
80 // ::com::sun::star::lang::XTypeProvider
81 IMPL_XTYPEPROVIDER_START( VCLXDevice )
82 cppu::UnoType<com::sun::star::awt::XDevice>::get(),
83 cppu::UnoType<com::sun::star::awt::XUnitConversion>::get()
84 IMPL_XTYPEPROVIDER_END
87 // ::com::sun::star::awt::XDevice,
88 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics > VCLXDevice::createGraphics( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
90 SolarMutexGuard aGuard;
92 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics > xRef;
94 if ( mpOutputDevice )
95 xRef = mpOutputDevice->CreateUnoGraphics();
97 return xRef;
100 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXDevice::createDevice( sal_Int32 nWidth, sal_Int32 nHeight ) throw(::com::sun::star::uno::RuntimeException, std::exception)
102 SolarMutexGuard aGuard;
104 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > xRef;
105 if ( GetOutputDevice() )
107 VCLXVirtualDevice* pVDev = new VCLXVirtualDevice;
108 VclPtrInstance<VirtualDevice> pVclVDev( *GetOutputDevice() );
109 pVclVDev->SetOutputSizePixel( Size( nWidth, nHeight ) );
110 pVDev->SetVirtualDevice( pVclVDev );
111 xRef = pVDev;
113 return xRef;
116 ::com::sun::star::awt::DeviceInfo VCLXDevice::getInfo() throw(::com::sun::star::uno::RuntimeException, std::exception)
118 SolarMutexGuard aGuard;
120 ::com::sun::star::awt::DeviceInfo aInfo;
122 if( mpOutputDevice )
124 Size aDevSz;
125 OutDevType eDevType = mpOutputDevice->GetOutDevType();
126 if ( eDevType == OUTDEV_WINDOW )
128 aDevSz = static_cast<vcl::Window*>(mpOutputDevice.get())->GetSizePixel();
129 static_cast<vcl::Window*>(mpOutputDevice.get())->GetBorder( aInfo.LeftInset, aInfo.TopInset, aInfo.RightInset, aInfo.BottomInset );
131 else if ( eDevType == OUTDEV_PRINTER )
133 aDevSz = static_cast<Printer*>(mpOutputDevice.get())->GetPaperSizePixel();
134 Size aOutSz = mpOutputDevice->GetOutputSizePixel();
135 Point aOffset = static_cast<Printer*>(mpOutputDevice.get())->GetPageOffset();
136 aInfo.LeftInset = aOffset.X();
137 aInfo.TopInset = aOffset.Y();
138 aInfo.RightInset = aDevSz.Width() - aOutSz.Width() - aOffset.X();
139 aInfo.BottomInset = aDevSz.Height() - aOutSz.Height() - aOffset.Y();
141 else // VirtualDevice
143 aDevSz = mpOutputDevice->GetOutputSizePixel();
144 aInfo.LeftInset = 0;
145 aInfo.TopInset = 0;
146 aInfo.RightInset = 0;
147 aInfo.BottomInset = 0;
150 aInfo.Width = aDevSz.Width();
151 aInfo.Height = aDevSz.Height();
153 Size aTmpSz = mpOutputDevice->LogicToPixel( Size( 1000, 1000 ), MapMode( MAP_CM ) );
154 aInfo.PixelPerMeterX = aTmpSz.Width()/10;
155 aInfo.PixelPerMeterY = aTmpSz.Height()/10;
157 aInfo.BitsPerPixel = mpOutputDevice->GetBitCount();
159 aInfo.Capabilities = 0;
160 if ( mpOutputDevice->GetOutDevType() != OUTDEV_PRINTER )
161 aInfo.Capabilities = ::com::sun::star::awt::DeviceCapability::RASTEROPERATIONS|::com::sun::star::awt::DeviceCapability::GETBITS;
164 return aInfo;
167 ::com::sun::star::uno::Sequence< ::com::sun::star::awt::FontDescriptor > VCLXDevice::getFontDescriptors( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
169 SolarMutexGuard aGuard;
171 ::com::sun::star::uno::Sequence< ::com::sun::star::awt::FontDescriptor> aFonts;
172 if( mpOutputDevice )
174 int nFonts = mpOutputDevice->GetDevFontCount();
175 if ( nFonts )
177 aFonts = ::com::sun::star::uno::Sequence< ::com::sun::star::awt::FontDescriptor>( nFonts );
178 ::com::sun::star::awt::FontDescriptor* pFonts = aFonts.getArray();
179 for ( int n = 0; n < nFonts; n++ )
180 pFonts[n] = VCLUnoHelper::CreateFontDescriptor( mpOutputDevice->GetDevFont( n ) );
183 return aFonts;
186 ::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, std::exception)
188 SolarMutexGuard aGuard;
190 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont > xRef;
191 if( mpOutputDevice )
193 VCLXFont* pMetric = new VCLXFont;
194 pMetric->Init( *this, VCLUnoHelper::CreateFont( rDescriptor, mpOutputDevice->GetFont() ) );
195 xRef = pMetric;
197 return xRef;
200 ::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, std::exception)
202 SolarMutexGuard aGuard;
204 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap > xBmp;
205 if( mpOutputDevice )
207 Bitmap aBmp = mpOutputDevice->GetBitmap( Point( nX, nY ), Size( nWidth, nHeight ) );
209 VCLXBitmap* pBmp = new VCLXBitmap;
210 pBmp->SetBitmap( BitmapEx( aBmp ) );
211 xBmp = pBmp;
213 return xBmp;
216 ::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, std::exception)
218 SolarMutexGuard aGuard;
220 BitmapEx aBmp = VCLUnoHelper::GetBitmap( rxBitmap );
221 VCLXBitmap* pBmp = new VCLXBitmap;
222 pBmp->SetBitmap( aBmp );
223 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDisplayBitmap > xDBmp = pBmp;
224 return xDBmp;
227 VCLXVirtualDevice::~VCLXVirtualDevice()
229 SolarMutexGuard aGuard;
231 mpOutputDevice.disposeAndClear();
234 // Interface implementation of ::com::sun::star::awt::XUnitConversion
236 ::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, std::exception)
238 (void)aPoint;
239 SolarMutexGuard aGuard;
240 if (TargetUnit == com::sun::star::util::MeasureUnit::PERCENT )
242 // percentage not allowed here
243 throw ::com::sun::star::lang::IllegalArgumentException();
246 ::com::sun::star::awt::Point aAWTPoint(0,0);
247 // X,Y
249 if( mpOutputDevice )
251 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
252 ::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
253 ::Point aDevPoint = mpOutputDevice->PixelToLogic(aVCLPoint, aMode );
254 aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
257 return aAWTPoint;
261 ::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, std::exception)
263 (void)aPoint;
264 SolarMutexGuard aGuard;
265 if (SourceUnit == com::sun::star::util::MeasureUnit::PERCENT ||
266 SourceUnit == com::sun::star::util::MeasureUnit::PIXEL )
268 // pixel or percentage not allowed here
269 throw ::com::sun::star::lang::IllegalArgumentException();
272 ::com::sun::star::awt::Point aAWTPoint(0,0);
274 if( mpOutputDevice )
276 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
277 ::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
278 ::Point aDevPoint = mpOutputDevice->LogicToPixel(aVCLPoint, aMode );
279 aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
282 return aAWTPoint;
285 ::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, std::exception)
287 (void)aSize;
288 SolarMutexGuard aGuard;
289 if (TargetUnit == com::sun::star::util::MeasureUnit::PERCENT)
291 // percentage not allowed here
292 throw ::com::sun::star::lang::IllegalArgumentException();
295 ::com::sun::star::awt::Size aAWTSize(0,0);
296 // Width, Height
299 if( mpOutputDevice )
301 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
302 ::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
303 ::Size aDevSz = mpOutputDevice->PixelToLogic(aVCLSize, aMode );
304 aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
307 return aAWTSize;
310 ::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, std::exception)
312 (void)aSize;
313 SolarMutexGuard aGuard;
314 if (SourceUnit == com::sun::star::util::MeasureUnit::PERCENT ||
315 SourceUnit == com::sun::star::util::MeasureUnit::PIXEL)
317 // pixel or percentage not allowed here
318 throw ::com::sun::star::lang::IllegalArgumentException();
321 ::com::sun::star::awt::Size aAWTSize(0,0);
322 // Width, Height
323 if( mpOutputDevice )
325 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
326 ::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
327 ::Size aDevSz = mpOutputDevice->LogicToPixel(aVCLSize, aMode );
328 aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
331 return aAWTSize;
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */