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 #include "precompiled_basctl.hxx"
28 
29 #include "basicrenderable.hxx"
30 #include "bastypes.hxx"
31 #include "basidesh.hrc"
32 
33 #include "com/sun/star/awt/XDevice.hpp"
34 #include "toolkit/awt/vclxdevice.hxx"
35 #include "vcl/print.hxx"
36 #include "tools/multisel.hxx"
37 #include "tools/resary.hxx"
38 
39 using namespace com::sun::star;
40 using namespace com::sun::star::uno;
41 using namespace basicide;
42 
43 BasicRenderable::BasicRenderable( IDEBaseWindow* pWin )
44 : cppu::WeakComponentImplHelper1< com::sun::star::view::XRenderable >( maMutex )
45 , mpWindow( pWin )
46 {
47     ResStringArray aStrings( IDEResId( RID_PRINTDLG_STRLIST )  );
48     DBG_ASSERT( aStrings.Count() >= 3, "resource incomplete" );
49     if( aStrings.Count() < 3 ) // bad resource ?
50         return;
51 
52     m_aUIProperties.realloc( 3 );
53 
54     // create Subgroup for print range
55     vcl::PrinterOptionsHelper::UIControlOptions aPrintRangeOpt;
56     aPrintRangeOpt.maGroupHint = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PrintRange" ) );
57     aPrintRangeOpt.mbInternalOnly = sal_True;
58     m_aUIProperties[0].Value = getSubgroupControlOpt( rtl::OUString( aStrings.GetString( 0 ) ),
59                                                       rtl::OUString(),
60                                                       aPrintRangeOpt
61                                                       );
62 
63     // create a choice for the range to print
64     rtl::OUString aPrintContentName( RTL_CONSTASCII_USTRINGPARAM( "PrintContent" ) );
65     Sequence< rtl::OUString > aChoices( 2 );
66     Sequence< rtl::OUString > aHelpIds( 2 );
67     aChoices[0] = aStrings.GetString( 1 );
68     aHelpIds[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".HelpID:vcl:PrintDialog:PrintContent:RadioButton:0" ) );
69     aChoices[1] = aStrings.GetString( 2 );
70     aHelpIds[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".HelpID:vcl:PrintDialog:PrintContent:RadioButton:1" ) );
71     m_aUIProperties[1].Value = getChoiceControlOpt( rtl::OUString(),
72                                                     aHelpIds,
73                                                     aPrintContentName,
74                                                     aChoices,
75                                                     0 );
76 
77     // create a an Edit dependent on "Pages" selected
78     vcl::PrinterOptionsHelper::UIControlOptions aPageRangeOpt( aPrintContentName, 1, sal_True );
79     m_aUIProperties[2].Value = getEditControlOpt( rtl::OUString(),
80                                                   rtl::OUString(),
81                                                   rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PageRange" ) ),
82                                                   rtl::OUString(),
83                                                   aPageRangeOpt
84                                                   );
85 }
86 
87 BasicRenderable::~BasicRenderable()
88 {
89 }
90 
91 Printer* BasicRenderable::getPrinter()
92 {
93     Printer* pPrinter = NULL;
94     Any aValue( getValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RenderDevice" ) ) ) );
95     Reference<awt::XDevice> xRenderDevice;
96 
97     if( aValue >>= xRenderDevice )
98     {
99         VCLXDevice* pDevice = VCLXDevice::GetImplementation(xRenderDevice);
100         OutputDevice* pOut = pDevice ? pDevice->GetOutputDevice() : NULL;
101         pPrinter = dynamic_cast<Printer*>(pOut);
102     }
103     return pPrinter;
104 }
105 
106 sal_Int32 SAL_CALL BasicRenderable::getRendererCount (
107         const Any&, const Sequence<beans::PropertyValue >& i_xOptions
108         ) throw (lang::IllegalArgumentException, RuntimeException)
109 {
110     processProperties( i_xOptions );
111 
112     sal_Int32 nCount = 0;
113     if( mpWindow )
114     {
115         Printer* pPrinter = getPrinter();
116         if( pPrinter )
117         {
118             nCount = mpWindow->countPages( pPrinter );
119             sal_Int64 nContent = getIntValue( "PrintContent", -1 );
120             if( nContent == 1 )
121             {
122                 rtl::OUString aPageRange( getStringValue( "PageRange" ) );
123                 MultiSelection aSel( aPageRange );
124                 long nSelCount = aSel.GetSelectCount();
125                 if( nSelCount >= 0 && nSelCount < nCount )
126                     nCount = nSelCount;
127             }
128         }
129         else
130             throw lang::IllegalArgumentException();
131     }
132 
133     return nCount;
134 }
135 
136 Sequence<beans::PropertyValue> SAL_CALL BasicRenderable::getRenderer (
137         sal_Int32, const Any&, const Sequence<beans::PropertyValue>& i_xOptions
138         ) throw (lang::IllegalArgumentException, RuntimeException)
139 {
140     processProperties( i_xOptions );
141 
142     Sequence< beans::PropertyValue > aVals;
143     // insert page size here
144     Printer* pPrinter = getPrinter();
145     // no renderdevice is legal; the first call is to get our print ui options
146     if( pPrinter )
147     {
148         Size aPageSize( pPrinter->PixelToLogic( pPrinter->GetPaperSizePixel(), MapMode( MAP_100TH_MM ) ) );
149 
150         aVals.realloc( 1 );
151         aVals[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) );
152         awt::Size aSize;
153         aSize.Width  = aPageSize.Width();
154         aSize.Height = aPageSize.Height();
155         aVals[0].Value <<= aSize;
156     }
157 
158     appendPrintUIOptions( aVals );
159 
160     return aVals;
161 }
162 
163 void SAL_CALL BasicRenderable::render (
164         sal_Int32 nRenderer, const Any&,
165         const Sequence<beans::PropertyValue>& i_xOptions
166         ) throw (lang::IllegalArgumentException, RuntimeException)
167 {
168     processProperties( i_xOptions );
169 
170     if( mpWindow )
171     {
172         Printer* pPrinter = getPrinter();
173         if( pPrinter )
174         {
175             sal_Int64 nContent = getIntValue( "PrintContent", -1 );
176             if( nContent == 1 )
177             {
178                 rtl::OUString aPageRange( getStringValue( "PageRange" ) );
179                 MultiSelection aSel( aPageRange );
180                 long nSelect = aSel.FirstSelected();
181                 while( nSelect != long(SFX_ENDOFSELECTION) && nRenderer-- )
182                     nSelect = aSel.NextSelected();
183                 if( nSelect != long(SFX_ENDOFSELECTION) )
184                     mpWindow->printPage( sal_Int32(nSelect-1), pPrinter );
185             }
186             else
187                 mpWindow->printPage( nRenderer, pPrinter );
188         }
189         else
190             throw lang::IllegalArgumentException();
191     }
192 }
193 
194 
195