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 <toolkit/awt/vclxprinter.hxx>
21 #include <toolkit/helper/macros.hxx>
22 #include <cppuhelper/typeprovider.hxx>
26 #include <vcl/print.hxx>
27 #include <vcl/jobset.hxx>
28 #include <vcl/svapp.hxx>
30 #include <tools/debug.hxx>
31 #include <tools/stream.hxx>
33 #include <toolkit/awt/vclxdevice.hxx>
36 #define BINARYSETUPMARKER 0x23864691
38 #define PROPERTY_Orientation 0
39 #define PROPERTY_Horizontal 1
41 ::com::sun::star::beans::Property
* ImplGetProperties( sal_uInt16
& rElementCount
)
43 static ::com::sun::star::beans::Property
* pProperties
= NULL
;
44 static sal_uInt16 nElements
= 0;
47 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
50 static ::com::sun::star::beans::Property aPropTable
[] =
52 ::com::sun::star::beans::Property( ::rtl::OUString("Orientation"), PROPERTY_Orientation
, ::getCppuType((const sal_Int16
*)0), 0 ),
53 ::com::sun::star::beans::Property( ::rtl::OUString("Horizontal"), PROPERTY_Horizontal
, ::getBooleanCppuType(), 0 )
55 pProperties
= aPropTable
;
56 nElements
= sizeof( aPropTable
) / sizeof( ::com::sun::star::beans::Property
);
59 rElementCount
= nElements
;
63 // ----------------------------------------------------
64 // class VCLXPrinterPropertySet
65 // ----------------------------------------------------
67 // ::com::sun::star::uno::XInterface
68 ::com::sun::star::uno::Any
VCLXPrinterPropertySet::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
70 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
71 (static_cast< ::com::sun::star::beans::XMultiPropertySet
* >(this)),
72 (static_cast< ::com::sun::star::beans::XFastPropertySet
* >(this)),
73 (static_cast< ::com::sun::star::beans::XPropertySet
* >((::cppu::OPropertySetHelper
*) this) ),
74 (static_cast< ::com::sun::star::awt::XPrinterPropertySet
* >(this)),
75 (static_cast< ::com::sun::star::lang::XTypeProvider
* >(this)) );
76 return (aRet
.hasValue() ? aRet
: OPropertySetHelper::queryInterface( rType
));
79 // ::com::sun::star::lang::XTypeProvider
80 IMPL_XTYPEPROVIDER_START( VCLXPrinterPropertySet
)
81 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XMultiPropertySet
>* ) NULL
),
82 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XFastPropertySet
>* ) NULL
),
83 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>* ) NULL
),
84 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPrinterPropertySet
>* ) NULL
)
85 IMPL_XTYPEPROVIDER_END
87 VCLXPrinterPropertySet::VCLXPrinterPropertySet( const String
& rPrinterName
)
88 : OPropertySetHelper( BrdcstHelper
)
89 , mpPrinter( new Printer( rPrinterName
) )
91 SolarMutexGuard aSolarGuard
;
94 mbHorizontal
= sal_False
;
97 VCLXPrinterPropertySet::~VCLXPrinterPropertySet()
99 SolarMutexGuard aSolarGuard
;
103 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXPrinterPropertySet::GetDevice()
105 if ( !mxPrnDevice
.is() )
107 VCLXDevice
* pDev
= new VCLXDevice
;
108 pDev
->SetOutputDevice( GetPrinter() );
114 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySetInfo
> VCLXPrinterPropertySet::getPropertySetInfo( ) throw(::com::sun::star::uno::RuntimeException
)
116 static ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
120 ::cppu::IPropertyArrayHelper
& VCLXPrinterPropertySet::getInfoHelper()
122 static ::cppu::OPropertyArrayHelper
* pPropertyArrayHelper
= NULL
;
123 if ( !pPropertyArrayHelper
)
125 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
126 if( !pPropertyArrayHelper
)
128 sal_uInt16 nElements
;
129 ::com::sun::star::beans::Property
* pProps
= ImplGetProperties( nElements
);
130 pPropertyArrayHelper
= new ::cppu::OPropertyArrayHelper( pProps
, nElements
, sal_False
);
133 return *pPropertyArrayHelper
;
136 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
)
138 ::osl::MutexGuard
aGuard( Mutex
);
140 sal_Bool bDifferent
= sal_False
;
143 case PROPERTY_Orientation
:
146 if( ( rValue
>>= n
) && ( n
!= mnOrientation
) )
148 rConvertedValue
<<= n
;
149 rOldValue
<<= mnOrientation
;
150 bDifferent
= sal_True
;
154 case PROPERTY_Horizontal
:
157 if( ( rValue
>>= b
) && ( b
!= mbHorizontal
) )
159 rConvertedValue
<<= b
;
160 rOldValue
<<= mbHorizontal
;
161 bDifferent
= sal_True
;
167 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
173 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const ::com::sun::star::uno::Any
& rValue
) throw (::com::sun::star::uno::Exception
)
175 ::osl::MutexGuard
aGuard( Mutex
);
179 case PROPERTY_Orientation
:
181 rValue
>>= mnOrientation
;
184 case PROPERTY_Horizontal
:
186 rValue
>>= mbHorizontal
;
191 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
196 void VCLXPrinterPropertySet::getFastPropertyValue( ::com::sun::star::uno::Any
& rValue
, sal_Int32 nHandle
) const
198 ::osl::MutexGuard
aGuard( ((VCLXPrinterPropertySet
*)this)->Mutex
);
202 case PROPERTY_Orientation
:
203 rValue
<<= mnOrientation
;
205 case PROPERTY_Horizontal
:
206 rValue
<<= mbHorizontal
;
210 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
215 // ::com::sun::star::awt::XPrinterPropertySet
216 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal
) throw(::com::sun::star::beans::PropertyVetoException
, ::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
)
218 ::osl::MutexGuard
aGuard( Mutex
);
220 ::com::sun::star::uno::Any aValue
;
221 aValue
<<= bHorizontal
;
222 setFastPropertyValue( PROPERTY_Horizontal
, aValue
);
225 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> VCLXPrinterPropertySet::getFormDescriptions( ) throw(::com::sun::star::uno::RuntimeException
)
227 ::osl::MutexGuard
aGuard( Mutex
);
229 sal_uInt16 nPaperBinCount
= GetPrinter()->GetPaperBinCount();
230 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aDescriptions( nPaperBinCount
);
231 for ( sal_uInt16 n
= 0; n
< nPaperBinCount
; n
++ )
233 // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
234 String
aDescr( RTL_CONSTASCII_USTRINGPARAM( "*;*;" ) );
235 aDescr
+= GetPrinter()->GetPaperBinName( n
);
238 aDescr
.AppendAscii( ";*;*", 4 );
240 aDescriptions
.getArray()[n
] = aDescr
;
242 return aDescriptions
;
245 void VCLXPrinterPropertySet::selectForm( const ::rtl::OUString
& rFormDescription
) throw(::com::sun::star::beans::PropertyVetoException
, ::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
)
247 ::osl::MutexGuard
aGuard( Mutex
);
249 sal_Int32 nIndex
= 0;
250 sal_uInt16 nPaperBin
= sal::static_int_cast
< sal_uInt16
>(
251 rFormDescription
.getToken( 3, ';', nIndex
).toInt32());
252 GetPrinter()->SetPaperBin( nPaperBin
);
255 ::com::sun::star::uno::Sequence
< sal_Int8
> VCLXPrinterPropertySet::getBinarySetup( ) throw(::com::sun::star::uno::RuntimeException
)
257 ::osl::MutexGuard
aGuard( Mutex
);
260 aMem
<< sal_uInt32(BINARYSETUPMARKER
);
261 aMem
<< GetPrinter()->GetJobSetup();
262 return ::com::sun::star::uno::Sequence
<sal_Int8
>( (sal_Int8
*) aMem
.GetData(), aMem
.Tell() );
265 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
)
267 ::osl::MutexGuard
aGuard( Mutex
);
269 SvMemoryStream
aMem( (char*) data
.getConstArray(), data
.getLength(), STREAM_READ
);
272 DBG_ASSERT( nMarker
== BINARYSETUPMARKER
, "setBinarySetup - invalid!" );
273 if ( nMarker
== BINARYSETUPMARKER
)
277 GetPrinter()->SetJobSetup( aSetup
);
282 // ----------------------------------------------------
284 // ----------------------------------------------------
285 VCLXPrinter::VCLXPrinter( const String
& rPrinterName
)
286 : VCLXPrinterPropertySet( rPrinterName
)
290 VCLXPrinter::~VCLXPrinter()
294 // ::com::sun::star::uno::XInterface
295 ::com::sun::star::uno::Any
VCLXPrinter::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
297 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
298 (static_cast< ::com::sun::star::awt::XPrinter
* >(this)) );
300 if ( !aRet
.hasValue() )
301 aRet
= VCLXPrinterPropertySet::queryInterface( rType
);
303 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
306 // ::com::sun::star::lang::XTypeProvider
307 IMPL_XTYPEPROVIDER_START( VCLXPrinter
)
308 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPrinter
>* ) NULL
),
309 VCLXPrinterPropertySet::getTypes()
310 IMPL_XTYPEPROVIDER_END
312 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
)
314 ::osl::MutexGuard
aGuard( Mutex
);
316 sal_Bool bDone
= sal_True
;
317 if ( mpListener
.get() )
319 maInitJobSetup
= mpPrinter
->GetJobSetup();
320 mpListener
.reset( new vcl::OldStylePrintAdaptor( mpPrinter
) );
326 void VCLXPrinter::end( ) throw(::com::sun::star::awt::PrinterException
, ::com::sun::star::uno::RuntimeException
)
328 ::osl::MutexGuard
aGuard( Mutex
);
330 if ( mpListener
.get() )
332 Printer::PrintJob( mpListener
, maInitJobSetup
);
337 void VCLXPrinter::terminate( ) throw(::com::sun::star::uno::RuntimeException
)
339 ::osl::MutexGuard
aGuard( Mutex
);
344 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXPrinter::startPage( ) throw(::com::sun::star::awt::PrinterException
, ::com::sun::star::uno::RuntimeException
)
346 ::osl::MutexGuard
aGuard( Mutex
);
348 if ( mpListener
.get() )
350 mpListener
->StartPage();
355 void VCLXPrinter::endPage( ) throw(::com::sun::star::awt::PrinterException
, ::com::sun::star::uno::RuntimeException
)
357 ::osl::MutexGuard
aGuard( Mutex
);
359 if ( mpListener
.get() )
361 mpListener
->EndPage();
366 // ----------------------------------------------------
367 // class VCLXInfoPrinter
368 // ----------------------------------------------------
370 VCLXInfoPrinter::VCLXInfoPrinter( const String
& rPrinterName
)
371 : VCLXPrinterPropertySet( rPrinterName
)
375 VCLXInfoPrinter::~VCLXInfoPrinter()
379 // ::com::sun::star::uno::XInterface
380 ::com::sun::star::uno::Any
VCLXInfoPrinter::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
382 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
383 (static_cast< ::com::sun::star::awt::XInfoPrinter
* >(this)) );
385 if ( !aRet
.hasValue() )
386 aRet
= VCLXPrinterPropertySet::queryInterface( rType
);
388 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
391 // ::com::sun::star::lang::XTypeProvider
392 IMPL_XTYPEPROVIDER_START( VCLXInfoPrinter
)
393 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XInfoPrinter
>* ) NULL
),
394 VCLXPrinterPropertySet::getTypes()
395 IMPL_XTYPEPROVIDER_END
397 // ::com::sun::star::awt::XInfoPrinter
398 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XDevice
> VCLXInfoPrinter::createDevice( ) throw(::com::sun::star::uno::RuntimeException
)
400 ::osl::MutexGuard
aGuard( Mutex
);
405 // ----------------------------------------------------
406 // class VCLXPrinterServer
407 // ----------------------------------------------------
409 // ::com::sun::star::uno::XInterface
410 ::com::sun::star::uno::Any
VCLXPrinterServer::queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
412 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
413 (static_cast< ::com::sun::star::awt::XPrinterServer
* >(this)) );
414 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
417 // ::com::sun::star::lang::XTypeProvider
418 IMPL_XTYPEPROVIDER_START( VCLXPrinterServer
)
419 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPrinterServer
>* ) NULL
)
420 IMPL_XTYPEPROVIDER_END
422 // ::com::sun::star::awt::XPrinterServer
423 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> VCLXPrinterServer::getPrinterNames( ) throw(::com::sun::star::uno::RuntimeException
)
425 const std::vector
<rtl::OUString
>& rQueues
= Printer::GetPrinterQueues();
426 sal_uInt32 nPrinters
= rQueues
.size();
428 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aNames( nPrinters
);
429 for ( sal_uInt32 n
= 0; n
< nPrinters
; n
++ )
430 aNames
.getArray()[n
] = rQueues
[n
];
435 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPrinter
> VCLXPrinterServer::createPrinter( const ::rtl::OUString
& rPrinterName
) throw(::com::sun::star::uno::RuntimeException
)
437 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPrinter
> xP
;
438 xP
= new VCLXPrinter( rPrinterName
);
442 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XInfoPrinter
> VCLXPrinterServer::createInfoPrinter( const ::rtl::OUString
& rPrinterName
) throw(::com::sun::star::uno::RuntimeException
)
444 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XInfoPrinter
> xP
;
445 xP
= new VCLXInfoPrinter( rPrinterName
);
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */