merge the formfield patch from ooo-build
[ooovba.git] / svx / source / dialog / opengrf.cxx
blobf6564449ae6bc2a814c17e1f678b226d516792b8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: opengrf.cxx,v $
10 * $Revision: 1.22 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <tools/urlobj.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <com/sun/star/uno/Reference.h>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
38 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
40 #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
41 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
42 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
43 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
44 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
45 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
46 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
47 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
48 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
49 #include <svtools/urihelper.hxx>
50 #ifndef _UNOTOOLS_UCBSTREAMHELPER_HXX
51 #include <unotools/ucbstreamhelper.hxx>
52 #endif
53 #include <svtools/transfer.hxx>
54 #include <svx/svdograf.hxx>
55 #include <sot/formats.hxx>
56 #ifndef _MSGBOX_HXX //autogen
57 #include <vcl/msgbox.hxx>
58 #endif
59 #include <sfx2/filedlghelper.hxx>
60 #include <sfx2/docfile.hxx>
61 #include <svtools/pathoptions.hxx>
62 #include <svx/dialmgr.hxx>
63 #include "opengrf.hxx"
65 #include <svx/dialogs.hrc>
66 #include "impgrf.hrc"
69 //-----------------------------------------------------------------------------
71 using namespace ::com::sun::star;
72 using namespace ::com::sun::star::lang;
73 using namespace ::com::sun::star::ui::dialogs;
74 using namespace ::com::sun::star::uno;
75 using namespace ::rtl;
76 using namespace ::cppu;
79 //-----------------------------------------------------------------------------
81 USHORT SvxOpenGrfErr2ResId( short err )
83 switch( err )
85 case GRFILTER_OPENERROR:
86 return RID_SVXSTR_GRFILTER_OPENERROR;
87 case GRFILTER_IOERROR:
88 return RID_SVXSTR_GRFILTER_IOERROR;
89 case GRFILTER_VERSIONERROR:
90 return RID_SVXSTR_GRFILTER_VERSIONERROR;
91 case GRFILTER_FILTERERROR:
92 return RID_SVXSTR_GRFILTER_FILTERERROR;
93 case GRFILTER_FORMATERROR:
94 default:
95 return RID_SVXSTR_GRFILTER_FORMATERROR;
100 struct SvxOpenGrf_Impl
102 SvxOpenGrf_Impl ();
104 sfx2::FileDialogHelper aFileDlg;
105 uno::Reference < XFilePickerControlAccess > xCtrlAcc;
109 SvxOpenGrf_Impl::SvxOpenGrf_Impl() :
110 aFileDlg(SFXWB_GRAPHIC)
112 uno::Reference < XFilePicker > xFP = aFileDlg.GetFilePicker();
113 xCtrlAcc = uno::Reference < XFilePickerControlAccess >(xFP, UNO_QUERY);
117 SvxOpenGraphicDialog::SvxOpenGraphicDialog( const String& rTitle ) :
118 mpImpl( new SvxOpenGrf_Impl )
120 mpImpl->aFileDlg.SetTitle(rTitle);
124 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
129 GraphicFilter* GetGrfFilter();
131 short SvxOpenGraphicDialog::Execute()
133 USHORT nImpRet;
134 BOOL bQuitLoop(FALSE);
136 while( bQuitLoop == FALSE &&
137 mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
139 if( GetPath().Len() )
141 GraphicFilter* pFilter = GetGrfFilter();
142 INetURLObject aObj( GetPath() );
144 // check whether we can load the graphic
145 String aCurFilter( GetCurrentFilter() );
146 USHORT nFormatNum = pFilter->GetImportFormatNumber( aCurFilter );
147 USHORT nRetFormat = 0;
148 USHORT nFound = USHRT_MAX;
150 // non-local?
151 if ( INET_PROT_FILE != aObj.GetProtocol() )
153 SfxMedium aMed( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ, TRUE );
154 aMed.DownLoad();
155 SvStream* pStream = aMed.GetInStream();
157 if( pStream )
158 nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream, nFormatNum, &nRetFormat );
159 else
160 nImpRet = pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat );
162 if ( GRFILTER_OK != nImpRet )
164 if ( !pStream )
165 nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
166 else
167 nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream,
168 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
171 else
173 if( (nImpRet=pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat )) != GRFILTER_OK )
174 nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
177 if ( GRFILTER_OK == nImpRet )
178 nFound = nRetFormat;
180 // could not load?
181 if ( nFound == USHRT_MAX )
183 WarningBox aWarningBox( NULL, WB_3DLOOK | WB_RETRY_CANCEL, SVX_RESSTR( SvxOpenGrfErr2ResId(nImpRet) ) );
184 bQuitLoop = aWarningBox.Execute()==RET_RETRY ? FALSE : TRUE;
186 else
188 // setup appropriate filter (so next time, it will work)
189 if( pFilter->GetImportFormatCount() )
191 String aFormatName(pFilter->GetImportFormatName(nFound));
192 SetCurrentFilter(aFormatName);
195 return nImpRet;
200 // cancel
201 return -1;
205 void SvxOpenGraphicDialog::SetPath( const String& rPath )
207 mpImpl->aFileDlg.SetDisplayDirectory(rPath);
210 void SvxOpenGraphicDialog::SetPath( const String& rPath, sal_Bool bLinkState )
212 SetPath(rPath);
213 AsLink(bLinkState);
217 void SvxOpenGraphicDialog::EnableLink( sal_Bool state )
219 if( mpImpl->xCtrlAcc.is() )
223 mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
225 catch(IllegalArgumentException)
227 #ifdef DBG_UTIL
228 DBG_ERROR( "Cannot enable \"link\" checkbox" );
229 #endif
235 void SvxOpenGraphicDialog::AsLink(sal_Bool bState)
237 if( mpImpl->xCtrlAcc.is() )
241 Any aAny; aAny <<= bState;
242 mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, aAny );
244 catch(IllegalArgumentException)
246 #ifdef DBG_UTIL
247 DBG_ERROR( "Cannot check \"link\" checkbox" );
248 #endif
254 sal_Bool SvxOpenGraphicDialog::IsAsLink() const
258 if( mpImpl->xCtrlAcc.is() )
260 Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
261 DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
262 return aVal.hasValue() ? *(sal_Bool*) aVal.getValue() : sal_False;
265 catch(IllegalArgumentException)
267 #ifdef DBG_UTIL
268 DBG_ERROR( "Cannot access \"link\" checkbox" );
269 #endif
272 return sal_False;
276 int SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
278 return mpImpl->aFileDlg.GetGraphic(rGraphic);
282 String SvxOpenGraphicDialog::GetPath() const
284 return mpImpl->aFileDlg.GetPath();
288 String SvxOpenGraphicDialog::GetCurrentFilter() const
290 return mpImpl->aFileDlg.GetCurrentFilter();
294 void SvxOpenGraphicDialog::SetCurrentFilter(const String& rStr)
296 mpImpl->aFileDlg.SetCurrentFilter(rStr);
299 void SvxOpenGraphicDialog::SetControlHelpIds( const INT16* _pControlId, const INT32* _pHelpId )
301 mpImpl->aFileDlg.SetControlHelpIds( _pControlId, _pHelpId );
304 void SvxOpenGraphicDialog::SetDialogHelpId( const INT32 _nHelpId )
306 mpImpl->aFileDlg.SetDialogHelpId( _nHelpId );