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