1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25 ************************************************************************/
26 
27 // MARKER(update_precomp.py): autogen include statement, do not remove
28 #include "precompiled_forms.hxx"
29 
30 #include "commandimageprovider.hxx"
31 
32 /** === begin UNO includes === **/
33 #include <com/sun/star/ui/XImageManager.hpp>
34 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
35 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
36 #include <com/sun/star/frame/XModuleManager.hpp>
37 #include <com/sun/star/ui/ImageType.hpp>
38 /** === end UNO includes === **/
39 
40 #include <tools/diagnose_ex.h>
41 
42 //........................................................................
43 namespace frm
44 {
45 //........................................................................
46 
47 	/** === begin UNO using === **/
48 	using ::com::sun::star::uno::Reference;
49 	using ::com::sun::star::uno::XInterface;
50 	using ::com::sun::star::uno::UNO_QUERY;
51 	using ::com::sun::star::uno::UNO_QUERY_THROW;
52 	using ::com::sun::star::uno::UNO_SET_THROW;
53 	using ::com::sun::star::uno::Exception;
54 	using ::com::sun::star::uno::RuntimeException;
55 	using ::com::sun::star::uno::Any;
56 	using ::com::sun::star::uno::makeAny;
57 	using ::com::sun::star::uno::Sequence;
58 	using ::com::sun::star::uno::Type;
59     using ::com::sun::star::frame::XModel;
60     using ::com::sun::star::ui::XImageManager;
61     using ::com::sun::star::ui::XUIConfigurationManagerSupplier;
62     using ::com::sun::star::ui::XUIConfigurationManager;
63     using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
64     using ::com::sun::star::frame::XModuleManager;
65     using ::com::sun::star::graphic::XGraphic;
66 	/** === end UNO using === **/
67     namespace ImageType = ::com::sun::star::ui::ImageType;
68 
69 	//====================================================================
70 	//= DocumentCommandImageProvider
71 	//====================================================================
72     class DocumentCommandImageProvider : public ICommandImageProvider
73     {
74     public:
75         DocumentCommandImageProvider( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
76         {
77             impl_init_nothrow( _rContext, _rxDocument );
78         }
79         virtual ~DocumentCommandImageProvider()
80         {
81         }
82 
83         // ICommandImageProvider
84         virtual CommandImages getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge, const bool _bHiContrast ) const;
85 
86     private:
87         void    impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument );
88 
89     private:
90         Reference< XImageManager >    m_xDocumentImageManager;
91         Reference< XImageManager >    m_xModuleImageManager;
92     };
93 
94     //--------------------------------------------------------------------
95     void DocumentCommandImageProvider::impl_init_nothrow( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
96     {
97         OSL_ENSURE( _rxDocument.is(), "DocumentCommandImageProvider::impl_init_nothrow: no document => no images!" );
98         if ( !_rxDocument.is() )
99             return;
100 
101         // obtain the image manager of the document
102         try
103         {
104             Reference< XUIConfigurationManagerSupplier > xSuppUIConfig( _rxDocument, UNO_QUERY_THROW );
105             Reference< XUIConfigurationManager > xUIConfig( xSuppUIConfig->getUIConfigurationManager(), UNO_QUERY );
106             m_xDocumentImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
107         }
108         catch( const Exception& )
109         {
110         	DBG_UNHANDLED_EXCEPTION();
111         }
112 
113         // obtain the image manager or the module
114         try
115         {
116             Reference< XModuleManager > xModuleManager( _rContext.createComponent( "com.sun.star.frame.ModuleManager" ), UNO_QUERY_THROW );
117             ::rtl::OUString sModuleID = xModuleManager->identify( _rxDocument );
118 
119             Reference< XModuleUIConfigurationManagerSupplier > xSuppUIConfig(
120                 _rContext.createComponent( "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ), UNO_QUERY_THROW );
121             Reference< XUIConfigurationManager > xUIConfig(
122                 xSuppUIConfig->getUIConfigurationManager( sModuleID ), UNO_SET_THROW );
123             m_xModuleImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
124         }
125         catch( const Exception& )
126         {
127         	DBG_UNHANDLED_EXCEPTION();
128         }
129     }
130 
131     //--------------------------------------------------------------------
132     CommandImages DocumentCommandImageProvider::getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge, const bool _bHiContrast ) const
133     {
134         const size_t nCommandCount = _rCommandURLs.getLength();
135         CommandImages aImages( nCommandCount );
136         try
137         {
138             const sal_Int16 nImageType =
139                     ( _bLarge       ? ImageType::SIZE_LARGE         : ImageType::SIZE_DEFAULT )
140                 +   ( _bHiContrast  ? ImageType::COLOR_HIGHCONTRAST : ImageType::COLOR_NORMAL );
141 
142             Sequence< Reference< XGraphic > > aDocImages( nCommandCount );
143             Sequence< Reference< XGraphic > > aModImages( nCommandCount );
144 
145             // first try the document image manager
146             if ( m_xDocumentImageManager.is() )
147                 aDocImages = m_xDocumentImageManager->getImages( nImageType, _rCommandURLs );
148 
149             // then the module's image manager
150             if ( m_xModuleImageManager.is() )
151                 aModImages = m_xModuleImageManager->getImages( nImageType, _rCommandURLs );
152 
153             ENSURE_OR_THROW( (size_t)aDocImages.getLength() == nCommandCount, "illegal array size returned by getImages (document image manager)" );
154             ENSURE_OR_THROW( (size_t)aModImages.getLength() == nCommandCount, "illegal array size returned by getImages (module image manager)" );
155 
156             for ( size_t i=0; i<nCommandCount; ++i )
157             {
158                 if ( aDocImages[i].is() )
159                     aImages[i] = Image( aDocImages[i] );
160                 else
161                     aImages[i] = Image( aModImages[i] );
162             }
163         }
164         catch( const Exception& )
165         {
166         	DBG_UNHANDLED_EXCEPTION();
167         }
168         return aImages;
169     }
170 
171     //--------------------------------------------------------------------
172     PCommandImageProvider createDocumentCommandImageProvider(
173         const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
174     {
175         PCommandImageProvider pImageProvider( new DocumentCommandImageProvider( _rContext, _rxDocument ) );
176         return pImageProvider;
177     }
178 
179 //........................................................................
180 } // namespace frm
181 //........................................................................
182