lwp build fixes
[LibreOffice.git] / toolkit / source / awt / vclxprinter.cxx
blob32e2c20f60969cefcdac369f6deb86a7803301af
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/uno/XComponentContext.hpp>
21 #include <toolkit/awt/vclxprinter.hxx>
22 #include <toolkit/helper/macros.hxx>
23 #include <cppuhelper/typeprovider.hxx>
24 #include <rtl/uuid.h>
27 #include <vcl/print.hxx>
28 #include <vcl/jobset.hxx>
29 #include <vcl/svapp.hxx>
31 #include <tools/debug.hxx>
32 #include <tools/stream.hxx>
34 #include <toolkit/awt/vclxdevice.hxx>
37 #define BINARYSETUPMARKER 0x23864691
39 #define PROPERTY_Orientation 0
40 #define PROPERTY_Horizontal 1
42 ::com::sun::star::beans::Property* ImplGetProperties( sal_uInt16& rElementCount )
44 static ::com::sun::star::beans::Property* pProperties = NULL;
45 static sal_uInt16 nElements = 0;
46 if( !pProperties )
48 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
49 if( !pProperties )
51 static ::com::sun::star::beans::Property aPropTable[] =
53 ::com::sun::star::beans::Property( OUString("Orientation"), PROPERTY_Orientation, cppu::UnoType<sal_Int16>::get(), 0 ),
54 ::com::sun::star::beans::Property( OUString("Horizontal"), PROPERTY_Horizontal, ::getBooleanCppuType(), 0 )
56 pProperties = aPropTable;
57 nElements = sizeof( aPropTable ) / sizeof( ::com::sun::star::beans::Property );
60 rElementCount = nElements;
61 return pProperties;
64 // ----------------------------------------------------
65 // class VCLXPrinterPropertySet
66 // ----------------------------------------------------
68 IMPLEMENT_FORWARD_XINTERFACE2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, OPropertySetHelper )
69 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, ::cppu::OPropertySetHelper )
71 VCLXPrinterPropertySet::VCLXPrinterPropertySet( const OUString& rPrinterName )
72 : OPropertySetHelper( BrdcstHelper )
73 , mpPrinter( new Printer( rPrinterName ) )
75 SolarMutexGuard aSolarGuard;
77 mnOrientation = 0;
78 mbHorizontal = false;
81 VCLXPrinterPropertySet::~VCLXPrinterPropertySet()
83 SolarMutexGuard aSolarGuard;
84 mpPrinter.reset();
87 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinterPropertySet::GetDevice()
89 if ( !mxPrnDevice.is() )
91 VCLXDevice* pDev = new VCLXDevice;
92 pDev->SetOutputDevice( GetPrinter() );
93 mxPrnDevice = pDev;
95 return mxPrnDevice;
98 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > VCLXPrinterPropertySet::getPropertySetInfo( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
100 static ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
101 return xInfo;
104 ::cppu::IPropertyArrayHelper& VCLXPrinterPropertySet::getInfoHelper()
106 static ::cppu::OPropertyArrayHelper* pPropertyArrayHelper = NULL;
107 if ( !pPropertyArrayHelper )
109 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
110 if( !pPropertyArrayHelper )
112 sal_uInt16 nElements;
113 ::com::sun::star::beans::Property* pProps = ImplGetProperties( nElements );
114 pPropertyArrayHelper = new ::cppu::OPropertyArrayHelper( pProps, nElements, sal_False );
117 return *pPropertyArrayHelper ;
120 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)
122 ::osl::MutexGuard aGuard( Mutex );
124 bool bDifferent = false;
125 switch ( nHandle )
127 case PROPERTY_Orientation:
129 sal_Int16 n;
130 if( ( rValue >>= n ) && ( n != mnOrientation ) )
132 rConvertedValue <<= n;
133 rOldValue <<= mnOrientation;
134 bDifferent = true;
137 break;
138 case PROPERTY_Horizontal:
140 bool b;
141 if( ( rValue >>= b ) && ( b != mbHorizontal ) )
143 rConvertedValue <<= b;
144 rOldValue <<= mbHorizontal;
145 bDifferent = true;
148 break;
149 default:
151 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
154 return bDifferent;
157 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::uno::Exception, std::exception)
159 ::osl::MutexGuard aGuard( Mutex );
161 switch( nHandle )
163 case PROPERTY_Orientation:
165 rValue >>= mnOrientation;
167 break;
168 case PROPERTY_Horizontal:
170 rValue >>= mbHorizontal;
172 break;
173 default:
175 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
180 void VCLXPrinterPropertySet::getFastPropertyValue( ::com::sun::star::uno::Any& rValue, sal_Int32 nHandle ) const
182 ::osl::MutexGuard aGuard( ((VCLXPrinterPropertySet*)this)->Mutex );
184 switch( nHandle )
186 case PROPERTY_Orientation:
187 rValue <<= mnOrientation;
188 break;
189 case PROPERTY_Horizontal:
190 rValue <<= mbHorizontal;
191 break;
192 default:
194 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
199 // ::com::sun::star::awt::XPrinterPropertySet
200 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
202 ::osl::MutexGuard aGuard( Mutex );
204 ::com::sun::star::uno::Any aValue;
205 aValue <<= bHorizontal;
206 setFastPropertyValue( PROPERTY_Horizontal, aValue );
209 ::com::sun::star::uno::Sequence< OUString > VCLXPrinterPropertySet::getFormDescriptions( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
211 ::osl::MutexGuard aGuard( Mutex );
213 sal_uInt16 nPaperBinCount = GetPrinter()->GetPaperBinCount();
214 ::com::sun::star::uno::Sequence< OUString > aDescriptions( nPaperBinCount );
215 for ( sal_uInt16 n = 0; n < nPaperBinCount; n++ )
217 // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
218 OUStringBuffer aDescr( "*;*;" );
219 aDescr.append(GetPrinter()->GetPaperBinName( n ));
220 aDescr.append(';');
221 aDescr.append(OUString::number(n));
222 aDescr.append(";*;*");
224 aDescriptions.getArray()[n] = aDescr.makeStringAndClear();
226 return aDescriptions;
229 void VCLXPrinterPropertySet::selectForm( const OUString& rFormDescription ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
231 ::osl::MutexGuard aGuard( Mutex );
233 sal_Int32 nIndex = 0;
234 sal_uInt16 nPaperBin = sal::static_int_cast< sal_uInt16 >(
235 rFormDescription.getToken( 3, ';', nIndex ).toInt32());
236 GetPrinter()->SetPaperBin( nPaperBin );
239 ::com::sun::star::uno::Sequence< sal_Int8 > VCLXPrinterPropertySet::getBinarySetup( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
241 ::osl::MutexGuard aGuard( Mutex );
243 SvMemoryStream aMem;
244 aMem.WriteUInt32( BINARYSETUPMARKER );
245 WriteJobSetup( aMem, GetPrinter()->GetJobSetup() );
246 return ::com::sun::star::uno::Sequence<sal_Int8>( (sal_Int8*) aMem.GetData(), aMem.Tell() );
249 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, std::exception)
251 ::osl::MutexGuard aGuard( Mutex );
253 SvMemoryStream aMem( (char*) data.getConstArray(), data.getLength(), STREAM_READ );
254 sal_uInt32 nMarker;
255 aMem.ReadUInt32( nMarker );
256 DBG_ASSERT( nMarker == BINARYSETUPMARKER, "setBinarySetup - invalid!" );
257 if ( nMarker == BINARYSETUPMARKER )
259 JobSetup aSetup;
260 ReadJobSetup( aMem, aSetup );
261 GetPrinter()->SetJobSetup( aSetup );
266 // ----------------------------------------------------
267 // class VCLXPrinter
268 // ----------------------------------------------------
269 VCLXPrinter::VCLXPrinter( const OUString& rPrinterName )
270 : VCLXPrinter_Base( rPrinterName )
274 VCLXPrinter::~VCLXPrinter()
278 sal_Bool VCLXPrinter::start( const OUString& /*rJobName*/, sal_Int16 /*nCopies*/, sal_Bool /*bCollate*/ ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
280 ::osl::MutexGuard aGuard( Mutex );
282 bool bDone = true;
283 if ( mpPrinter.get() )
285 maInitJobSetup = mpPrinter->GetJobSetup();
286 mpListener.reset( new vcl::OldStylePrintAdaptor( mpPrinter ) );
289 return bDone;
292 void VCLXPrinter::end( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException, std::exception)
294 ::osl::MutexGuard aGuard( Mutex );
296 if ( mpListener.get() )
298 Printer::PrintJob( mpListener, maInitJobSetup );
299 mpListener.reset();
303 void VCLXPrinter::terminate( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
305 ::osl::MutexGuard aGuard( Mutex );
307 mpListener.reset();
310 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinter::startPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException, std::exception)
312 ::osl::MutexGuard aGuard( Mutex );
314 if ( mpListener.get() )
316 mpListener->StartPage();
318 return GetDevice();
321 void VCLXPrinter::endPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException, std::exception)
323 ::osl::MutexGuard aGuard( Mutex );
325 if ( mpListener.get() )
327 mpListener->EndPage();
332 // ----------------------------------------------------
333 // class VCLXInfoPrinter
334 // ----------------------------------------------------
336 VCLXInfoPrinter::VCLXInfoPrinter( const OUString& rPrinterName )
337 : VCLXInfoPrinter_Base( rPrinterName )
341 VCLXInfoPrinter::~VCLXInfoPrinter()
345 // ::com::sun::star::awt::XInfoPrinter
346 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXInfoPrinter::createDevice( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
348 ::osl::MutexGuard aGuard( Mutex );
350 return GetDevice();
353 // ----------------------------------------------------
354 // class VCLXPrinterServer
355 // ----------------------------------------------------
357 // ::com::sun::star::awt::XPrinterServer
358 ::com::sun::star::uno::Sequence< OUString > VCLXPrinterServer::getPrinterNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
360 const std::vector<OUString>& rQueues = Printer::GetPrinterQueues();
361 sal_uInt32 nPrinters = rQueues.size();
363 ::com::sun::star::uno::Sequence< OUString > aNames( nPrinters );
364 for ( sal_uInt32 n = 0; n < nPrinters; n++ )
365 aNames.getArray()[n] = rQueues[n];
367 return aNames;
370 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > VCLXPrinterServer::createPrinter( const OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
372 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > xP;
373 xP = new VCLXPrinter( rPrinterName );
374 return xP;
377 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > VCLXPrinterServer::createInfoPrinter( const OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
379 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > xP;
380 xP = new VCLXInfoPrinter( rPrinterName );
381 return xP;
384 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
385 stardiv_Toolkit_VCLXPrinterServer_get_implementation(
386 css::uno::XComponentContext *,
387 css::uno::Sequence<css::uno::Any> const &)
389 return cppu::acquire(new VCLXPrinterServer());
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */