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 : #ifdef _MSC_VER
21 : #pragma warning (disable:4190)
22 : #endif
23 : #include <com/sun/star/drawing/GraphicExportFilter.hpp>
24 : #include <com/sun/star/graphic/GraphicProvider.hpp>
25 : #include <com/sun/star/graphic/XGraphicProvider.hpp>
26 : #include <com/sun/star/graphic/GraphicType.hpp>
27 : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
28 : #include <com/sun/star/ucb/XSimpleFileAccess2.hpp>
29 :
30 : #include <unotools/localfilehelper.hxx>
31 : #include <tools/errinf.hxx>
32 : #include <vcl/layout.hxx>
33 : #include <vcl/metaact.hxx>
34 : #include <vcl/virdev.hxx>
35 : #include <sfx2/docfile.hxx>
36 : #include <sfx2/docfilt.hxx>
37 : #include <sfx2/frame.hxx>
38 : #include <svx/svdograf.hxx>
39 : #include <svx/svdpagv.hxx>
40 :
41 : #include "../../ui/inc/strings.hrc"
42 : #include "../../ui/inc/DrawViewShell.hxx"
43 : #include "../../ui/inc/DrawDocShell.hxx"
44 : #include "../../ui/inc/ClientView.hxx"
45 : #include "../../ui/inc/FrameView.hxx"
46 :
47 : #include "comphelper/anytostring.hxx"
48 : #include "cppuhelper/exc_hlp.hxx"
49 :
50 : #include <comphelper/processfactory.hxx>
51 : #include <unotools/pathoptions.hxx>
52 : #include <sfx2/filedlghelper.hxx>
53 : #include <vcl/graphicfilter.hxx>
54 : #include <svx/xoutbmp.hxx>
55 :
56 : #include "sdpage.hxx"
57 : #include "drawdoc.hxx"
58 : #include "sdresid.hxx"
59 : #include "sdgrffilter.hxx"
60 : #include "../../ui/inc/ViewShellBase.hxx"
61 : #include <com/sun/star/uno/Sequence.h>
62 : #include <com/sun/star/beans/PropertyValue.hpp>
63 : #include <com/sun/star/beans/PropertyValues.hpp>
64 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
65 : #include <com/sun/star/lang/XComponent.hpp>
66 : #include <com/sun/star/document/XFilter.hpp>
67 : #include <com/sun/star/document/XExporter.hpp>
68 : #include <com/sun/star/view/XSelectionSupplier.hpp>
69 : #include <com/sun/star/drawing/XDrawView.hpp>
70 : #include "../../ui/inc/DrawController.hxx"
71 : #include <cppuhelper/implbase2.hxx>
72 : #include <com/sun/star/drawing/XShape.hpp>
73 : #include <com/sun/star/task/XInteractionHandler.hpp>
74 : #include <com/sun/star/task/XInteractionRequest.hpp>
75 : #include <com/sun/star/drawing/GraphicFilterRequest.hpp>
76 :
77 : using namespace ::com::sun::star;
78 : using namespace ::com::sun::star::uno;
79 : using namespace ::com::sun::star::lang;
80 : using namespace ::com::sun::star::beans;
81 : using namespace ::com::sun::star::graphic;
82 : using namespace ::com::sun::star::io;
83 : using namespace ::com::sun::star::ucb;
84 : using namespace com::sun::star::ui::dialogs;
85 : using namespace ::sfx2;
86 :
87 : class SdGRFFilter_ImplInteractionHdl : public ::cppu::WeakImplHelper1< com::sun::star::task::XInteractionHandler >
88 : {
89 : com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > m_xInter;
90 : sal_uInt16 nFilterError;
91 :
92 : public:
93 :
94 0 : SdGRFFilter_ImplInteractionHdl( com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > xInteraction ) :
95 : m_xInter( xInteraction ),
96 0 : nFilterError( GRFILTER_OK )
97 0 : {}
98 :
99 : virtual ~SdGRFFilter_ImplInteractionHdl();
100 :
101 0 : sal_uInt16 GetErrorCode() const { return nFilterError; };
102 :
103 : virtual void SAL_CALL handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& )
104 : throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
105 : };
106 :
107 0 : SdGRFFilter_ImplInteractionHdl::~SdGRFFilter_ImplInteractionHdl()
108 : {
109 0 : }
110 :
111 0 : void SdGRFFilter_ImplInteractionHdl::handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& xRequest )
112 : throw( com::sun::star::uno::RuntimeException, std::exception )
113 : {
114 0 : if( !m_xInter.is() )
115 0 : return;
116 :
117 0 : com::sun::star::drawing::GraphicFilterRequest aErr;
118 0 : if ( xRequest->getRequest() >>= aErr )
119 0 : nFilterError = (sal_uInt16)aErr.ErrCode;
120 : else
121 0 : m_xInter->handle( xRequest );
122 : }
123 :
124 : // - SdPPTFilter -
125 :
126 1 : SdGRFFilter::SdGRFFilter( SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell ) :
127 1 : SdFilter( rMedium, rDocShell, true )
128 : {
129 1 : }
130 :
131 1 : SdGRFFilter::~SdGRFFilter()
132 : {
133 1 : }
134 :
135 0 : void SdGRFFilter::HandleGraphicFilterError( sal_uInt16 nFilterError, sal_uLong nStreamError )
136 : {
137 : sal_uInt16 nId;
138 :
139 0 : switch( nFilterError )
140 : {
141 : case GRFILTER_OPENERROR:
142 0 : nId = STR_IMPORT_GRFILTER_OPENERROR;
143 0 : break;
144 : case GRFILTER_IOERROR:
145 0 : nId = STR_IMPORT_GRFILTER_IOERROR;
146 0 : break;
147 : case GRFILTER_FORMATERROR:
148 0 : nId = STR_IMPORT_GRFILTER_FORMATERROR;
149 0 : break;
150 : case GRFILTER_VERSIONERROR:
151 0 : nId = STR_IMPORT_GRFILTER_VERSIONERROR;
152 0 : break;
153 : case GRFILTER_TOOBIG:
154 0 : nId = STR_IMPORT_GRFILTER_TOOBIG;
155 0 : break;
156 : case 0 :
157 0 : nId = 0;
158 0 : break;
159 :
160 : default:
161 : case GRFILTER_FILTERERROR:
162 0 : nId = STR_IMPORT_GRFILTER_FILTERERROR;
163 0 : break;
164 : }
165 :
166 0 : if( ERRCODE_NONE != nStreamError )
167 0 : ErrorHandler::HandleError( nStreamError );
168 0 : else if( STR_IMPORT_GRFILTER_IOERROR == nId )
169 0 : ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
170 : else
171 : {
172 0 : ScopedVclPtrInstance< MessageDialog > aErrorBox(nullptr, SD_RESSTR(nId));
173 0 : aErrorBox->Execute();
174 : }
175 0 : }
176 :
177 1 : bool SdGRFFilter::Import()
178 : {
179 1 : Graphic aGraphic;
180 2 : const OUString aFileName( mrMedium.GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) );
181 1 : GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
182 1 : const sal_uInt16 nFilter = rGraphicFilter.GetImportFormatNumberForTypeName( mrMedium.GetFilter()->GetTypeName() );
183 1 : bool bRet = false;
184 :
185 1 : SvStream* pIStm = mrMedium.GetInStream();
186 1 : sal_uInt16 nReturn = pIStm ? rGraphicFilter.ImportGraphic( aGraphic, aFileName, *pIStm, nFilter ) : 1;
187 :
188 1 : if( nReturn )
189 0 : HandleGraphicFilterError( nReturn, rGraphicFilter.GetLastError().nStreamError );
190 : else
191 : {
192 1 : if( mrDocument.GetPageCount() == 0L )
193 0 : mrDocument.CreateFirstPages();
194 :
195 1 : SdPage* pPage = mrDocument.GetSdPage( 0, PK_STANDARD );
196 1 : Point aPos;
197 1 : Size aPagSize( pPage->GetSize() );
198 : Size aGrfSize( OutputDevice::LogicToLogic( aGraphic.GetPrefSize(),
199 1 : aGraphic.GetPrefMapMode(), MAP_100TH_MM ) );
200 :
201 1 : aPagSize.Width() -= pPage->GetLftBorder() + pPage->GetRgtBorder();
202 1 : aPagSize.Height() -= pPage->GetUppBorder() + pPage->GetLwrBorder();
203 :
204 : // scale to fit page
205 3 : if ( ( ( aGrfSize.Height() > aPagSize.Height() ) || ( aGrfSize.Width() > aPagSize.Width() ) ) &&
206 1 : aGrfSize.Height() && aPagSize.Height() )
207 : {
208 0 : double fGrfWH = (double) aGrfSize.Width() / aGrfSize.Height();
209 0 : double fWinWH = (double) aPagSize.Width() / aPagSize.Height();
210 :
211 : // adjust graphic to page size (scales)
212 0 : if( fGrfWH < fWinWH )
213 : {
214 0 : aGrfSize.Width() = (long) ( aPagSize.Height() * fGrfWH );
215 0 : aGrfSize.Height() = aPagSize.Height();
216 : }
217 0 : else if( fGrfWH > 0.F )
218 : {
219 0 : aGrfSize.Width() = aPagSize.Width();
220 0 : aGrfSize.Height()= (long) ( aPagSize.Width() / fGrfWH );
221 : }
222 : }
223 :
224 : // set output rectangle for graphic
225 1 : aPos.X() = ( ( aPagSize.Width() - aGrfSize.Width() ) >> 1 ) + pPage->GetLftBorder();
226 1 : aPos.Y() = ( ( aPagSize.Height() - aGrfSize.Height() ) >> 1 ) + pPage->GetUppBorder();
227 :
228 1 : pPage->InsertObject( new SdrGrafObj( aGraphic, Rectangle( aPos, aGrfSize ) ) );
229 1 : bRet = true;
230 : }
231 2 : return bRet;
232 : }
233 :
234 0 : bool SdGRFFilter::Export()
235 : {
236 : // SJ: todo: error handling, the GraphicExportFilter does not support proper errorhandling
237 :
238 0 : bool bRet = false;
239 :
240 0 : uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
241 0 : uno::Reference< drawing::XGraphicExportFilter > xExporter = drawing::GraphicExportFilter::create( xContext );
242 :
243 0 : SdPage* pPage = NULL;
244 : sd::DrawViewShell* pDrawViewShell = static_cast< ::sd::DrawViewShell* >
245 0 : ( ( ( mrDocShell.GetViewShell() && mrDocShell.GetViewShell()->ISA(::sd::DrawViewShell ) ) ? mrDocShell.GetViewShell() : NULL ) );
246 :
247 0 : PageKind ePageKind = PK_STANDARD;
248 0 : if( pDrawViewShell )
249 : {
250 0 : ePageKind = pDrawViewShell->GetPageKind();
251 0 : if( PK_HANDOUT == ePageKind )
252 0 : pPage = mrDocument.GetSdPage( 0, PK_HANDOUT );
253 : else
254 0 : pPage = pDrawViewShell->GetActualPage();
255 : }
256 : else
257 0 : pPage = mrDocument.GetSdPage( 0, PK_STANDARD );
258 :
259 0 : if ( pPage )
260 : {
261 : // taking the 'correct' page number, seems that there might exist a better method to archive this
262 0 : pPage = mrDocument.GetSdPage( pPage->GetPageNum() ? ( pPage->GetPageNum() - 1 ) >> 1 : 0, ePageKind );
263 0 : if ( pPage )
264 : {
265 0 : uno::Reference< lang::XComponent > xSource( pPage->getUnoPage(), uno::UNO_QUERY );
266 0 : SfxItemSet* pSet = mrMedium.GetItemSet();
267 0 : if ( pSet && xSource.is() )
268 : {
269 0 : const OUString aTypeName( mrMedium.GetFilter()->GetTypeName() );
270 0 : GraphicFilter &rGraphicFilter = GraphicFilter::GetGraphicFilter();
271 0 : const sal_uInt16 nFilter = rGraphicFilter.GetExportFormatNumberForTypeName( aTypeName );
272 0 : if ( nFilter != GRFILTER_FORMAT_NOTFOUND )
273 : {
274 0 : uno::Reference< task::XInteractionHandler > mXInteractionHandler;
275 :
276 0 : beans::PropertyValues aArgs;
277 0 : TransformItems( SID_SAVEASDOC, *pSet, aArgs );
278 :
279 0 : OUString sInteractionHandler( "InteractionHandler" );
280 0 : OUString sFilterName( "FilterName" );
281 0 : OUString sShortName( rGraphicFilter.GetExportFormatShortName( nFilter ) );
282 :
283 0 : bool bFilterNameFound = false;
284 : sal_Int32 i, nCount;
285 0 : for ( i = 0, nCount = aArgs.getLength(); i < nCount; i++ )
286 : {
287 0 : OUString& rStr = aArgs[ i ].Name;
288 0 : if ( rStr == sFilterName )
289 : {
290 0 : bFilterNameFound = true;
291 0 : aArgs[ i ].Name = sFilterName;
292 0 : aArgs[ i ].Value <<= sShortName;
293 : }
294 0 : else if ( rStr == sInteractionHandler )
295 : {
296 0 : uno::Reference< task::XInteractionHandler > xHdl;
297 0 : if ( aArgs[ i ].Value >>= xHdl )
298 : {
299 0 : mXInteractionHandler = new SdGRFFilter_ImplInteractionHdl( xHdl );
300 0 : aArgs[ i ].Value <<= mXInteractionHandler;
301 0 : }
302 : }
303 : }
304 0 : if ( !bFilterNameFound )
305 : {
306 0 : aArgs.realloc( ++nCount );
307 0 : aArgs[ i ].Name = sFilterName;
308 0 : aArgs[ i ].Value <<= sShortName;
309 : }
310 :
311 : // take selection if needed
312 0 : if( ( SfxItemState::SET == pSet->GetItemState( SID_SELECTION ) )
313 0 : && static_cast< const SfxBoolItem& >( pSet->Get( SID_SELECTION ) ).GetValue()
314 0 : && pDrawViewShell )
315 : {
316 : uno::Reference< view::XSelectionSupplier > xSelectionSupplier(
317 0 : pDrawViewShell->GetViewShellBase().GetController(), uno::UNO_QUERY );
318 0 : if ( xSelectionSupplier.is() )
319 : {
320 0 : uno::Any aSelection( xSelectionSupplier->getSelection() );
321 0 : uno::Reference< lang::XComponent > xSelection;
322 0 : if ( aSelection >>= xSelection )
323 0 : xSource = xSelection;
324 0 : }
325 : }
326 0 : xExporter->setSourceDocument( xSource );
327 0 : bRet = xExporter->filter( aArgs );
328 0 : if ( !bRet && mXInteractionHandler.is() )
329 : SdGRFFilter::HandleGraphicFilterError(
330 0 : static_cast< SdGRFFilter_ImplInteractionHdl* >( mXInteractionHandler.get() )->GetErrorCode(),
331 0 : rGraphicFilter.GetLastError().nStreamError );
332 0 : }
333 0 : }
334 : }
335 : }
336 0 : return bRet;
337 66 : }
338 :
339 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|