1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <com/sun/star/awt/DeviceCapability.hpp>
21
22 #include <com/sun/star/util/MeasureUnit.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24
25 #include <toolkit/awt/vclxdevice.hxx>
26 #include <toolkit/awt/vclxfont.hxx>
27 #include <toolkit/awt/vclxbitmap.hxx>
28 #include <toolkit/helper/vclunohelper.hxx>
29 #include <toolkit/helper/macros.hxx>
30 #include <cppuhelper/queryinterface.hxx>
31
32 #include <vcl/svapp.hxx>
33 #include <vcl/outdev.hxx>
34 #include <vcl/window.hxx>
35 #include <vcl/print.hxx>
36 #include <vcl/virdev.hxx>
37 #include <vcl/bitmapex.hxx>
38 #include <vcl/metric.hxx>
39
40 // class VCLXDevice
41
VCLXDevice()42 VCLXDevice::VCLXDevice()
43 {
44 }
45
~VCLXDevice()46 VCLXDevice::~VCLXDevice()
47 {
48 //TODO: why was this empty, and everything done in ~VCLXVirtualDevice?
49 SolarMutexGuard g;
50 mpOutputDevice.reset();
51 }
52
53 // css::lang::XUnoTunnel
54 UNO3_GETIMPLEMENTATION_IMPL( VCLXDevice );
55
56 // css::awt::XDevice,
createGraphics()57 css::uno::Reference< css::awt::XGraphics > VCLXDevice::createGraphics( )
58 {
59 SolarMutexGuard aGuard;
60
61 css::uno::Reference< css::awt::XGraphics > xRef;
62
63 if ( mpOutputDevice )
64 xRef = mpOutputDevice->CreateUnoGraphics();
65
66 return xRef;
67 }
68
createDevice(sal_Int32 nWidth,sal_Int32 nHeight)69 css::uno::Reference< css::awt::XDevice > VCLXDevice::createDevice( sal_Int32 nWidth, sal_Int32 nHeight )
70 {
71 SolarMutexGuard aGuard;
72
73 css::uno::Reference< css::awt::XDevice > xRef;
74 if ( GetOutputDevice() )
75 {
76 VCLXVirtualDevice* pVDev = new VCLXVirtualDevice;
77 VclPtrInstance<VirtualDevice> pVclVDev( *GetOutputDevice() );
78 pVclVDev->SetOutputSizePixel( Size( nWidth, nHeight ) );
79 pVDev->SetVirtualDevice( pVclVDev );
80 xRef = pVDev;
81 }
82 return xRef;
83 }
84
getInfo()85 css::awt::DeviceInfo VCLXDevice::getInfo()
86 {
87 SolarMutexGuard aGuard;
88
89 css::awt::DeviceInfo aInfo;
90
91 if( mpOutputDevice )
92 {
93 Size aDevSz;
94 OutDevType eDevType = mpOutputDevice->GetOutDevType();
95 if ( eDevType == OUTDEV_WINDOW )
96 {
97 aDevSz = static_cast<vcl::Window*>(mpOutputDevice.get())->GetSizePixel();
98 static_cast<vcl::Window*>(mpOutputDevice.get())->GetBorder( aInfo.LeftInset, aInfo.TopInset, aInfo.RightInset, aInfo.BottomInset );
99 }
100 else if ( eDevType == OUTDEV_PRINTER )
101 {
102 aDevSz = static_cast<Printer*>(mpOutputDevice.get())->GetPaperSizePixel();
103 Size aOutSz = mpOutputDevice->GetOutputSizePixel();
104 Point aOffset = static_cast<Printer*>(mpOutputDevice.get())->GetPageOffset();
105 aInfo.LeftInset = aOffset.X();
106 aInfo.TopInset = aOffset.Y();
107 aInfo.RightInset = aDevSz.Width() - aOutSz.Width() - aOffset.X();
108 aInfo.BottomInset = aDevSz.Height() - aOutSz.Height() - aOffset.Y();
109 }
110 else // VirtualDevice
111 {
112 aDevSz = mpOutputDevice->GetOutputSizePixel();
113 aInfo.LeftInset = 0;
114 aInfo.TopInset = 0;
115 aInfo.RightInset = 0;
116 aInfo.BottomInset = 0;
117 }
118
119 aInfo.Width = aDevSz.Width();
120 aInfo.Height = aDevSz.Height();
121
122 Size aTmpSz = mpOutputDevice->LogicToPixel( Size( 1000, 1000 ), MapMode( MapUnit::MapCM ) );
123 aInfo.PixelPerMeterX = aTmpSz.Width()/10;
124 aInfo.PixelPerMeterY = aTmpSz.Height()/10;
125
126 aInfo.BitsPerPixel = mpOutputDevice->GetBitCount();
127
128 aInfo.Capabilities = 0;
129 if ( mpOutputDevice->GetOutDevType() != OUTDEV_PRINTER )
130 aInfo.Capabilities = css::awt::DeviceCapability::RASTEROPERATIONS|css::awt::DeviceCapability::GETBITS;
131 }
132
133 return aInfo;
134 }
135
getFontDescriptors()136 css::uno::Sequence< css::awt::FontDescriptor > VCLXDevice::getFontDescriptors( )
137 {
138 SolarMutexGuard aGuard;
139
140 css::uno::Sequence< css::awt::FontDescriptor> aFonts;
141 if( mpOutputDevice )
142 {
143 int nFonts = mpOutputDevice->GetDevFontCount();
144 if ( nFonts )
145 {
146 aFonts = css::uno::Sequence< css::awt::FontDescriptor>( nFonts );
147 css::awt::FontDescriptor* pFonts = aFonts.getArray();
148 for ( int n = 0; n < nFonts; n++ )
149 pFonts[n] = VCLUnoHelper::CreateFontDescriptor( mpOutputDevice->GetDevFont( n ) );
150 }
151 }
152 return aFonts;
153 }
154
getFont(const css::awt::FontDescriptor & rDescriptor)155 css::uno::Reference< css::awt::XFont > VCLXDevice::getFont( const css::awt::FontDescriptor& rDescriptor )
156 {
157 SolarMutexGuard aGuard;
158
159 css::uno::Reference< css::awt::XFont > xRef;
160 if( mpOutputDevice )
161 {
162 VCLXFont* pMetric = new VCLXFont;
163 pMetric->Init( *this, VCLUnoHelper::CreateFont( rDescriptor, mpOutputDevice->GetFont() ) );
164 xRef = pMetric;
165 }
166 return xRef;
167 }
168
createBitmap(sal_Int32 nX,sal_Int32 nY,sal_Int32 nWidth,sal_Int32 nHeight)169 css::uno::Reference< css::awt::XBitmap > VCLXDevice::createBitmap( sal_Int32 nX, sal_Int32 nY, sal_Int32 nWidth, sal_Int32 nHeight )
170 {
171 SolarMutexGuard aGuard;
172
173 css::uno::Reference< css::awt::XBitmap > xBmp;
174 if( mpOutputDevice )
175 {
176 BitmapEx aBmp = mpOutputDevice->GetBitmapEx( Point( nX, nY ), Size( nWidth, nHeight ) );
177
178 VCLXBitmap* pBmp = new VCLXBitmap;
179 pBmp->SetBitmap( aBmp );
180 xBmp = pBmp;
181 }
182 return xBmp;
183 }
184
createDisplayBitmap(const css::uno::Reference<css::awt::XBitmap> & rxBitmap)185 css::uno::Reference< css::awt::XDisplayBitmap > VCLXDevice::createDisplayBitmap( const css::uno::Reference< css::awt::XBitmap >& rxBitmap )
186 {
187 SolarMutexGuard aGuard;
188
189 BitmapEx aBmp = VCLUnoHelper::GetBitmap( rxBitmap );
190 VCLXBitmap* pBmp = new VCLXBitmap;
191 pBmp->SetBitmap( aBmp );
192 css::uno::Reference< css::awt::XDisplayBitmap > xDBmp = pBmp;
193 return xDBmp;
194 }
195
~VCLXVirtualDevice()196 VCLXVirtualDevice::~VCLXVirtualDevice()
197 {
198 SolarMutexGuard aGuard;
199
200 mpOutputDevice.disposeAndClear();
201 }
202
203 // Interface implementation of css::awt::XUnitConversion
204
convertPointToLogic(const css::awt::Point & aPoint,::sal_Int16 TargetUnit)205 css::awt::Point SAL_CALL VCLXDevice::convertPointToLogic( const css::awt::Point& aPoint, ::sal_Int16 TargetUnit )
206 {
207 SolarMutexGuard aGuard;
208 if (TargetUnit == css::util::MeasureUnit::PERCENT )
209 {
210 // percentage not allowed here
211 throw css::lang::IllegalArgumentException();
212 }
213
214 css::awt::Point aAWTPoint(0,0);
215 // X,Y
216
217 if( mpOutputDevice )
218 {
219 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
220 ::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
221 ::Point aDevPoint = mpOutputDevice->PixelToLogic(aVCLPoint, aMode );
222 aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
223 }
224
225 return aAWTPoint;
226 }
227
228
convertPointToPixel(const css::awt::Point & aPoint,::sal_Int16 SourceUnit)229 css::awt::Point SAL_CALL VCLXDevice::convertPointToPixel( const css::awt::Point& aPoint, ::sal_Int16 SourceUnit )
230 {
231 SolarMutexGuard aGuard;
232 if (SourceUnit == css::util::MeasureUnit::PERCENT ||
233 SourceUnit == css::util::MeasureUnit::PIXEL )
234 {
235 // pixel or percentage not allowed here
236 throw css::lang::IllegalArgumentException();
237 }
238
239 css::awt::Point aAWTPoint(0,0);
240
241 if( mpOutputDevice )
242 {
243 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
244 ::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
245 ::Point aDevPoint = mpOutputDevice->LogicToPixel(aVCLPoint, aMode );
246 aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
247 }
248
249 return aAWTPoint;
250 }
251
convertSizeToLogic(const css::awt::Size & aSize,::sal_Int16 TargetUnit)252 css::awt::Size SAL_CALL VCLXDevice::convertSizeToLogic( const css::awt::Size& aSize, ::sal_Int16 TargetUnit )
253 {
254 SolarMutexGuard aGuard;
255 if (TargetUnit == css::util::MeasureUnit::PERCENT)
256 {
257 // percentage not allowed here
258 throw css::lang::IllegalArgumentException();
259 }
260
261 css::awt::Size aAWTSize(0,0);
262 // Width, Height
263
264
265 if( mpOutputDevice )
266 {
267 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
268 ::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
269 ::Size aDevSz = mpOutputDevice->PixelToLogic(aVCLSize, aMode );
270 aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
271 }
272
273 return aAWTSize;
274 }
275
convertSizeToPixel(const css::awt::Size & aSize,::sal_Int16 SourceUnit)276 css::awt::Size SAL_CALL VCLXDevice::convertSizeToPixel( const css::awt::Size& aSize, ::sal_Int16 SourceUnit )
277 {
278 SolarMutexGuard aGuard;
279 if (SourceUnit == css::util::MeasureUnit::PERCENT ||
280 SourceUnit == css::util::MeasureUnit::PIXEL)
281 {
282 // pixel or percentage not allowed here
283 throw css::lang::IllegalArgumentException();
284 }
285
286 css::awt::Size aAWTSize(0,0);
287 // Width, Height
288 if( mpOutputDevice )
289 {
290 MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
291 ::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
292 ::Size aDevSz = mpOutputDevice->LogicToPixel(aVCLSize, aMode );
293 aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
294 }
295
296 return aAWTSize;
297 }
298
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
300