update credits
[LibreOffice.git] / forms / source / helper / commandimageprovider.cxx
blobb9c0575659b7ba396a1cd3fa8f706d019207485e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "commandimageprovider.hxx"
23 #include <com/sun/star/ui/XImageManager.hpp>
24 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
25 #include <com/sun/star/ui/ModuleUIConfigurationManagerSupplier.hpp>
26 #include <com/sun/star/frame/ModuleManager.hpp>
27 #include <com/sun/star/ui/ImageType.hpp>
29 #include <tools/diagnose_ex.h>
31 //........................................................................
32 namespace frm
34 //........................................................................
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::XInterface;
38 using ::com::sun::star::uno::UNO_QUERY;
39 using ::com::sun::star::uno::UNO_QUERY_THROW;
40 using ::com::sun::star::uno::UNO_SET_THROW;
41 using ::com::sun::star::uno::Exception;
42 using ::com::sun::star::uno::RuntimeException;
43 using ::com::sun::star::uno::Any;
44 using ::com::sun::star::uno::makeAny;
45 using ::com::sun::star::uno::Sequence;
46 using ::com::sun::star::uno::Type;
47 using ::com::sun::star::frame::XModel;
48 using ::com::sun::star::ui::XImageManager;
49 using ::com::sun::star::ui::XUIConfigurationManagerSupplier;
50 using ::com::sun::star::ui::XUIConfigurationManager;
51 using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
52 using ::com::sun::star::ui::ModuleUIConfigurationManagerSupplier;
53 using ::com::sun::star::frame::ModuleManager;
54 using ::com::sun::star::frame::XModuleManager2;
55 using ::com::sun::star::graphic::XGraphic;
57 namespace ImageType = ::com::sun::star::ui::ImageType;
59 //====================================================================
60 //= DocumentCommandImageProvider
61 //====================================================================
62 class DocumentCommandImageProvider : public ICommandImageProvider
64 public:
65 DocumentCommandImageProvider( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
67 impl_init_nothrow( _rContext, _rxDocument );
69 virtual ~DocumentCommandImageProvider()
73 // ICommandImageProvider
74 virtual CommandImages getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const;
76 private:
77 void impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument );
79 private:
80 Reference< XImageManager > m_xDocumentImageManager;
81 Reference< XImageManager > m_xModuleImageManager;
84 //--------------------------------------------------------------------
85 void DocumentCommandImageProvider::impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
87 OSL_ENSURE( _rxDocument.is(), "DocumentCommandImageProvider::impl_init_nothrow: no document => no images!" );
88 if ( !_rxDocument.is() )
89 return;
91 // obtain the image manager of the document
92 try
94 Reference< XUIConfigurationManagerSupplier > xSuppUIConfig( _rxDocument, UNO_QUERY_THROW );
95 Reference< XUIConfigurationManager > xUIConfig( xSuppUIConfig->getUIConfigurationManager(), UNO_QUERY );
96 m_xDocumentImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
98 catch( const Exception& )
100 DBG_UNHANDLED_EXCEPTION();
103 // obtain the image manager or the module
106 Reference< XModuleManager2 > xModuleManager( ModuleManager::create(_rContext.getUNOContext()) );
107 OUString sModuleID = xModuleManager->identify( _rxDocument );
109 Reference< XModuleUIConfigurationManagerSupplier > xSuppUIConfig(
110 ModuleUIConfigurationManagerSupplier::create(_rContext.getUNOContext()) );
111 Reference< XUIConfigurationManager > xUIConfig(
112 xSuppUIConfig->getUIConfigurationManager( sModuleID ), UNO_SET_THROW );
113 m_xModuleImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
115 catch( const Exception& )
117 DBG_UNHANDLED_EXCEPTION();
121 //--------------------------------------------------------------------
122 CommandImages DocumentCommandImageProvider::getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const
124 const size_t nCommandCount = _rCommandURLs.getLength();
125 CommandImages aImages( nCommandCount );
128 const sal_Int16 nImageType = ImageType::COLOR_NORMAL
129 + ( _bLarge ? ImageType::SIZE_LARGE : ImageType::SIZE_DEFAULT );
131 Sequence< Reference< XGraphic > > aDocImages( nCommandCount );
132 Sequence< Reference< XGraphic > > aModImages( nCommandCount );
134 // first try the document image manager
135 if ( m_xDocumentImageManager.is() )
136 aDocImages = m_xDocumentImageManager->getImages( nImageType, _rCommandURLs );
138 // then the module's image manager
139 if ( m_xModuleImageManager.is() )
140 aModImages = m_xModuleImageManager->getImages( nImageType, _rCommandURLs );
142 ENSURE_OR_THROW( (size_t)aDocImages.getLength() == nCommandCount, "illegal array size returned by getImages (document image manager)" );
143 ENSURE_OR_THROW( (size_t)aModImages.getLength() == nCommandCount, "illegal array size returned by getImages (module image manager)" );
145 for ( size_t i=0; i<nCommandCount; ++i )
147 if ( aDocImages[i].is() )
148 aImages[i] = Image( aDocImages[i] );
149 else
150 aImages[i] = Image( aModImages[i] );
153 catch( const Exception& )
155 DBG_UNHANDLED_EXCEPTION();
157 return aImages;
160 //--------------------------------------------------------------------
161 PCommandImageProvider createDocumentCommandImageProvider(
162 const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
164 PCommandImageProvider pImageProvider( new DocumentCommandImageProvider( _rContext, _rxDocument ) );
165 return pImageProvider;
168 //........................................................................
169 } // namespace frm
170 //........................................................................
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */