bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / helper / commandimageprovider.cxx
blobaa8bd1e72e3a15a4237b7e426b05493a7f3d76cb
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/theModuleUIConfigurationManagerSupplier.hpp>
26 #include <com/sun/star/frame/ModuleManager.hpp>
27 #include <com/sun/star/ui/ImageType.hpp>
29 #include <tools/diagnose_ex.h>
32 namespace frm
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::uno::XComponentContext;
48 using ::com::sun::star::frame::XModel;
49 using ::com::sun::star::ui::XImageManager;
50 using ::com::sun::star::ui::XUIConfigurationManagerSupplier;
51 using ::com::sun::star::ui::XUIConfigurationManager;
52 using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
53 using ::com::sun::star::ui::theModuleUIConfigurationManagerSupplier;
54 using ::com::sun::star::frame::ModuleManager;
55 using ::com::sun::star::frame::XModuleManager2;
56 using ::com::sun::star::graphic::XGraphic;
58 namespace ImageType = ::com::sun::star::ui::ImageType;
60 class DocumentCommandImageProvider : public ICommandImageProvider
62 public:
63 DocumentCommandImageProvider( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
65 impl_init_nothrow( _rContext, _rxDocument );
67 virtual ~DocumentCommandImageProvider()
71 // ICommandImageProvider
72 virtual CommandImages getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const SAL_OVERRIDE;
74 private:
75 void impl_init_nothrow( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument );
77 private:
78 Reference< XImageManager > m_xDocumentImageManager;
79 Reference< XImageManager > m_xModuleImageManager;
83 void DocumentCommandImageProvider::impl_init_nothrow( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
85 OSL_ENSURE( _rxDocument.is(), "DocumentCommandImageProvider::impl_init_nothrow: no document => no images!" );
86 if ( !_rxDocument.is() )
87 return;
89 // obtain the image manager of the document
90 try
92 Reference< XUIConfigurationManagerSupplier > xSuppUIConfig( _rxDocument, UNO_QUERY_THROW );
93 Reference< XUIConfigurationManager > xUIConfig( xSuppUIConfig->getUIConfigurationManager(), UNO_QUERY );
94 m_xDocumentImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
96 catch( const Exception& )
98 DBG_UNHANDLED_EXCEPTION();
101 // obtain the image manager or the module
104 Reference< XModuleManager2 > xModuleManager( ModuleManager::create(_rContext) );
105 OUString sModuleID = xModuleManager->identify( _rxDocument );
107 Reference< XModuleUIConfigurationManagerSupplier > xSuppUIConfig(
108 theModuleUIConfigurationManagerSupplier::get(_rContext) );
109 Reference< XUIConfigurationManager > xUIConfig(
110 xSuppUIConfig->getUIConfigurationManager( sModuleID ), UNO_SET_THROW );
111 m_xModuleImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
113 catch( const Exception& )
115 DBG_UNHANDLED_EXCEPTION();
120 CommandImages DocumentCommandImageProvider::getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const
122 const size_t nCommandCount = _rCommandURLs.getLength();
123 CommandImages aImages( nCommandCount );
126 const sal_Int16 nImageType = ImageType::COLOR_NORMAL
127 + ( _bLarge ? ImageType::SIZE_LARGE : ImageType::SIZE_DEFAULT );
129 Sequence< Reference< XGraphic > > aDocImages( nCommandCount );
130 Sequence< Reference< XGraphic > > aModImages( nCommandCount );
132 // first try the document image manager
133 if ( m_xDocumentImageManager.is() )
134 aDocImages = m_xDocumentImageManager->getImages( nImageType, _rCommandURLs );
136 // then the module's image manager
137 if ( m_xModuleImageManager.is() )
138 aModImages = m_xModuleImageManager->getImages( nImageType, _rCommandURLs );
140 ENSURE_OR_THROW( (size_t)aDocImages.getLength() == nCommandCount, "illegal array size returned by getImages (document image manager)" );
141 ENSURE_OR_THROW( (size_t)aModImages.getLength() == nCommandCount, "illegal array size returned by getImages (module image manager)" );
143 for ( size_t i=0; i<nCommandCount; ++i )
145 if ( aDocImages[i].is() )
146 aImages[i] = Image( aDocImages[i] );
147 else
148 aImages[i] = Image( aModImages[i] );
151 catch( const Exception& )
153 DBG_UNHANDLED_EXCEPTION();
155 return aImages;
159 PCommandImageProvider createDocumentCommandImageProvider(
160 const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
162 PCommandImageProvider pImageProvider( new DocumentCommandImageProvider( _rContext, _rxDocument ) );
163 return pImageProvider;
167 } // namespace frm
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */