Update ooo320-m1
[ooovba.git] / toolkit / source / awt / vclxprinter.cxx
blobc05b15711ed311f2044df66f274f15623a795762
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxprinter.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
35 #include <toolkit/awt/vclxprinter.hxx>
36 #include <toolkit/helper/macros.hxx>
37 #include <cppuhelper/typeprovider.hxx>
38 #include <rtl/memory.h>
39 #include <rtl/uuid.h>
42 #include <vcl/print.hxx>
43 #include <vcl/jobset.hxx>
44 #include <vcl/svapp.hxx>
46 #include <tools/debug.hxx>
47 #include <tools/stream.hxx>
49 #include <toolkit/awt/vclxdevice.hxx>
52 #define BINARYSETUPMARKER 0x23864691
54 #define PROPERTY_Orientation 0
55 #define PROPERTY_Horizontal 1
57 ::com::sun::star::beans::Property* ImplGetProperties( sal_uInt16& rElementCount )
59 static ::com::sun::star::beans::Property* pProperties = NULL;
60 static sal_uInt16 nElements = 0;
61 if( !pProperties )
63 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
64 if( !pProperties )
66 static ::com::sun::star::beans::Property __FAR_DATA aPropTable[] =
68 ::com::sun::star::beans::Property( ::rtl::OUString::createFromAscii( "Orientation" ), PROPERTY_Orientation, ::getCppuType((const sal_Int16*)0), 0 ),
69 ::com::sun::star::beans::Property( ::rtl::OUString::createFromAscii( "Horizontal" ), PROPERTY_Horizontal, ::getBooleanCppuType(), 0 )
71 pProperties = aPropTable;
72 nElements = sizeof( aPropTable ) / sizeof( ::com::sun::star::beans::Property );
75 rElementCount = nElements;
76 return pProperties;
79 // ----------------------------------------------------
80 // class VCLXPrinterPropertySet
81 // ----------------------------------------------------
83 // ::com::sun::star::uno::XInterface
84 ::com::sun::star::uno::Any VCLXPrinterPropertySet::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
86 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
87 SAL_STATIC_CAST( ::com::sun::star::beans::XMultiPropertySet*, this ),
88 SAL_STATIC_CAST( ::com::sun::star::beans::XFastPropertySet*, this ),
89 SAL_STATIC_CAST( ::com::sun::star::beans::XPropertySet*, (::cppu::OPropertySetHelper*) this ),
90 SAL_STATIC_CAST( ::com::sun::star::awt::XPrinterPropertySet*, this ),
91 SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider*, this ) );
92 return (aRet.hasValue() ? aRet : OPropertySetHelper::queryInterface( rType ));
95 // ::com::sun::star::lang::XTypeProvider
96 IMPL_XTYPEPROVIDER_START( VCLXPrinterPropertySet )
97 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>* ) NULL ),
98 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>* ) NULL ),
99 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>* ) NULL ),
100 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinterPropertySet>* ) NULL )
101 IMPL_XTYPEPROVIDER_END
103 VCLXPrinterPropertySet::VCLXPrinterPropertySet( const String& rPrinterName )
104 : OPropertySetHelper( BrdcstHelper )
106 osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
108 mpPrinter = new Printer( rPrinterName );
109 mnOrientation = 0;
110 mbHorizontal = sal_False;
113 VCLXPrinterPropertySet::~VCLXPrinterPropertySet()
115 osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
117 delete mpPrinter;
120 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinterPropertySet::GetDevice()
122 if ( !mxPrnDevice.is() )
124 VCLXDevice* pDev = new VCLXDevice;
125 pDev->SetOutputDevice( GetPrinter() );
126 mxPrnDevice = pDev;
128 return mxPrnDevice;
131 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > VCLXPrinterPropertySet::getPropertySetInfo( ) throw(::com::sun::star::uno::RuntimeException)
133 static ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
134 return xInfo;
137 ::cppu::IPropertyArrayHelper& VCLXPrinterPropertySet::getInfoHelper()
139 static ::cppu::OPropertyArrayHelper* pPropertyArrayHelper = NULL;
140 if ( !pPropertyArrayHelper )
142 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
143 if( !pPropertyArrayHelper )
145 sal_uInt16 nElements;
146 ::com::sun::star::beans::Property* pProps = ImplGetProperties( nElements );
147 pPropertyArrayHelper = new ::cppu::OPropertyArrayHelper( pProps, nElements, sal_False );
150 return *pPropertyArrayHelper ;
153 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)
155 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
157 sal_Bool bDifferent = sal_False;
158 switch ( nHandle )
160 case PROPERTY_Orientation:
162 sal_Int16 n;
163 if( ( rValue >>= n ) && ( n != mnOrientation ) )
165 rConvertedValue <<= n;
166 rOldValue <<= mnOrientation;
167 bDifferent = sal_True;
170 break;
171 case PROPERTY_Horizontal:
173 sal_Bool b;
174 if( ( rValue >>= b ) && ( b != mbHorizontal ) )
176 rConvertedValue <<= b;
177 rOldValue <<= mbHorizontal;
178 bDifferent = sal_True;
181 break;
182 default:
184 DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
187 return bDifferent;
190 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::uno::Exception)
192 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
194 switch( nHandle )
196 case PROPERTY_Orientation:
198 rValue >>= mnOrientation;
200 break;
201 case PROPERTY_Horizontal:
203 rValue >>= mbHorizontal;
205 break;
206 default:
208 DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
213 void VCLXPrinterPropertySet::getFastPropertyValue( ::com::sun::star::uno::Any& rValue, sal_Int32 nHandle ) const
215 ::osl::Guard< ::osl::Mutex > aGuard( ((VCLXPrinterPropertySet*)this)->Mutex );
217 switch( nHandle )
219 case PROPERTY_Orientation:
220 rValue <<= mnOrientation;
221 break;
222 case PROPERTY_Horizontal:
223 rValue <<= mbHorizontal;
224 break;
225 default:
227 DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
232 // ::com::sun::star::awt::XPrinterPropertySet
233 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
235 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
237 ::com::sun::star::uno::Any aValue;
238 aValue <<= bHorizontal;
239 setFastPropertyValue( PROPERTY_Horizontal, aValue );
242 ::com::sun::star::uno::Sequence< ::rtl::OUString > VCLXPrinterPropertySet::getFormDescriptions( ) throw(::com::sun::star::uno::RuntimeException)
244 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
246 sal_uInt16 nPaperBinCount = GetPrinter()->GetPaperBinCount();
247 ::com::sun::star::uno::Sequence< ::rtl::OUString > aDescriptions( nPaperBinCount );
248 for ( sal_uInt16 n = 0; n < nPaperBinCount; n++ )
250 // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
251 String aDescr( RTL_CONSTASCII_USTRINGPARAM( "*;*;" ) );
252 aDescr += GetPrinter()->GetPaperBinName( n );
253 aDescr += ';';
254 aDescr += n;
255 aDescr.AppendAscii( ";*;*", 4 );
257 aDescriptions.getArray()[n] = aDescr;
259 return aDescriptions;
262 void VCLXPrinterPropertySet::selectForm( const ::rtl::OUString& rFormDescription ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
264 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
266 sal_Int32 nIndex = 0;
267 sal_uInt16 nPaperBin = sal::static_int_cast< sal_uInt16 >(
268 rFormDescription.getToken( 3, ';', nIndex ).toInt32());
269 GetPrinter()->SetPaperBin( nPaperBin );
272 ::com::sun::star::uno::Sequence< sal_Int8 > VCLXPrinterPropertySet::getBinarySetup( ) throw(::com::sun::star::uno::RuntimeException)
274 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
276 SvMemoryStream aMem;
277 aMem << BINARYSETUPMARKER;
278 aMem << GetPrinter()->GetJobSetup();
279 return ::com::sun::star::uno::Sequence<sal_Int8>( (sal_Int8*) aMem.GetData(), aMem.Tell() );
282 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)
284 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
286 SvMemoryStream aMem( (char*) data.getConstArray(), data.getLength(), STREAM_READ );
287 sal_uInt32 nMarker;
288 aMem >> nMarker;
289 DBG_ASSERT( nMarker == BINARYSETUPMARKER, "setBinarySetup - invalid!" );
290 if ( nMarker == BINARYSETUPMARKER )
292 JobSetup aSetup;
293 aMem >> aSetup;
294 GetPrinter()->SetJobSetup( aSetup );
299 // ----------------------------------------------------
300 // class VCLXPrinter
301 // ----------------------------------------------------
302 VCLXPrinter::VCLXPrinter( const String& rPrinterName )
303 : VCLXPrinterPropertySet( rPrinterName )
307 VCLXPrinter::~VCLXPrinter()
311 // ::com::sun::star::uno::XInterface
312 ::com::sun::star::uno::Any VCLXPrinter::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
314 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
315 SAL_STATIC_CAST( ::com::sun::star::awt::XPrinter*, this ) );
317 if ( !aRet.hasValue() )
318 aRet = VCLXPrinterPropertySet::queryInterface( rType );
320 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
323 // ::com::sun::star::lang::XTypeProvider
324 IMPL_XTYPEPROVIDER_START( VCLXPrinter )
325 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter>* ) NULL ),
326 VCLXPrinterPropertySet::getTypes()
327 IMPL_XTYPEPROVIDER_END
329 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)
331 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
333 sal_Bool bDone = sal_True;
334 if ( GetPrinter() )
335 bDone = GetPrinter()->StartJob( rJobName );
337 return bDone;
340 void VCLXPrinter::end( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException)
342 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
344 if ( GetPrinter() )
345 GetPrinter()->EndJob();
348 void VCLXPrinter::terminate( ) throw(::com::sun::star::uno::RuntimeException)
350 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
352 if ( GetPrinter() )
353 GetPrinter()->AbortJob();
356 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinter::startPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException)
358 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
360 if ( GetPrinter() )
361 GetPrinter()->StartPage();
362 return GetDevice();
365 void VCLXPrinter::endPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException)
367 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
369 if ( GetPrinter() )
370 GetPrinter()->EndPage();
374 // ----------------------------------------------------
375 // class VCLXInfoPrinter
376 // ----------------------------------------------------
378 VCLXInfoPrinter::VCLXInfoPrinter( const String& rPrinterName )
379 : VCLXPrinterPropertySet( rPrinterName )
383 VCLXInfoPrinter::~VCLXInfoPrinter()
387 // ::com::sun::star::uno::XInterface
388 ::com::sun::star::uno::Any VCLXInfoPrinter::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
390 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
391 SAL_STATIC_CAST( ::com::sun::star::awt::XInfoPrinter*, this ) );
393 if ( !aRet.hasValue() )
394 aRet = VCLXPrinterPropertySet::queryInterface( rType );
396 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
399 // ::com::sun::star::lang::XTypeProvider
400 IMPL_XTYPEPROVIDER_START( VCLXInfoPrinter )
401 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter>* ) NULL ),
402 VCLXPrinterPropertySet::getTypes()
403 IMPL_XTYPEPROVIDER_END
405 // ::com::sun::star::awt::XInfoPrinter
406 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXInfoPrinter::createDevice( ) throw(::com::sun::star::uno::RuntimeException)
408 ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
410 return GetDevice();
413 // ----------------------------------------------------
414 // class VCLXPrinterServer
415 // ----------------------------------------------------
417 // ::com::sun::star::uno::XInterface
418 ::com::sun::star::uno::Any VCLXPrinterServer::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
420 ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
421 SAL_STATIC_CAST( ::com::sun::star::awt::XPrinterServer*, this ) );
422 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
425 // ::com::sun::star::lang::XTypeProvider
426 IMPL_XTYPEPROVIDER_START( VCLXPrinterServer )
427 getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinterServer>* ) NULL )
428 IMPL_XTYPEPROVIDER_END
430 // ::com::sun::star::awt::XPrinterServer
431 ::com::sun::star::uno::Sequence< ::rtl::OUString > VCLXPrinterServer::getPrinterNames( ) throw(::com::sun::star::uno::RuntimeException)
433 const std::vector<rtl::OUString>& rQueues = Printer::GetPrinterQueues();
434 sal_uInt32 nPrinters = rQueues.size();
436 ::com::sun::star::uno::Sequence< ::rtl::OUString > aNames( nPrinters );
437 for ( sal_uInt32 n = 0; n < nPrinters; n++ )
438 aNames.getArray()[n] = rQueues[n];
440 return aNames;
443 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > VCLXPrinterServer::createPrinter( const ::rtl::OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException)
445 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > xP;
446 xP = new VCLXPrinter( rPrinterName );
447 return xP;
450 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > VCLXPrinterServer::createInfoPrinter( const ::rtl::OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException)
452 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > xP;
453 xP = new VCLXInfoPrinter( rPrinterName );
454 return xP;