xref: /aoo41x/main/toolkit/source/awt/vclxdevice.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_toolkit.hxx"
30 #include <com/sun/star/awt/DeviceCapability.hpp>
31 
32 #include <com/sun/star/util/MeasureUnit.hpp>
33 
34 #include <toolkit/awt/vclxdevice.hxx>
35 #include <toolkit/awt/vclxfont.hxx>
36 #include <toolkit/awt/vclxbitmap.hxx>
37 #include <toolkit/helper/vclunohelper.hxx>
38 #include <toolkit/helper/macros.hxx>
39 #include <cppuhelper/typeprovider.hxx>
40 
41 #include <rtl/memory.h>
42 #include <rtl/uuid.h>
43 
44 #include <vcl/svapp.hxx>
45 #include <vcl/outdev.hxx>
46 #include <vcl/window.hxx>
47 #include <vcl/print.hxx>
48 #include <vcl/virdev.hxx>
49 #include <vcl/bitmapex.hxx>
50 #include <vcl/font.hxx>
51 
52 //	----------------------------------------------------
53 //	class VCLXDevice
54 //	----------------------------------------------------
55 VCLXDevice::VCLXDevice() : mrMutex( Application::GetSolarMutex() )
56 {
57 	mpOutputDevice = NULL;
58 	nFlags = 0;
59 }
60 
61 VCLXDevice::~VCLXDevice()
62 {
63 // Was thought for #88347#, but didn't help, because the interface will not be released
64 // But would be a good idea anyway, check after 6.0, it's a little bit dangerous now
65 //    if( mpOutputDevice && IsCreatedWithToolkit() )
66 //    {
67 //        delete mpOutputDevice;
68 //    }
69 }
70 
71 void VCLXDevice::DestroyOutputDevice()
72 {
73     delete mpOutputDevice;
74 	mpOutputDevice = NULL;
75 }
76 
77 void VCLXDevice::SetCreatedWithToolkit( sal_Bool bCreatedWithToolkit )
78 {
79     if ( bCreatedWithToolkit )
80         nFlags |= FLAGS_CREATEDWITHTOOLKIT;
81     else
82         nFlags &= ~FLAGS_CREATEDWITHTOOLKIT;
83 }
84 
85 sal_Bool VCLXDevice::IsCreatedWithToolkit() const
86 {
87     return ( nFlags & FLAGS_CREATEDWITHTOOLKIT ) != 0;
88 }
89 
90 // ::com::sun::star::uno::XInterface
91 ::com::sun::star::uno::Any VCLXDevice::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
92 {
93 	::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
94 										SAL_STATIC_CAST( ::com::sun::star::awt::XDevice*, this ),
95 										SAL_STATIC_CAST( ::com::sun::star::lang::XUnoTunnel*, this ),
96 										SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider*, this ),
97 										SAL_STATIC_CAST( ::com::sun::star::awt::XUnitConversion*, this ) );
98 	return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
99 }
100 
101 // ::com::sun::star::lang::XUnoTunnel
102 IMPL_XUNOTUNNEL( VCLXDevice )
103 
104 // ::com::sun::star::lang::XTypeProvider
105 IMPL_XTYPEPROVIDER_START( VCLXDevice )
106 	getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice>* ) NULL ),
107 	getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XUnitConversion>* ) NULL )
108 IMPL_XTYPEPROVIDER_END
109 
110 
111 // ::com::sun::star::awt::XDevice,
112 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics > VCLXDevice::createGraphics(  ) throw(::com::sun::star::uno::RuntimeException)
113 {
114 	::vos::OGuard aGuard( GetMutex() );
115 
116 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics > xRef;
117 
118 	if ( mpOutputDevice )
119 		xRef = mpOutputDevice->CreateUnoGraphics();
120 
121 	return xRef;
122 }
123 
124 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXDevice::createDevice( sal_Int32 nWidth, sal_Int32 nHeight ) throw(::com::sun::star::uno::RuntimeException)
125 {
126 	::vos::OGuard aGuard( GetMutex() );
127 
128 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice >  xRef;
129 	if ( GetOutputDevice() )
130 	{
131 		VCLXVirtualDevice* pVDev = new VCLXVirtualDevice;
132 		VirtualDevice* pVclVDev = new VirtualDevice( *GetOutputDevice() );
133 		pVclVDev->SetOutputSizePixel( Size( nWidth, nHeight ) );
134 		pVDev->SetVirtualDevice( pVclVDev );
135 		xRef = pVDev;
136 	}
137 	return xRef;
138 }
139 
140 ::com::sun::star::awt::DeviceInfo VCLXDevice::getInfo() throw(::com::sun::star::uno::RuntimeException)
141 {
142 	::vos::OGuard aGuard( GetMutex() );
143 
144 	::com::sun::star::awt::DeviceInfo aInfo;
145 
146 	if( mpOutputDevice )
147 	{
148 		Size aDevSz;
149 		OutDevType eDevType = mpOutputDevice->GetOutDevType();
150 		if ( eDevType == OUTDEV_WINDOW )
151 		{
152 			aDevSz = ((Window*)mpOutputDevice)->GetSizePixel();
153 			((Window*)mpOutputDevice)->GetBorder( aInfo.LeftInset, aInfo.TopInset, aInfo.RightInset, aInfo.BottomInset );
154 		}
155 		else if ( eDevType == OUTDEV_PRINTER )
156 		{
157 			aDevSz = ((Printer*)mpOutputDevice)->GetPaperSizePixel();
158 			Size aOutSz = mpOutputDevice->GetOutputSizePixel();
159 			Point aOffset = ((Printer*)mpOutputDevice)->GetPageOffset();
160 			aInfo.LeftInset = aOffset.X();
161 			aInfo.TopInset = aOffset.Y();
162 			aInfo.RightInset = aDevSz.Width() - aOutSz.Width() - aOffset.X();
163 			aInfo.BottomInset = aDevSz.Height() - aOutSz.Height() - aOffset.Y();
164 		}
165 		else // VirtualDevice
166 		{
167 			aDevSz = mpOutputDevice->GetOutputSizePixel();
168 			aInfo.LeftInset = 0;
169 			aInfo.TopInset = 0;
170 			aInfo.RightInset = 0;
171 			aInfo.BottomInset = 0;
172 		}
173 
174 		aInfo.Width = aDevSz.Width();
175 		aInfo.Height = aDevSz.Height();
176 
177 		Size aTmpSz = mpOutputDevice->LogicToPixel( Size( 1000, 1000 ), MapMode( MAP_CM ) );
178 		aInfo.PixelPerMeterX = aTmpSz.Width()/10;
179 		aInfo.PixelPerMeterY = aTmpSz.Height()/10;
180 
181 		aInfo.BitsPerPixel = mpOutputDevice->GetBitCount();
182 
183 		aInfo.Capabilities = 0;
184 		if ( mpOutputDevice->GetOutDevType() != OUTDEV_PRINTER )
185 			aInfo.Capabilities = ::com::sun::star::awt::DeviceCapability::RASTEROPERATIONS|::com::sun::star::awt::DeviceCapability::GETBITS;
186 	}
187 
188 	return aInfo;
189 }
190 
191 ::com::sun::star::uno::Sequence< ::com::sun::star::awt::FontDescriptor > VCLXDevice::getFontDescriptors(  ) throw(::com::sun::star::uno::RuntimeException)
192 {
193 	::vos::OGuard aGuard( GetMutex() );
194 
195 	::com::sun::star::uno::Sequence< ::com::sun::star::awt::FontDescriptor> aFonts;
196 	if( mpOutputDevice )
197 	{
198 		int nFonts = mpOutputDevice->GetDevFontCount();
199 		if ( nFonts )
200 		{
201 			aFonts = ::com::sun::star::uno::Sequence< ::com::sun::star::awt::FontDescriptor>( nFonts );
202 			::com::sun::star::awt::FontDescriptor* pFonts = aFonts.getArray();
203 			for ( int n = 0; n < nFonts; n++ )
204 				pFonts[n] = VCLUnoHelper::CreateFontDescriptor( mpOutputDevice->GetDevFont( n ) );
205 		}
206 	}
207 	return aFonts;
208 }
209 
210 ::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)
211 {
212 	::vos::OGuard aGuard( GetMutex() );
213 
214 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont >  xRef;
215 	if( mpOutputDevice )
216 	{
217 		VCLXFont* pMetric = new VCLXFont;
218 		pMetric->Init( *this, VCLUnoHelper::CreateFont( rDescriptor, mpOutputDevice->GetFont() ) );
219 		xRef = pMetric;
220 	}
221 	return xRef;
222 }
223 
224 ::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)
225 {
226 	::vos::OGuard aGuard( GetMutex() );
227 
228 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap >  xBmp;
229 	if( mpOutputDevice )
230 	{
231 		Bitmap aBmp = mpOutputDevice->GetBitmap( Point( nX, nY ), Size( nWidth, nHeight ) );
232 
233 		VCLXBitmap* pBmp = new VCLXBitmap;
234 		pBmp->SetBitmap( BitmapEx( aBmp ) );
235 		xBmp = pBmp;
236 	}
237 	return xBmp;
238 }
239 
240 ::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)
241 {
242 	::vos::OGuard aGuard( GetMutex() );
243 
244 	BitmapEx aBmp = VCLUnoHelper::GetBitmap( rxBitmap );
245 	VCLXBitmap* pBmp = new VCLXBitmap;
246 	pBmp->SetBitmap( aBmp );
247 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XDisplayBitmap >  xDBmp = pBmp;
248 	return xDBmp;
249 }
250 
251 
252 VCLXVirtualDevice::~VCLXVirtualDevice()
253 {
254 	::vos::OGuard aGuard( GetMutex() );
255 
256     DestroyOutputDevice();
257 }
258 
259 
260 // -----------------------------------------------------------------------------
261 // ::com::sun::star::awt::XTextConstraints
262 // -----------------------------------------------------------------------------
263 // ::sal_Int32 SAL_CALL VCLXDevice::getTextWidth( const ::rtl::OUString& Text ) throw (::com::sun::star::uno::RuntimeException)
264 // {
265 // 	::vos::OGuard aGuard( GetMutex() );
266 //     if (Text.getLength() == 0)
267 //     {
268 //         return 0;
269 //     }
270 //
271 //     return 1;
272 // }
273 //
274 // ::sal_Int32 SAL_CALL VCLXDevice::getTextHeight(  ) throw (::com::sun::star::uno::RuntimeException)
275 // {
276 // 	::vos::OGuard aGuard( GetMutex() );
277 //     return 1;
278 // }
279 
280 
281 // -----------------------------------------------------------------------------
282 // Interface implementation of ::com::sun::star::awt::XUnitConversion
283 // -----------------------------------------------------------------------------
284 
285 ::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)
286 {
287     (void)aPoint;
288 	::vos::OGuard aGuard( GetMutex() );
289     if (TargetUnit == com::sun::star::util::MeasureUnit::PERCENT )
290     {
291         // percentage not allowed here
292         throw ::com::sun::star::lang::IllegalArgumentException();
293     }
294 
295     ::com::sun::star::awt::Point aAWTPoint(0,0);
296     // X,Y
297 
298 	if( mpOutputDevice )
299 	{
300         MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
301 		::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
302 		::Point aDevPoint = mpOutputDevice->PixelToLogic(aVCLPoint, aMode );
303         aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
304     }
305 
306     return aAWTPoint;
307 }
308 
309 
310 ::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)
311 {
312     (void)aPoint;
313 	::vos::OGuard aGuard( GetMutex() );
314     if (SourceUnit == com::sun::star::util::MeasureUnit::PERCENT ||
315         SourceUnit == com::sun::star::util::MeasureUnit::PIXEL )
316     {
317         // pixel or percentage not allowed here
318         throw ::com::sun::star::lang::IllegalArgumentException();
319     }
320 
321     ::com::sun::star::awt::Point aAWTPoint(0,0);
322 
323 	if( mpOutputDevice )
324 	{
325         MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
326 		::Point aVCLPoint = VCLUnoHelper::ConvertToVCLPoint(aPoint);
327 		::Point aDevPoint = mpOutputDevice->LogicToPixel(aVCLPoint, aMode );
328         aAWTPoint = VCLUnoHelper::ConvertToAWTPoint(aDevPoint);
329     }
330 
331     return aAWTPoint;
332 }
333 
334 ::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)
335 {
336     (void)aSize;
337 	::vos::OGuard aGuard( GetMutex() );
338     if (TargetUnit == com::sun::star::util::MeasureUnit::PERCENT)
339     {
340         // percentage not allowed here
341         throw ::com::sun::star::lang::IllegalArgumentException();
342     }
343 
344     ::com::sun::star::awt::Size aAWTSize(0,0);
345     // Width, Height
346 
347 
348 	if( mpOutputDevice )
349 	{
350         MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(TargetUnit));
351 		::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
352 		::Size aDevSz = mpOutputDevice->PixelToLogic(aVCLSize, aMode );
353         aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
354     }
355 
356     return aAWTSize;
357 }
358 
359 ::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)
360 {
361     (void)aSize;
362 	::vos::OGuard aGuard( GetMutex() );
363     if (SourceUnit == com::sun::star::util::MeasureUnit::PERCENT ||
364         SourceUnit == com::sun::star::util::MeasureUnit::PIXEL)
365     {
366         // pixel or percentage not allowed here
367         throw ::com::sun::star::lang::IllegalArgumentException();
368     }
369 
370     ::com::sun::star::awt::Size aAWTSize(0,0);
371     // Width, Height
372 	if( mpOutputDevice )
373 	{
374         MapMode aMode(VCLUnoHelper::ConvertToMapModeUnit(SourceUnit));
375 		::Size aVCLSize = VCLUnoHelper::ConvertToVCLSize(aSize);
376 		::Size aDevSz = mpOutputDevice->LogicToPixel(aVCLSize, aMode );
377         aAWTSize = VCLUnoHelper::ConvertToAWTSize(aDevSz);
378     }
379 
380     return aAWTSize;
381 }
382 
383