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