1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 
27 #include "commandimageprovider.hxx"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/ui/XImageManager.hpp>
31 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
32 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
33 #include <com/sun/star/frame/XModuleManager.hpp>
34 #include <com/sun/star/ui/ImageType.hpp>
35 /** === end UNO includes === **/
36 
37 #include <tools/diagnose_ex.h>
38 
39 //........................................................................
40 namespace frm
41 {
42 //........................................................................
43 
44 	/** === begin UNO using === **/
45 	using ::com::sun::star::uno::Reference;
46 	using ::com::sun::star::uno::XInterface;
47 	using ::com::sun::star::uno::UNO_QUERY;
48 	using ::com::sun::star::uno::UNO_QUERY_THROW;
49 	using ::com::sun::star::uno::UNO_SET_THROW;
50 	using ::com::sun::star::uno::Exception;
51 	using ::com::sun::star::uno::RuntimeException;
52 	using ::com::sun::star::uno::Any;
53 	using ::com::sun::star::uno::makeAny;
54 	using ::com::sun::star::uno::Sequence;
55 	using ::com::sun::star::uno::Type;
56     using ::com::sun::star::frame::XModel;
57     using ::com::sun::star::ui::XImageManager;
58     using ::com::sun::star::ui::XUIConfigurationManagerSupplier;
59     using ::com::sun::star::ui::XUIConfigurationManager;
60     using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
61     using ::com::sun::star::frame::XModuleManager;
62     using ::com::sun::star::graphic::XGraphic;
63 	/** === end UNO using === **/
64     namespace ImageType = ::com::sun::star::ui::ImageType;
65 
66 	//====================================================================
67 	//= DocumentCommandImageProvider
68 	//====================================================================
69     class DocumentCommandImageProvider : public ICommandImageProvider
70     {
71     public:
DocumentCommandImageProvider(const::comphelper::ComponentContext & _rContext,const Reference<XModel> & _rxDocument)72         DocumentCommandImageProvider( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
73         {
74             impl_init_nothrow( _rContext, _rxDocument );
75         }
~DocumentCommandImageProvider()76         virtual ~DocumentCommandImageProvider()
77         {
78         }
79 
80         // ICommandImageProvider
81         virtual CommandImages getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge, const bool _bHiContrast ) const;
82 
83     private:
84         void    impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument );
85 
86     private:
87         Reference< XImageManager >    m_xDocumentImageManager;
88         Reference< XImageManager >    m_xModuleImageManager;
89     };
90 
91     //--------------------------------------------------------------------
impl_init_nothrow(const::comphelper::ComponentContext & _rContext,const Reference<XModel> & _rxDocument)92     void DocumentCommandImageProvider::impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
93     {
94         OSL_ENSURE( _rxDocument.is(), "DocumentCommandImageProvider::impl_init_nothrow: no document => no images!" );
95         if ( !_rxDocument.is() )
96             return;
97 
98         // obtain the image manager of the document
99         try
100         {
101             Reference< XUIConfigurationManagerSupplier > xSuppUIConfig( _rxDocument, UNO_QUERY_THROW );
102             Reference< XUIConfigurationManager > xUIConfig( xSuppUIConfig->getUIConfigurationManager(), UNO_QUERY );
103             m_xDocumentImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
104         }
105         catch( const Exception& )
106         {
107         	DBG_UNHANDLED_EXCEPTION();
108         }
109 
110         // obtain the image manager or the module
111         try
112         {
113             Reference< XModuleManager > xModuleManager( _rContext.createComponent( "com.sun.star.frame.ModuleManager" ), UNO_QUERY_THROW );
114             ::rtl::OUString sModuleID = xModuleManager->identify( _rxDocument );
115 
116             Reference< XModuleUIConfigurationManagerSupplier > xSuppUIConfig(
117                 _rContext.createComponent( "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ), UNO_QUERY_THROW );
118             Reference< XUIConfigurationManager > xUIConfig(
119                 xSuppUIConfig->getUIConfigurationManager( sModuleID ), UNO_SET_THROW );
120             m_xModuleImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
121         }
122         catch( const Exception& )
123         {
124         	DBG_UNHANDLED_EXCEPTION();
125         }
126     }
127 
128     //--------------------------------------------------------------------
getCommandImages(const CommandURLs & _rCommandURLs,const bool _bLarge,const bool _bHiContrast) const129     CommandImages DocumentCommandImageProvider::getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge, const bool _bHiContrast ) const
130     {
131         const size_t nCommandCount = _rCommandURLs.getLength();
132         CommandImages aImages( nCommandCount );
133         try
134         {
135             const sal_Int16 nImageType =
136                     ( _bLarge       ? ImageType::SIZE_LARGE         : ImageType::SIZE_DEFAULT )
137                 +   ( _bHiContrast  ? ImageType::COLOR_HIGHCONTRAST : ImageType::COLOR_NORMAL );
138 
139             Sequence< Reference< XGraphic > > aDocImages( nCommandCount );
140             Sequence< Reference< XGraphic > > aModImages( nCommandCount );
141 
142             // first try the document image manager
143             if ( m_xDocumentImageManager.is() )
144                 aDocImages = m_xDocumentImageManager->getImages( nImageType, _rCommandURLs );
145 
146             // then the module's image manager
147             if ( m_xModuleImageManager.is() )
148                 aModImages = m_xModuleImageManager->getImages( nImageType, _rCommandURLs );
149 
150             ENSURE_OR_THROW( (size_t)aDocImages.getLength() == nCommandCount, "illegal array size returned by getImages (document image manager)" );
151             ENSURE_OR_THROW( (size_t)aModImages.getLength() == nCommandCount, "illegal array size returned by getImages (module image manager)" );
152 
153             for ( size_t i=0; i<nCommandCount; ++i )
154             {
155                 if ( aDocImages[i].is() )
156                     aImages[i] = Image( aDocImages[i] );
157                 else
158                     aImages[i] = Image( aModImages[i] );
159             }
160         }
161         catch( const Exception& )
162         {
163         	DBG_UNHANDLED_EXCEPTION();
164         }
165         return aImages;
166     }
167 
168     //--------------------------------------------------------------------
createDocumentCommandImageProvider(const::comphelper::ComponentContext & _rContext,const Reference<XModel> & _rxDocument)169     PCommandImageProvider createDocumentCommandImageProvider(
170         const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
171     {
172         PCommandImageProvider pImageProvider( new DocumentCommandImageProvider( _rContext, _rxDocument ) );
173         return pImageProvider;
174     }
175 
176 //........................................................................
177 } // namespace frm
178 //........................................................................
179