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