xref: /trunk/main/toolkit/source/awt/vclxprinter.cxx (revision 44b09b52)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_toolkit.hxx"
26 
27 
28 #include <toolkit/awt/vclxprinter.hxx>
29 #include <toolkit/helper/macros.hxx>
30 #include <cppuhelper/typeprovider.hxx>
31 #include <rtl/memory.h>
32 #include <rtl/uuid.h>
33 
34 
35 #include <vcl/print.hxx>
36 #include <vcl/jobset.hxx>
37 #include <vcl/svapp.hxx>
38 
39 #include <tools/debug.hxx>
40 #include <tools/stream.hxx>
41 
42 #include <toolkit/awt/vclxdevice.hxx>
43 
44 
45 #define BINARYSETUPMARKER       0x23864691
46 
47 #define PROPERTY_Orientation    0
48 #define PROPERTY_Horizontal     1
49 
ImplGetProperties(sal_uInt16 & rElementCount)50 ::com::sun::star::beans::Property* ImplGetProperties( sal_uInt16& rElementCount )
51 {
52     static ::com::sun::star::beans::Property* pProperties = NULL;
53     static sal_uInt16 nElements = 0;
54     if( !pProperties )
55     {
56         ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
57         if( !pProperties )
58         {
59             static ::com::sun::star::beans::Property __FAR_DATA aPropTable[] =
60             {
61                 ::com::sun::star::beans::Property( ::rtl::OUString::createFromAscii( "Orientation" ), PROPERTY_Orientation, ::getCppuType((const sal_Int16*)0), 0 ),
62                 ::com::sun::star::beans::Property( ::rtl::OUString::createFromAscii( "Horizontal" ), PROPERTY_Horizontal, ::getBooleanCppuType(), 0 )
63             };
64             pProperties = aPropTable;
65             nElements = sizeof( aPropTable ) / sizeof( ::com::sun::star::beans::Property );
66         }
67     }
68     rElementCount = nElements;
69     return pProperties;
70 }
71 
72 //    ----------------------------------------------------
73 //    class VCLXPrinterPropertySet
74 //    ----------------------------------------------------
75 
IMPLEMENT_FORWARD_XINTERFACE2(VCLXPrinterPropertySet,VCLXPrinterPropertySet_Base,OPropertySetHelper)76 IMPLEMENT_FORWARD_XINTERFACE2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, OPropertySetHelper )
77 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, ::cppu::OPropertySetHelper )
78 
79 VCLXPrinterPropertySet::VCLXPrinterPropertySet( const String& rPrinterName )
80     : OPropertySetHelper( BrdcstHelper )
81     , mpPrinter( new Printer( rPrinterName ) )
82 {
83     osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
84 
85     mnOrientation = 0;
86     mbHorizontal = sal_False;
87 }
88 
~VCLXPrinterPropertySet()89 VCLXPrinterPropertySet::~VCLXPrinterPropertySet()
90 {
91     osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
92     mpPrinter.reset();
93 }
94 
GetDevice()95 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice >  VCLXPrinterPropertySet::GetDevice()
96 {
97     if ( !mxPrnDevice.is() )
98     {
99         VCLXDevice* pDev = new VCLXDevice;
100         pDev->SetOutputDevice( GetPrinter() );
101         mxPrnDevice = pDev;
102     }
103     return mxPrnDevice;
104 }
105 
getPropertySetInfo()106 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > VCLXPrinterPropertySet::getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException)
107 {
108     static ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo >  xInfo( createPropertySetInfo( getInfoHelper() ) );
109     return xInfo;
110 }
111 
getInfoHelper()112 ::cppu::IPropertyArrayHelper& VCLXPrinterPropertySet::getInfoHelper()
113 {
114     static ::cppu::OPropertyArrayHelper* pPropertyArrayHelper = NULL;
115     if ( !pPropertyArrayHelper )
116     {
117         ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
118         if( !pPropertyArrayHelper )
119         {
120             sal_uInt16 nElements;
121             ::com::sun::star::beans::Property* pProps = ImplGetProperties( nElements );
122             pPropertyArrayHelper = new ::cppu::OPropertyArrayHelper( pProps, nElements, sal_False );
123         }
124     }
125     return *pPropertyArrayHelper ;
126 }
127 
convertFastPropertyValue(::com::sun::star::uno::Any & rConvertedValue,::com::sun::star::uno::Any & rOldValue,sal_Int32 nHandle,const::com::sun::star::uno::Any & rValue)128 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)
129 {
130     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
131 
132     sal_Bool bDifferent = sal_False;
133     switch ( nHandle )
134     {
135         case PROPERTY_Orientation:
136         {
137             sal_Int16 n;
138             if( ( rValue >>= n ) && ( n != mnOrientation ) )
139             {
140                 rConvertedValue <<= n;
141                 rOldValue <<= mnOrientation;
142                 bDifferent = sal_True;
143             }
144         }
145         break;
146         case PROPERTY_Horizontal:
147         {
148             sal_Bool b;
149             if( ( rValue >>= b ) && ( b != mbHorizontal ) )
150             {
151                 rConvertedValue <<= b;
152                 rOldValue <<= mbHorizontal;
153                 bDifferent = sal_True;
154             }
155         }
156         break;
157         default:
158         {
159             DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
160         }
161     }
162     return bDifferent;
163 }
164 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const::com::sun::star::uno::Any & rValue)165 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::uno::Exception)
166 {
167     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
168 
169     switch( nHandle )
170     {
171         case PROPERTY_Orientation:
172         {
173             rValue >>= mnOrientation;
174         }
175         break;
176         case PROPERTY_Horizontal:
177         {
178             rValue >>= mbHorizontal;
179         }
180         break;
181         default:
182         {
183             DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
184         }
185     }
186 }
187 
getFastPropertyValue(::com::sun::star::uno::Any & rValue,sal_Int32 nHandle) const188 void VCLXPrinterPropertySet::getFastPropertyValue( ::com::sun::star::uno::Any& rValue, sal_Int32 nHandle ) const
189 {
190     ::osl::Guard< ::osl::Mutex > aGuard( ((VCLXPrinterPropertySet*)this)->Mutex );
191 
192     switch( nHandle )
193     {
194         case PROPERTY_Orientation:
195             rValue <<= mnOrientation;
196         break;
197         case PROPERTY_Horizontal:
198             rValue <<= mbHorizontal;
199         break;
200         default:
201         {
202             DBG_ERROR( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
203         }
204     }
205 }
206 
207 // ::com::sun::star::awt::XPrinterPropertySet
setHorizontal(sal_Bool bHorizontal)208 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
209 {
210     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
211 
212     ::com::sun::star::uno::Any aValue;
213     aValue <<= bHorizontal;
214     setFastPropertyValue( PROPERTY_Horizontal, aValue );
215 }
216 
getFormDescriptions()217 ::com::sun::star::uno::Sequence< ::rtl::OUString > VCLXPrinterPropertySet::getFormDescriptions(  ) throw(::com::sun::star::uno::RuntimeException)
218 {
219     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
220 
221     sal_uInt16 nPaperBinCount = GetPrinter()->GetPaperBinCount();
222     ::com::sun::star::uno::Sequence< ::rtl::OUString > aDescriptions( nPaperBinCount );
223     for ( sal_uInt16 n = 0; n < nPaperBinCount; n++ )
224     {
225         // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
226         String aDescr( RTL_CONSTASCII_USTRINGPARAM( "*;*;" ) );
227         aDescr += GetPrinter()->GetPaperBinName( n );
228         aDescr += ';';
229         aDescr += n;
230         aDescr.AppendAscii( ";*;*", 4 );
231 
232         aDescriptions.getArray()[n] = aDescr;
233     }
234     return aDescriptions;
235 }
236 
selectForm(const::rtl::OUString & rFormDescription)237 void VCLXPrinterPropertySet::selectForm( const ::rtl::OUString& rFormDescription ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
238 {
239     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
240 
241     sal_Int32 nIndex = 0;
242     sal_uInt16 nPaperBin = sal::static_int_cast< sal_uInt16 >(
243         rFormDescription.getToken( 3, ';', nIndex ).toInt32());
244     GetPrinter()->SetPaperBin( nPaperBin );
245 }
246 
getBinarySetup()247 ::com::sun::star::uno::Sequence< sal_Int8 > VCLXPrinterPropertySet::getBinarySetup(  ) throw(::com::sun::star::uno::RuntimeException)
248 {
249     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
250 
251     SvMemoryStream aMem;
252     aMem << BINARYSETUPMARKER;
253     aMem << GetPrinter()->GetJobSetup();
254     return ::com::sun::star::uno::Sequence<sal_Int8>( (sal_Int8*) aMem.GetData(), aMem.Tell() );
255 }
256 
setBinarySetup(const::com::sun::star::uno::Sequence<sal_Int8> & data)257 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)
258 {
259     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
260 
261     SvMemoryStream aMem( (char*) data.getConstArray(), data.getLength(), STREAM_READ );
262     sal_uInt32 nMarker;
263     aMem >> nMarker;
264     DBG_ASSERT( nMarker == BINARYSETUPMARKER, "setBinarySetup - invalid!" );
265     if ( nMarker == BINARYSETUPMARKER )
266     {
267         JobSetup aSetup;
268         aMem >> aSetup;
269         GetPrinter()->SetJobSetup( aSetup );
270     }
271 }
272 
273 
274 //    ----------------------------------------------------
275 //    class VCLXPrinter
276 //    ----------------------------------------------------
VCLXPrinter(const String & rPrinterName)277 VCLXPrinter::VCLXPrinter( const String& rPrinterName )
278     : VCLXPrinter_Base( rPrinterName )
279 {
280 }
281 
~VCLXPrinter()282 VCLXPrinter::~VCLXPrinter()
283 {
284 }
285 
start(const::rtl::OUString &,sal_Int16,sal_Bool)286 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)
287 {
288     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
289 
290     sal_Bool bDone = sal_True;
291     if ( mpPrinter.get() )
292     {
293         maInitJobSetup = mpPrinter->GetJobSetup();
294         mpListener.reset( new vcl::OldStylePrintAdaptor( mpPrinter ) );
295     }
296 
297     return bDone;
298 }
299 
end()300 void VCLXPrinter::end(  ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException)
301 {
302     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
303 
304     if ( mpListener.get() )
305     {
306         Printer::PrintJob( mpListener, maInitJobSetup );
307         mpListener.reset();
308     }
309 }
310 
terminate()311 void VCLXPrinter::terminate(  ) throw(::com::sun::star::uno::RuntimeException)
312 {
313     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
314 
315     mpListener.reset();
316 }
317 
startPage()318 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinter::startPage(  ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException)
319 {
320     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
321 
322     if ( mpListener.get() )
323     {
324         mpListener->StartPage();
325     }
326     return GetDevice();
327 }
328 
endPage()329 void VCLXPrinter::endPage(  ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException)
330 {
331     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
332 
333     if ( mpListener.get() )
334     {
335         mpListener->EndPage();
336     }
337 }
338 
339 
340 //    ----------------------------------------------------
341 //    class VCLXInfoPrinter
342 //    ----------------------------------------------------
343 
VCLXInfoPrinter(const String & rPrinterName)344 VCLXInfoPrinter::VCLXInfoPrinter( const String& rPrinterName )
345     : VCLXInfoPrinter_Base( rPrinterName )
346 {
347 }
348 
~VCLXInfoPrinter()349 VCLXInfoPrinter::~VCLXInfoPrinter()
350 {
351 }
352 
353 // ::com::sun::star::awt::XInfoPrinter
createDevice()354 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXInfoPrinter::createDevice(  ) throw(::com::sun::star::uno::RuntimeException)
355 {
356     ::osl::Guard< ::osl::Mutex > aGuard( Mutex );
357 
358     return GetDevice();
359 }
360 
361 //    ----------------------------------------------------
362 //    class VCLXPrinterServer
363 //    ----------------------------------------------------
364 
365 // ::com::sun::star::awt::XPrinterServer
getPrinterNames()366 ::com::sun::star::uno::Sequence< ::rtl::OUString > VCLXPrinterServer::getPrinterNames(  ) throw(::com::sun::star::uno::RuntimeException)
367 {
368     const std::vector<rtl::OUString>& rQueues = Printer::GetPrinterQueues();
369     sal_uInt32 nPrinters = rQueues.size();
370 
371     ::com::sun::star::uno::Sequence< ::rtl::OUString > aNames( nPrinters );
372     for ( sal_uInt32 n = 0; n < nPrinters; n++ )
373         aNames.getArray()[n] = rQueues[n];
374 
375     return aNames;
376 }
377 
createPrinter(const::rtl::OUString & rPrinterName)378 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > VCLXPrinterServer::createPrinter( const ::rtl::OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException)
379 {
380     ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > xP;
381     xP = new VCLXPrinter( rPrinterName );
382     return xP;
383 }
384 
createInfoPrinter(const::rtl::OUString & rPrinterName)385 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > VCLXPrinterServer::createInfoPrinter( const ::rtl::OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException)
386 {
387     ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > xP;
388     xP = new VCLXInfoPrinter( rPrinterName );
389     return xP;
390 }
391 
392 
393