1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
45 VCLXDevice::VCLXDevice()
46 : mpOutputDevice(NULL
)
52 VCLXDevice::~VCLXDevice()
56 void VCLXDevice::DestroyOutputDevice()
58 delete mpOutputDevice
;
59 mpOutputDevice
= NULL
;
62 void VCLXDevice::SetCreatedWithToolkit( bool bCreatedWithToolkit
)
64 if ( bCreatedWithToolkit
)
65 nFlags
|= FLAGS_CREATEDWITHTOOLKIT
;
67 nFlags
&= ~FLAGS_CREATEDWITHTOOLKIT
;
70 // ::com::sun::star::uno::XInterface
71 ::com::sun::star::uno::Any
VCLXDevice::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
73 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
74 (static_cast< ::com::sun::star::awt::XDevice
* >(this)),
75 (static_cast< ::com::sun::star::lang::XUnoTunnel
* >(this)),
76 (static_cast< ::com::sun::star::lang::XTypeProvider
* >(this)),
77 (static_cast< ::com::sun::star::awt::XUnitConversion
* >(this)) );
78 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
81 // ::com::sun::star::lang::XUnoTunnel
82 IMPL_XUNOTUNNEL( VCLXDevice
)
84 // ::com::sun::star::lang::XTypeProvider
85 IMPL_XTYPEPROVIDER_START( VCLXDevice
)
86 cppu::UnoType
<com::sun::star::awt::XDevice
>::get(),
87 cppu::UnoType
<com::sun::star::awt::XUnitConversion
>::get()
88 IMPL_XTYPEPROVIDER_END
91 // ::com::sun::star::awt::XDevice,
92 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
> VCLXDevice::createGraphics( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
94 SolarMutexGuard aGuard
;
96 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
> xRef
;
99 xRef
= mpOutputDevice
->CreateUnoGraphics();
104 ::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
)
106 SolarMutexGuard aGuard
;
108 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> xRef
;
109 if ( GetOutputDevice() )
111 VCLXVirtualDevice
* pVDev
= new VCLXVirtualDevice
;
112 VirtualDevice
* pVclVDev
= new VirtualDevice( *GetOutputDevice() );
113 pVclVDev
->SetOutputSizePixel( Size( nWidth
, nHeight
) );
114 pVDev
->SetVirtualDevice( pVclVDev
);
120 ::com::sun::star::awt::DeviceInfo
VCLXDevice::getInfo() throw(::com::sun::star::uno::RuntimeException
, std::exception
)
122 SolarMutexGuard aGuard
;
124 ::com::sun::star::awt::DeviceInfo aInfo
;
129 OutDevType eDevType
= mpOutputDevice
->GetOutDevType();
130 if ( eDevType
== OUTDEV_WINDOW
)
132 aDevSz
= static_cast<vcl::Window
*>(mpOutputDevice
)->GetSizePixel();
133 static_cast<vcl::Window
*>(mpOutputDevice
)->GetBorder( aInfo
.LeftInset
, aInfo
.TopInset
, aInfo
.RightInset
, aInfo
.BottomInset
);
135 else if ( eDevType
== OUTDEV_PRINTER
)
137 aDevSz
= static_cast<Printer
*>(mpOutputDevice
)->GetPaperSizePixel();
138 Size aOutSz
= mpOutputDevice
->GetOutputSizePixel();
139 Point aOffset
= static_cast<Printer
*>(mpOutputDevice
)->GetPageOffset();
140 aInfo
.LeftInset
= aOffset
.X();
141 aInfo
.TopInset
= aOffset
.Y();
142 aInfo
.RightInset
= aDevSz
.Width() - aOutSz
.Width() - aOffset
.X();
143 aInfo
.BottomInset
= aDevSz
.Height() - aOutSz
.Height() - aOffset
.Y();
145 else // VirtualDevice
147 aDevSz
= mpOutputDevice
->GetOutputSizePixel();
150 aInfo
.RightInset
= 0;
151 aInfo
.BottomInset
= 0;
154 aInfo
.Width
= aDevSz
.Width();
155 aInfo
.Height
= aDevSz
.Height();
157 Size aTmpSz
= mpOutputDevice
->LogicToPixel( Size( 1000, 1000 ), MapMode( MAP_CM
) );
158 aInfo
.PixelPerMeterX
= aTmpSz
.Width()/10;
159 aInfo
.PixelPerMeterY
= aTmpSz
.Height()/10;
161 aInfo
.BitsPerPixel
= mpOutputDevice
->GetBitCount();
163 aInfo
.Capabilities
= 0;
164 if ( mpOutputDevice
->GetOutDevType() != OUTDEV_PRINTER
)
165 aInfo
.Capabilities
= ::com::sun::star::awt::DeviceCapability::RASTEROPERATIONS
|::com::sun::star::awt::DeviceCapability::GETBITS
;
171 ::com::sun::star::uno::Sequence
< ::com::sun::star::awt::FontDescriptor
> VCLXDevice::getFontDescriptors( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
173 SolarMutexGuard aGuard
;
175 ::com::sun::star::uno::Sequence
< ::com::sun::star::awt::FontDescriptor
> aFonts
;
178 int nFonts
= mpOutputDevice
->GetDevFontCount();
181 aFonts
= ::com::sun::star::uno::Sequence
< ::com::sun::star::awt::FontDescriptor
>( nFonts
);
182 ::com::sun::star::awt::FontDescriptor
* pFonts
= aFonts
.getArray();
183 for ( int n
= 0; n
< nFonts
; n
++ )
184 pFonts
[n
] = VCLUnoHelper::CreateFontDescriptor( mpOutputDevice
->GetDevFont( n
) );
190 ::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
)
192 SolarMutexGuard aGuard
;
194 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XFont
> xRef
;
197 VCLXFont
* pMetric
= new VCLXFont
;
198 pMetric
->Init( *this, VCLUnoHelper::CreateFont( rDescriptor
, mpOutputDevice
->GetFont() ) );
204 ::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
)
206 SolarMutexGuard aGuard
;
208 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
> xBmp
;
211 Bitmap aBmp
= mpOutputDevice
->GetBitmap( Point( nX
, nY
), Size( nWidth
, nHeight
) );
213 VCLXBitmap
* pBmp
= new VCLXBitmap
;
214 pBmp
->SetBitmap( BitmapEx( aBmp
) );
220 ::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
)
222 SolarMutexGuard aGuard
;
224 BitmapEx aBmp
= VCLUnoHelper::GetBitmap( rxBitmap
);
225 VCLXBitmap
* pBmp
= new VCLXBitmap
;
226 pBmp
->SetBitmap( aBmp
);
227 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDisplayBitmap
> xDBmp
= pBmp
;
232 VCLXVirtualDevice::~VCLXVirtualDevice()
234 SolarMutexGuard aGuard
;
236 DestroyOutputDevice();
241 // Interface implementation of ::com::sun::star::awt::XUnitConversion
244 ::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
)
247 SolarMutexGuard aGuard
;
248 if (TargetUnit
== com::sun::star::util::MeasureUnit::PERCENT
)
250 // percentage not allowed here
251 throw ::com::sun::star::lang::IllegalArgumentException();
254 ::com::sun::star::awt::Point
aAWTPoint(0,0);
259 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit
));
260 ::Point aVCLPoint
= VCLUnoHelper::ConvertToVCLPoint(aPoint
);
261 ::Point aDevPoint
= mpOutputDevice
->PixelToLogic(aVCLPoint
, aMode
);
262 aAWTPoint
= VCLUnoHelper::ConvertToAWTPoint(aDevPoint
);
269 ::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
)
272 SolarMutexGuard aGuard
;
273 if (SourceUnit
== com::sun::star::util::MeasureUnit::PERCENT
||
274 SourceUnit
== com::sun::star::util::MeasureUnit::PIXEL
)
276 // pixel or percentage not allowed here
277 throw ::com::sun::star::lang::IllegalArgumentException();
280 ::com::sun::star::awt::Point
aAWTPoint(0,0);
284 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit
));
285 ::Point aVCLPoint
= VCLUnoHelper::ConvertToVCLPoint(aPoint
);
286 ::Point aDevPoint
= mpOutputDevice
->LogicToPixel(aVCLPoint
, aMode
);
287 aAWTPoint
= VCLUnoHelper::ConvertToAWTPoint(aDevPoint
);
293 ::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
)
296 SolarMutexGuard aGuard
;
297 if (TargetUnit
== com::sun::star::util::MeasureUnit::PERCENT
)
299 // percentage not allowed here
300 throw ::com::sun::star::lang::IllegalArgumentException();
303 ::com::sun::star::awt::Size
aAWTSize(0,0);
309 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit
));
310 ::Size aVCLSize
= VCLUnoHelper::ConvertToVCLSize(aSize
);
311 ::Size aDevSz
= mpOutputDevice
->PixelToLogic(aVCLSize
, aMode
);
312 aAWTSize
= VCLUnoHelper::ConvertToAWTSize(aDevSz
);
318 ::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
)
321 SolarMutexGuard aGuard
;
322 if (SourceUnit
== com::sun::star::util::MeasureUnit::PERCENT
||
323 SourceUnit
== com::sun::star::util::MeasureUnit::PIXEL
)
325 // pixel or percentage not allowed here
326 throw ::com::sun::star::lang::IllegalArgumentException();
329 ::com::sun::star::awt::Size
aAWTSize(0,0);
333 MapMode
aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit
));
334 ::Size aVCLSize
= VCLUnoHelper::ConvertToVCLSize(aSize
);
335 ::Size aDevSz
= mpOutputDevice
->LogicToPixel(aVCLSize
, aMode
);
336 aAWTSize
= VCLUnoHelper::ConvertToAWTSize(aDevSz
);
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */