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 27 28 #include <toolkit/awt/vclxprinter.hxx> 29 #include <toolkit/helper/macros.hxx> 30 #include <cppuhelper/typeprovider.hxx> 31 #include <rtl/memory.h> 32 #include <rtl/uuid.h> 33 34 35 #include <vcl/print.hxx> 36 #include <vcl/jobset.hxx> 37 #include <vcl/svapp.hxx> 38 39 #include <tools/debug.hxx> 40 #include <tools/stream.hxx> 41 42 #include <toolkit/awt/vclxdevice.hxx> 43 44 45 #define BINARYSETUPMARKER 0x23864691 46 47 #define PROPERTY_Orientation 0 48 #define PROPERTY_Horizontal 1 49 50 ::com::sun::star::beans::Property* ImplGetProperties( sal_uInt16& rElementCount ) 51 { 52 static ::com::sun::star::beans::Property* pProperties = NULL; 53 static sal_uInt16 nElements = 0; 54 if( !pProperties ) 55 { 56 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ); 57 if( !pProperties ) 58 { 59 static ::com::sun::star::beans::Property __FAR_DATA aPropTable[] = 60 { 61 ::com::sun::star::beans::Property( ::rtl::OUString::createFromAscii( "Orientation" ), PROPERTY_Orientation, ::getCppuType((const sal_Int16*)0), 0 ), 62 ::com::sun::star::beans::Property( ::rtl::OUString::createFromAscii( "Horizontal" ), PROPERTY_Horizontal, ::getBooleanCppuType(), 0 ) 63 }; 64 pProperties = aPropTable; 65 nElements = sizeof( aPropTable ) / sizeof( ::com::sun::star::beans::Property ); 66 } 67 } 68 rElementCount = nElements; 69 return pProperties; 70 } 71 72 // ---------------------------------------------------- 73 // class VCLXPrinterPropertySet 74 // ---------------------------------------------------- 75 76 // ::com::sun::star::uno::XInterface 77 ::com::sun::star::uno::Any VCLXPrinterPropertySet::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException) 78 { 79 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType, 80 SAL_STATIC_CAST( ::com::sun::star::beans::XMultiPropertySet*, this ), 81 SAL_STATIC_CAST( ::com::sun::star::beans::XFastPropertySet*, this ), 82 SAL_STATIC_CAST( ::com::sun::star::beans::XPropertySet*, (::cppu::OPropertySetHelper*) this ), 83 SAL_STATIC_CAST( ::com::sun::star::awt::XPrinterPropertySet*, this ), 84 SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider*, this ) ); 85 return (aRet.hasValue() ? aRet : OPropertySetHelper::queryInterface( rType )); 86 } 87 88 // ::com::sun::star::lang::XTypeProvider 89 IMPL_XTYPEPROVIDER_START( VCLXPrinterPropertySet ) 90 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>* ) NULL ), 91 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>* ) NULL ), 92 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>* ) NULL ), 93 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinterPropertySet>* ) NULL ) 94 IMPL_XTYPEPROVIDER_END 95 96 VCLXPrinterPropertySet::VCLXPrinterPropertySet( const String& rPrinterName ) 97 : OPropertySetHelper( BrdcstHelper ) 98 , mpPrinter( new Printer( rPrinterName ) ) 99 { 100 osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() ); 101 102 mnOrientation = 0; 103 mbHorizontal = sal_False; 104 } 105 106 VCLXPrinterPropertySet::~VCLXPrinterPropertySet() 107 { 108 osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() ); 109 mpPrinter.reset(); 110 } 111 112 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinterPropertySet::GetDevice() 113 { 114 if ( !mxPrnDevice.is() ) 115 { 116 VCLXDevice* pDev = new VCLXDevice; 117 pDev->SetOutputDevice( GetPrinter() ); 118 mxPrnDevice = pDev; 119 } 120 return mxPrnDevice; 121 } 122 123 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > VCLXPrinterPropertySet::getPropertySetInfo( ) throw(::com::sun::star::uno::RuntimeException) 124 { 125 static ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) ); 126 return xInfo; 127 } 128 129 ::cppu::IPropertyArrayHelper& VCLXPrinterPropertySet::getInfoHelper() 130 { 131 static ::cppu::OPropertyArrayHelper* pPropertyArrayHelper = NULL; 132 if ( !pPropertyArrayHelper ) 133 { 134 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ); 135 if( !pPropertyArrayHelper ) 136 { 137 sal_uInt16 nElements; 138 ::com::sun::star::beans::Property* pProps = ImplGetProperties( nElements ); 139 pPropertyArrayHelper = new ::cppu::OPropertyArrayHelper( pProps, nElements, sal_False ); 140 } 141 } 142 return *pPropertyArrayHelper ; 143 } 144 145 sal_Bool VCLXPrinterPropertySet::convertFastPropertyValue( ::com::sun::star::uno::Any & rConvertedValue, ::com::sun::star::uno::Any & rOldValue, sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::lang::IllegalArgumentException) 146 { 147 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 148 149 sal_Bool bDifferent = sal_False; 150 switch ( nHandle ) 151 { 152 case PROPERTY_Orientation: 153 { 154 sal_Int16 n; 155 if( ( rValue >>= n ) && ( n != mnOrientation ) ) 156 { 157 rConvertedValue <<= n; 158 rOldValue <<= mnOrientation; 159 bDifferent = sal_True; 160 } 161 } 162 break; 163 case PROPERTY_Horizontal: 164 { 165 sal_Bool b; 166 if( ( rValue >>= b ) && ( b != mbHorizontal ) ) 167 { 168 rConvertedValue <<= b; 169 rOldValue <<= mbHorizontal; 170 bDifferent = sal_True; 171 } 172 } 173 break; 174 default: 175 { 176 DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" ); 177 } 178 } 179 return bDifferent; 180 } 181 182 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::uno::Exception) 183 { 184 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 185 186 switch( nHandle ) 187 { 188 case PROPERTY_Orientation: 189 { 190 rValue >>= mnOrientation; 191 } 192 break; 193 case PROPERTY_Horizontal: 194 { 195 rValue >>= mbHorizontal; 196 } 197 break; 198 default: 199 { 200 DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" ); 201 } 202 } 203 } 204 205 void VCLXPrinterPropertySet::getFastPropertyValue( ::com::sun::star::uno::Any& rValue, sal_Int32 nHandle ) const 206 { 207 ::osl::Guard< ::osl::Mutex > aGuard( ((VCLXPrinterPropertySet*)this)->Mutex ); 208 209 switch( nHandle ) 210 { 211 case PROPERTY_Orientation: 212 rValue <<= mnOrientation; 213 break; 214 case PROPERTY_Horizontal: 215 rValue <<= mbHorizontal; 216 break; 217 default: 218 { 219 DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" ); 220 } 221 } 222 } 223 224 // ::com::sun::star::awt::XPrinterPropertySet 225 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException) 226 { 227 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 228 229 ::com::sun::star::uno::Any aValue; 230 aValue <<= bHorizontal; 231 setFastPropertyValue( PROPERTY_Horizontal, aValue ); 232 } 233 234 ::com::sun::star::uno::Sequence< ::rtl::OUString > VCLXPrinterPropertySet::getFormDescriptions( ) throw(::com::sun::star::uno::RuntimeException) 235 { 236 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 237 238 sal_uInt16 nPaperBinCount = GetPrinter()->GetPaperBinCount(); 239 ::com::sun::star::uno::Sequence< ::rtl::OUString > aDescriptions( nPaperBinCount ); 240 for ( sal_uInt16 n = 0; n < nPaperBinCount; n++ ) 241 { 242 // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId> 243 String aDescr( RTL_CONSTASCII_USTRINGPARAM( "*;*;" ) ); 244 aDescr += GetPrinter()->GetPaperBinName( n ); 245 aDescr += ';'; 246 aDescr += n; 247 aDescr.AppendAscii( ";*;*", 4 ); 248 249 aDescriptions.getArray()[n] = aDescr; 250 } 251 return aDescriptions; 252 } 253 254 void VCLXPrinterPropertySet::selectForm( const ::rtl::OUString& rFormDescription ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException) 255 { 256 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 257 258 sal_Int32 nIndex = 0; 259 sal_uInt16 nPaperBin = sal::static_int_cast< sal_uInt16 >( 260 rFormDescription.getToken( 3, ';', nIndex ).toInt32()); 261 GetPrinter()->SetPaperBin( nPaperBin ); 262 } 263 264 ::com::sun::star::uno::Sequence< sal_Int8 > VCLXPrinterPropertySet::getBinarySetup( ) throw(::com::sun::star::uno::RuntimeException) 265 { 266 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 267 268 SvMemoryStream aMem; 269 aMem << BINARYSETUPMARKER; 270 aMem << GetPrinter()->GetJobSetup(); 271 return ::com::sun::star::uno::Sequence<sal_Int8>( (sal_Int8*) aMem.GetData(), aMem.Tell() ); 272 } 273 274 void VCLXPrinterPropertySet::setBinarySetup( const ::com::sun::star::uno::Sequence< sal_Int8 >& data ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException) 275 { 276 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 277 278 SvMemoryStream aMem( (char*) data.getConstArray(), data.getLength(), STREAM_READ ); 279 sal_uInt32 nMarker; 280 aMem >> nMarker; 281 DBG_ASSERT( nMarker == BINARYSETUPMARKER, "setBinarySetup - invalid!" ); 282 if ( nMarker == BINARYSETUPMARKER ) 283 { 284 JobSetup aSetup; 285 aMem >> aSetup; 286 GetPrinter()->SetJobSetup( aSetup ); 287 } 288 } 289 290 291 // ---------------------------------------------------- 292 // class VCLXPrinter 293 // ---------------------------------------------------- 294 VCLXPrinter::VCLXPrinter( const String& rPrinterName ) 295 : VCLXPrinterPropertySet( rPrinterName ) 296 { 297 } 298 299 VCLXPrinter::~VCLXPrinter() 300 { 301 } 302 303 // ::com::sun::star::uno::XInterface 304 ::com::sun::star::uno::Any VCLXPrinter::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException) 305 { 306 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType, 307 SAL_STATIC_CAST( ::com::sun::star::awt::XPrinter*, this ) ); 308 309 if ( !aRet.hasValue() ) 310 aRet = VCLXPrinterPropertySet::queryInterface( rType ); 311 312 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType )); 313 } 314 315 // ::com::sun::star::lang::XTypeProvider 316 IMPL_XTYPEPROVIDER_START( VCLXPrinter ) 317 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter>* ) NULL ), 318 VCLXPrinterPropertySet::getTypes() 319 IMPL_XTYPEPROVIDER_END 320 321 sal_Bool VCLXPrinter::start( const ::rtl::OUString& /*rJobName*/, sal_Int16 /*nCopies*/, sal_Bool /*bCollate*/ ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException) 322 { 323 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 324 325 sal_Bool bDone = sal_True; 326 if ( mpListener.get() ) 327 { 328 maInitJobSetup = mpPrinter->GetJobSetup(); 329 mpListener.reset( new vcl::OldStylePrintAdaptor( mpPrinter ) ); 330 } 331 332 return bDone; 333 } 334 335 void VCLXPrinter::end( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException) 336 { 337 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 338 339 if ( mpListener.get() ) 340 { 341 Printer::PrintJob( mpListener, maInitJobSetup ); 342 mpListener.reset(); 343 } 344 } 345 346 void VCLXPrinter::terminate( ) throw(::com::sun::star::uno::RuntimeException) 347 { 348 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 349 350 mpListener.reset(); 351 } 352 353 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinter::startPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException) 354 { 355 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 356 357 if ( mpListener.get() ) 358 { 359 mpListener->StartPage(); 360 } 361 return GetDevice(); 362 } 363 364 void VCLXPrinter::endPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException) 365 { 366 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 367 368 if ( mpListener.get() ) 369 { 370 mpListener->EndPage(); 371 } 372 } 373 374 375 // ---------------------------------------------------- 376 // class VCLXInfoPrinter 377 // ---------------------------------------------------- 378 379 VCLXInfoPrinter::VCLXInfoPrinter( const String& rPrinterName ) 380 : VCLXPrinterPropertySet( rPrinterName ) 381 { 382 } 383 384 VCLXInfoPrinter::~VCLXInfoPrinter() 385 { 386 } 387 388 // ::com::sun::star::uno::XInterface 389 ::com::sun::star::uno::Any VCLXInfoPrinter::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException) 390 { 391 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType, 392 SAL_STATIC_CAST( ::com::sun::star::awt::XInfoPrinter*, this ) ); 393 394 if ( !aRet.hasValue() ) 395 aRet = VCLXPrinterPropertySet::queryInterface( rType ); 396 397 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType )); 398 } 399 400 // ::com::sun::star::lang::XTypeProvider 401 IMPL_XTYPEPROVIDER_START( VCLXInfoPrinter ) 402 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter>* ) NULL ), 403 VCLXPrinterPropertySet::getTypes() 404 IMPL_XTYPEPROVIDER_END 405 406 // ::com::sun::star::awt::XInfoPrinter 407 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXInfoPrinter::createDevice( ) throw(::com::sun::star::uno::RuntimeException) 408 { 409 ::osl::Guard< ::osl::Mutex > aGuard( Mutex ); 410 411 return GetDevice(); 412 } 413 414 // ---------------------------------------------------- 415 // class VCLXPrinterServer 416 // ---------------------------------------------------- 417 418 // ::com::sun::star::uno::XInterface 419 ::com::sun::star::uno::Any VCLXPrinterServer::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException) 420 { 421 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType, 422 SAL_STATIC_CAST( ::com::sun::star::awt::XPrinterServer*, this ) ); 423 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType )); 424 } 425 426 // ::com::sun::star::lang::XTypeProvider 427 IMPL_XTYPEPROVIDER_START( VCLXPrinterServer ) 428 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinterServer>* ) NULL ) 429 IMPL_XTYPEPROVIDER_END 430 431 // ::com::sun::star::awt::XPrinterServer 432 ::com::sun::star::uno::Sequence< ::rtl::OUString > VCLXPrinterServer::getPrinterNames( ) throw(::com::sun::star::uno::RuntimeException) 433 { 434 const std::vector<rtl::OUString>& rQueues = Printer::GetPrinterQueues(); 435 sal_uInt32 nPrinters = rQueues.size(); 436 437 ::com::sun::star::uno::Sequence< ::rtl::OUString > aNames( nPrinters ); 438 for ( sal_uInt32 n = 0; n < nPrinters; n++ ) 439 aNames.getArray()[n] = rQueues[n]; 440 441 return aNames; 442 } 443 444 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > VCLXPrinterServer::createPrinter( const ::rtl::OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException) 445 { 446 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > xP; 447 xP = new VCLXPrinter( rPrinterName ); 448 return xP; 449 } 450 451 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > VCLXPrinterServer::createInfoPrinter( const ::rtl::OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException) 452 { 453 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > xP; 454 xP = new VCLXInfoPrinter( rPrinterName ); 455 return xP; 456 } 457 458 459 460