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 :
21 : #include "commandimageprovider.hxx"
22 :
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>
28 :
29 : #include <tools/diagnose_ex.h>
30 :
31 :
32 : namespace frm
33 : {
34 :
35 :
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;
57 :
58 : namespace ImageType = ::com::sun::star::ui::ImageType;
59 :
60 : class DocumentCommandImageProvider : public ICommandImageProvider
61 : {
62 : public:
63 36 : DocumentCommandImageProvider( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
64 36 : {
65 36 : impl_init_nothrow( _rContext, _rxDocument );
66 36 : }
67 72 : virtual ~DocumentCommandImageProvider()
68 36 : {
69 72 : }
70 :
71 : // ICommandImageProvider
72 : virtual CommandImages getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const SAL_OVERRIDE;
73 :
74 : private:
75 : void impl_init_nothrow( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument );
76 :
77 : private:
78 : Reference< XImageManager > m_xDocumentImageManager;
79 : Reference< XImageManager > m_xModuleImageManager;
80 : };
81 :
82 :
83 36 : void DocumentCommandImageProvider::impl_init_nothrow( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
84 : {
85 : OSL_ENSURE( _rxDocument.is(), "DocumentCommandImageProvider::impl_init_nothrow: no document => no images!" );
86 36 : if ( !_rxDocument.is() )
87 36 : return;
88 :
89 : // obtain the image manager of the document
90 : try
91 : {
92 36 : Reference< XUIConfigurationManagerSupplier > xSuppUIConfig( _rxDocument, UNO_QUERY_THROW );
93 72 : Reference< XUIConfigurationManager > xUIConfig( xSuppUIConfig->getUIConfigurationManager(), UNO_QUERY );
94 72 : m_xDocumentImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
95 : }
96 0 : catch( const Exception& )
97 : {
98 : DBG_UNHANDLED_EXCEPTION();
99 : }
100 :
101 : // obtain the image manager or the module
102 : try
103 : {
104 36 : Reference< XModuleManager2 > xModuleManager( ModuleManager::create(_rContext) );
105 72 : OUString sModuleID = xModuleManager->identify( _rxDocument );
106 :
107 : Reference< XModuleUIConfigurationManagerSupplier > xSuppUIConfig(
108 72 : theModuleUIConfigurationManagerSupplier::get(_rContext) );
109 : Reference< XUIConfigurationManager > xUIConfig(
110 72 : xSuppUIConfig->getUIConfigurationManager( sModuleID ), UNO_SET_THROW );
111 72 : m_xModuleImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
112 : }
113 0 : catch( const Exception& )
114 : {
115 : DBG_UNHANDLED_EXCEPTION();
116 : }
117 : }
118 :
119 :
120 90 : CommandImages DocumentCommandImageProvider::getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const
121 : {
122 90 : const size_t nCommandCount = _rCommandURLs.getLength();
123 90 : CommandImages aImages( nCommandCount );
124 : try
125 : {
126 : const sal_Int16 nImageType = ImageType::COLOR_NORMAL
127 90 : + ( _bLarge ? ImageType::SIZE_LARGE : ImageType::SIZE_DEFAULT );
128 :
129 90 : Sequence< Reference< XGraphic > > aDocImages( nCommandCount );
130 180 : Sequence< Reference< XGraphic > > aModImages( nCommandCount );
131 :
132 : // first try the document image manager
133 90 : if ( m_xDocumentImageManager.is() )
134 90 : aDocImages = m_xDocumentImageManager->getImages( nImageType, _rCommandURLs );
135 :
136 : // then the module's image manager
137 90 : if ( m_xModuleImageManager.is() )
138 90 : aModImages = m_xModuleImageManager->getImages( nImageType, _rCommandURLs );
139 :
140 90 : ENSURE_OR_THROW( (size_t)aDocImages.getLength() == nCommandCount, "illegal array size returned by getImages (document image manager)" );
141 90 : ENSURE_OR_THROW( (size_t)aModImages.getLength() == nCommandCount, "illegal array size returned by getImages (module image manager)" );
142 :
143 1800 : for ( size_t i=0; i<nCommandCount; ++i )
144 : {
145 1710 : if ( aDocImages[i].is() )
146 0 : aImages[i] = Image( aDocImages[i] );
147 : else
148 1710 : aImages[i] = Image( aModImages[i] );
149 90 : }
150 : }
151 0 : catch( const Exception& )
152 : {
153 : DBG_UNHANDLED_EXCEPTION();
154 : }
155 90 : return aImages;
156 : }
157 :
158 :
159 36 : PCommandImageProvider createDocumentCommandImageProvider(
160 : const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
161 : {
162 36 : PCommandImageProvider pImageProvider( new DocumentCommandImageProvider( _rContext, _rxDocument ) );
163 36 : return pImageProvider;
164 : }
165 :
166 :
167 192 : } // namespace frm
168 :
169 :
170 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|