1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/supportsservice.hxx>
24 #include <cppuhelper/typeprovider.hxx>
28 #include <vcl/print.hxx>
29 #include <vcl/jobset.hxx>
30 #include <vcl/svapp.hxx>
32 #include <tools/debug.hxx>
33 #include <tools/stream.hxx>
35 #include <toolkit/awt/vclxdevice.hxx>
38 #define BINARYSETUPMARKER 0x23864691
40 #define PROPERTY_Orientation 0
41 #define PROPERTY_Horizontal 1
43 ::com::sun::star::beans::Property
* ImplGetProperties( sal_uInt16
& rElementCount
)
45 static ::com::sun::star::beans::Property
* pProperties
= NULL
;
46 static sal_uInt16 nElements
= 0;
49 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
52 static ::com::sun::star::beans::Property aPropTable
[] =
54 ::com::sun::star::beans::Property( OUString("Orientation"), PROPERTY_Orientation
, cppu::UnoType
<sal_Int16
>::get(), 0 ),
55 ::com::sun::star::beans::Property( OUString("Horizontal"), PROPERTY_Horizontal
, cppu::UnoType
<bool>::get(), 0 )
57 pProperties
= aPropTable
;
58 nElements
= sizeof( aPropTable
) / sizeof( ::com::sun::star::beans::Property
);
61 rElementCount
= nElements
;
65 // ----------------------------------------------------
66 // class VCLXPrinterPropertySet
67 // ----------------------------------------------------
69 IMPLEMENT_FORWARD_XINTERFACE2( VCLXPrinterPropertySet
, VCLXPrinterPropertySet_Base
, OPropertySetHelper
)
70 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXPrinterPropertySet
, VCLXPrinterPropertySet_Base
, ::cppu::OPropertySetHelper
)
72 VCLXPrinterPropertySet::VCLXPrinterPropertySet( const OUString
& rPrinterName
)
73 : OPropertySetHelper( BrdcstHelper
)
74 , mxPrinter(VclPtrInstance
< Printer
>(rPrinterName
))
76 SolarMutexGuard aSolarGuard
;
82 VCLXPrinterPropertySet::~VCLXPrinterPropertySet()
84 SolarMutexGuard aSolarGuard
;
88 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXPrinterPropertySet::GetDevice()
90 if ( !mxPrnDevice
.is() )
92 VCLXDevice
* pDev
= new VCLXDevice
;
93 pDev
->SetOutputDevice( GetPrinter() );
99 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySetInfo
> VCLXPrinterPropertySet::getPropertySetInfo( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
101 static ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
105 ::cppu::IPropertyArrayHelper
& VCLXPrinterPropertySet::getInfoHelper()
107 static ::cppu::OPropertyArrayHelper
* pPropertyArrayHelper
= NULL
;
108 if ( !pPropertyArrayHelper
)
110 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
111 if( !pPropertyArrayHelper
)
113 sal_uInt16 nElements
;
114 ::com::sun::star::beans::Property
* pProps
= ImplGetProperties( nElements
);
115 pPropertyArrayHelper
= new ::cppu::OPropertyArrayHelper( pProps
, nElements
, sal_False
);
118 return *pPropertyArrayHelper
;
121 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
)
123 ::osl::MutexGuard
aGuard( Mutex
);
125 bool bDifferent
= false;
128 case PROPERTY_Orientation
:
131 if( ( rValue
>>= n
) && ( n
!= mnOrientation
) )
133 rConvertedValue
<<= n
;
134 rOldValue
<<= mnOrientation
;
139 case PROPERTY_Horizontal
:
142 if( ( rValue
>>= b
) && ( b
!= mbHorizontal
) )
144 rConvertedValue
<<= b
;
145 rOldValue
<<= mbHorizontal
;
152 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
158 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const ::com::sun::star::uno::Any
& rValue
) throw (::com::sun::star::uno::Exception
, std::exception
)
160 ::osl::MutexGuard
aGuard( Mutex
);
164 case PROPERTY_Orientation
:
166 rValue
>>= mnOrientation
;
169 case PROPERTY_Horizontal
:
171 rValue
>>= mbHorizontal
;
176 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
181 void VCLXPrinterPropertySet::getFastPropertyValue( ::com::sun::star::uno::Any
& rValue
, sal_Int32 nHandle
) const
183 ::osl::MutexGuard
aGuard( const_cast<VCLXPrinterPropertySet
*>(this)->Mutex
);
187 case PROPERTY_Orientation
:
188 rValue
<<= mnOrientation
;
190 case PROPERTY_Horizontal
:
191 rValue
<<= mbHorizontal
;
195 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
200 // ::com::sun::star::awt::XPrinterPropertySet
201 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal
) throw(::com::sun::star::beans::PropertyVetoException
, ::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
203 ::osl::MutexGuard
aGuard( Mutex
);
205 ::com::sun::star::uno::Any aValue
;
206 aValue
<<= bHorizontal
;
207 setFastPropertyValue( PROPERTY_Horizontal
, aValue
);
210 ::com::sun::star::uno::Sequence
< OUString
> VCLXPrinterPropertySet::getFormDescriptions( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
212 ::osl::MutexGuard
aGuard( Mutex
);
214 sal_uInt16 nPaperBinCount
= GetPrinter()->GetPaperBinCount();
215 ::com::sun::star::uno::Sequence
< OUString
> aDescriptions( nPaperBinCount
);
216 for ( sal_uInt16 n
= 0; n
< nPaperBinCount
; n
++ )
218 // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
219 OUStringBuffer
aDescr( "*;*;" );
220 aDescr
.append(GetPrinter()->GetPaperBinName( n
));
222 aDescr
.append(OUString::number(n
));
223 aDescr
.append(";*;*");
225 aDescriptions
.getArray()[n
] = aDescr
.makeStringAndClear();
227 return aDescriptions
;
230 void VCLXPrinterPropertySet::selectForm( const OUString
& rFormDescription
) throw(::com::sun::star::beans::PropertyVetoException
, ::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
232 ::osl::MutexGuard
aGuard( Mutex
);
234 sal_Int32 nIndex
= 0;
235 sal_uInt16 nPaperBin
= sal::static_int_cast
< sal_uInt16
>(
236 rFormDescription
.getToken( 3, ';', nIndex
).toInt32());
237 GetPrinter()->SetPaperBin( nPaperBin
);
240 ::com::sun::star::uno::Sequence
< sal_Int8
> VCLXPrinterPropertySet::getBinarySetup( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
242 ::osl::MutexGuard
aGuard( Mutex
);
245 aMem
.WriteUInt32( BINARYSETUPMARKER
);
246 WriteJobSetup( aMem
, GetPrinter()->GetJobSetup() );
247 return ::com::sun::star::uno::Sequence
<sal_Int8
>( static_cast<sal_Int8
const *>(aMem
.GetData()), aMem
.Tell() );
250 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
)
252 ::osl::MutexGuard
aGuard( Mutex
);
254 SvMemoryStream
aMem( const_cast<signed char*>(data
.getConstArray()), data
.getLength(), StreamMode::READ
);
256 aMem
.ReadUInt32( nMarker
);
257 DBG_ASSERT( nMarker
== BINARYSETUPMARKER
, "setBinarySetup - invalid!" );
258 if ( nMarker
== BINARYSETUPMARKER
)
261 ReadJobSetup( aMem
, aSetup
);
262 GetPrinter()->SetJobSetup( aSetup
);
267 // ----------------------------------------------------
269 // ----------------------------------------------------
270 VCLXPrinter::VCLXPrinter( const OUString
& rPrinterName
)
271 : VCLXPrinter_Base( rPrinterName
)
275 VCLXPrinter::~VCLXPrinter()
279 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
)
281 ::osl::MutexGuard
aGuard( Mutex
);
286 maInitJobSetup
= mxPrinter
->GetJobSetup();
287 mxListener
.reset(new vcl::OldStylePrintAdaptor(mxPrinter
));
293 void VCLXPrinter::end( ) throw(::com::sun::star::awt::PrinterException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
295 ::osl::MutexGuard
aGuard( Mutex
);
297 if (mxListener
.get())
299 Printer::PrintJob(mxListener
, maInitJobSetup
);
304 void VCLXPrinter::terminate( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
306 ::osl::MutexGuard
aGuard( Mutex
);
311 ::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
)
313 ::osl::MutexGuard
aGuard( Mutex
);
315 if (mxListener
.get())
317 mxListener
->StartPage();
322 void VCLXPrinter::endPage( ) throw(::com::sun::star::awt::PrinterException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
324 ::osl::MutexGuard
aGuard( Mutex
);
326 if (mxListener
.get())
328 mxListener
->EndPage();
333 // ----------------------------------------------------
334 // class VCLXInfoPrinter
335 // ----------------------------------------------------
337 VCLXInfoPrinter::VCLXInfoPrinter( const OUString
& rPrinterName
)
338 : VCLXInfoPrinter_Base( rPrinterName
)
342 VCLXInfoPrinter::~VCLXInfoPrinter()
346 // ::com::sun::star::awt::XInfoPrinter
347 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXInfoPrinter::createDevice( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
349 ::osl::MutexGuard
aGuard( Mutex
);
354 // ----------------------------------------------------
355 // class VCLXPrinterServer
356 // ----------------------------------------------------
358 // ::com::sun::star::awt::XPrinterServer
359 ::com::sun::star::uno::Sequence
< OUString
> VCLXPrinterServer::getPrinterNames( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
361 const std::vector
<OUString
>& rQueues
= Printer::GetPrinterQueues();
362 sal_uInt32 nPrinters
= rQueues
.size();
364 ::com::sun::star::uno::Sequence
< OUString
> aNames( nPrinters
);
365 for ( sal_uInt32 n
= 0; n
< nPrinters
; n
++ )
366 aNames
.getArray()[n
] = rQueues
[n
];
371 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPrinter
> VCLXPrinterServer::createPrinter( const OUString
& rPrinterName
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
373 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPrinter
> xP
;
374 xP
= new VCLXPrinter( rPrinterName
);
378 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XInfoPrinter
> VCLXPrinterServer::createInfoPrinter( const OUString
& rPrinterName
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
380 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XInfoPrinter
> xP
;
381 xP
= new VCLXInfoPrinter( rPrinterName
);
385 OUString
VCLXPrinterServer::getImplementationName()
386 throw (css::uno::RuntimeException
, std::exception
)
388 return OUString("stardiv.Toolkit.VCLXPrinterServer");
391 sal_Bool
VCLXPrinterServer::supportsService(OUString
const & ServiceName
)
392 throw (css::uno::RuntimeException
, std::exception
)
394 return cppu::supportsService(this, ServiceName
);
397 css::uno::Sequence
<OUString
> VCLXPrinterServer::getSupportedServiceNames()
398 throw (css::uno::RuntimeException
, std::exception
)
400 return css::uno::Sequence
<OUString
>{
401 "com.sun.star.awt.PrinterServer", "stardiv.vcl.PrinterServer"};
404 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
405 stardiv_Toolkit_VCLXPrinterServer_get_implementation(
406 css::uno::XComponentContext
*,
407 css::uno::Sequence
<css::uno::Any
> const &)
409 return cppu::acquire(new VCLXPrinterServer());
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */