merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / source / appl / imagemgr.cxx
blob9c2a7996e3988bf65feede4a36d2f3ca7001791d
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: imagemgr.cxx,v $
10 * $Revision: 1.20 $
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_sfx2.hxx"
33 #include "imagemgr.hxx"
34 #include <com/sun/star/frame/XController.hpp>
35 #include <com/sun/star/ui/XImageManager.hpp>
36 #include <com/sun/star/frame/XModuleManager.hpp>
37 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
38 #include <com/sun/star/ui/ImageType.hpp>
39 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
41 #include <tools/urlobj.hxx>
42 #include <svtools/imagemgr.hxx>
43 #include <comphelper/processfactory.hxx>
44 #include <rtl/ustring.hxx>
45 #include <rtl/logfile.hxx>
47 #include "imgmgr.hxx"
48 #include <sfx2/app.hxx>
49 #include <sfx2/unoctitm.hxx>
50 #include <sfx2/dispatch.hxx>
51 #include <sfx2/msg.hxx>
52 #include <sfx2/msgpool.hxx>
53 #include <sfx2/viewfrm.hxx>
54 #include <sfx2/module.hxx>
55 #include <sfx2/objsh.hxx>
56 #include <sfx2/docfac.hxx>
58 #include <hash_map>
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::frame;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::util;
64 using namespace ::com::sun::star::ui;
65 using namespace ::com::sun::star::frame;
67 typedef std::hash_map< ::rtl::OUString,
68 WeakReference< XImageManager >,
69 ::rtl::OUStringHash,
70 ::std::equal_to< ::rtl::OUString > > ModuleIdToImagegMgr;
72 static WeakReference< XModuleManager > m_xModuleManager;
73 static WeakReference< XModuleUIConfigurationManagerSupplier > m_xModuleCfgMgrSupplier;
74 static WeakReference< XURLTransformer > m_xURLTransformer;
75 static ModuleIdToImagegMgr m_aModuleIdToImageMgrMap;
77 Image SAL_CALL GetImage( ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame, const ::rtl::OUString& aURL, BOOL bBig, BOOL bHiContrast )
79 // TODO/LATeR: shouldn't this become a method at SfxViewFrame?! That would save the UnoTunnel
80 if ( !rFrame.is() )
81 return Image();
83 INetURLObject aObj( aURL );
84 INetProtocol nProtocol = aObj.GetProtocol();
86 Reference < XController > xController;
87 Reference < XModel > xModel;
88 if ( rFrame.is() )
89 xController = rFrame->getController();
90 if ( xController.is() )
91 xModel = xController->getModel();
93 rtl::OUString aCommandURL( aURL );
94 if ( nProtocol == INET_PROT_SLOT )
97 // Support old way to retrieve image via slot URL
98 Reference< XURLTransformer > xURLTransformer = m_xURLTransformer;
99 if ( !xURLTransformer.is() )
101 xURLTransformer = Reference< XURLTransformer >(
102 ::comphelper::getProcessServiceFactory()->createInstance(
103 rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )),
104 UNO_QUERY );
105 m_xURLTransformer = xURLTransformer;
108 URL aTargetURL;
109 aTargetURL.Complete = aURL;
110 xURLTransformer->parseStrict( aTargetURL );
111 USHORT nId = ( USHORT ) aTargetURL.Path.toInt32();*/
112 USHORT nId = ( USHORT ) String(aURL).Copy(5).ToInt32();
113 const SfxSlot* pSlot = 0;
114 if ( xModel.is() )
116 Reference < XUnoTunnel > xObj( xModel, UNO_QUERY );
117 Sequence < sal_Int8 > aSeq( SvGlobalName( SFX_GLOBAL_CLASSID ).GetByteSequence() );
118 sal_Int64 nHandle = xObj.is() ? xObj->getSomething( aSeq ) : 0;
119 if ( nHandle )
121 SfxObjectShell* pDoc = reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>( nHandle ));
122 SfxModule* pModule = pDoc->GetFactory().GetModule();
123 pSlot = pModule->GetSlotPool()->GetSlot( nId );
126 else
127 pSlot = SfxSlotPool::GetSlotPool().GetSlot( nId );
129 if ( pSlot )
131 aCommandURL = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
132 aCommandURL += rtl::OUString::createFromAscii( pSlot->GetUnoName() );
134 else
135 aCommandURL = rtl::OUString();
138 Reference< XImageManager > xDocImgMgr;
139 if ( xModel.is() )
141 Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
142 if ( xSupplier.is() )
144 Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
145 xDocImgMgr = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
149 sal_Int16 nImageType( ::com::sun::star::ui::ImageType::COLOR_NORMAL|
150 ::com::sun::star::ui::ImageType::SIZE_DEFAULT );
151 if ( bBig )
152 nImageType |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
153 if ( bHiContrast )
154 nImageType |= ::com::sun::star::ui::ImageType::COLOR_HIGHCONTRAST;
156 if ( xDocImgMgr.is() )
158 Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
159 Sequence< rtl::OUString > aImageCmdSeq( 1 );
160 aImageCmdSeq[0] = aCommandURL;
164 aGraphicSeq = xDocImgMgr->getImages( nImageType, aImageCmdSeq );
165 Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
166 Image aImage( xGraphic );
168 if ( !!aImage )
169 return aImage;
171 catch ( Exception& )
176 Reference< XModuleManager > xModuleManager = m_xModuleManager;
178 if ( !xModuleManager.is() )
180 xModuleManager = Reference< XModuleManager >(
181 ::comphelper::getProcessServiceFactory()->createInstance(
182 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
183 "com.sun.star.frame.ModuleManager" ))),
184 UNO_QUERY );
185 m_xModuleManager = xModuleManager;
190 if ( aCommandURL.getLength() > 0 )
192 Reference< XImageManager > xModuleImageManager;
193 rtl::OUString aModuleId = xModuleManager->identify( rFrame );
194 ModuleIdToImagegMgr::iterator pIter = m_aModuleIdToImageMgrMap.find( aModuleId );
195 if ( pIter != m_aModuleIdToImageMgrMap.end() )
196 xModuleImageManager = pIter->second;
197 else
199 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier = m_xModuleCfgMgrSupplier;
201 if ( !xModuleCfgMgrSupplier.is() )
203 xModuleCfgMgrSupplier = Reference< XModuleUIConfigurationManagerSupplier >(
204 ::comphelper::getProcessServiceFactory()->createInstance(
205 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
206 "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ))),
207 UNO_QUERY );
209 m_xModuleCfgMgrSupplier = xModuleCfgMgrSupplier;
212 Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId );
213 xModuleImageManager = Reference< XImageManager >( xUICfgMgr->getImageManager(), UNO_QUERY );
214 m_aModuleIdToImageMgrMap.insert( ModuleIdToImagegMgr::value_type( aModuleId, xModuleImageManager ));
217 Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
218 Sequence< rtl::OUString > aImageCmdSeq( 1 );
219 aImageCmdSeq[0] = aCommandURL;
221 aGraphicSeq = xModuleImageManager->getImages( nImageType, aImageCmdSeq );
223 Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
224 Image aImage( xGraphic );
226 if ( !!aImage )
227 return aImage;
228 else if ( nProtocol != INET_PROT_UNO && nProtocol != INET_PROT_SLOT )
229 return SvFileInformationManager::GetImageNoDefault( aObj, bBig, bHiContrast );
232 catch ( Exception& )
236 return Image();