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