1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <uielement/menubarwrapper.hxx>
21 #include <framework/actiontriggerhelper.hxx>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
27 #include <com/sun/star/awt/XMenuBar.hpp>
28 #include <com/sun/star/container/XIndexContainer.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 #include <com/sun/star/ui/UIElementType.hpp>
31 #include <com/sun/star/frame/ModuleManager.hpp>
32 #include <com/sun/star/util/URLTransformer.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <vcl/svapp.hxx>
37 using namespace com::sun::star
;
38 using namespace com::sun::star::uno
;
39 using namespace com::sun::star::beans
;
40 using namespace com::sun::star::frame
;
41 using namespace com::sun::star::lang
;
42 using namespace com::sun::star::container
;
43 using namespace com::sun::star::awt
;
44 using namespace com::sun::star::util
;
45 using namespace ::com::sun::star::ui
;
50 // XInterface, XTypeProvider
51 DEFINE_XINTERFACE_11 ( MenuBarWrapper
,
52 UIConfigElementWrapperBase
,
53 DIRECT_INTERFACE( ::com::sun::star::lang::XTypeProvider
),
54 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElement
),
55 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElementSettings
),
56 DIRECT_INTERFACE( ::com::sun::star::beans::XMultiPropertySet
),
57 DIRECT_INTERFACE( ::com::sun::star::beans::XFastPropertySet
),
58 DIRECT_INTERFACE( ::com::sun::star::beans::XPropertySet
),
59 DIRECT_INTERFACE( ::com::sun::star::lang::XInitialization
),
60 DIRECT_INTERFACE( ::com::sun::star::lang::XComponent
),
61 DIRECT_INTERFACE( ::com::sun::star::util::XUpdatable
),
62 DIRECT_INTERFACE( ::com::sun::star::ui::XUIConfigurationListener
),
63 DERIVED_INTERFACE( ::com::sun::star::container::XNameAccess
, ::com::sun::star::container::XElementAccess
)
66 DEFINE_XTYPEPROVIDER_11 ( MenuBarWrapper
,
67 ::com::sun::star::lang::XTypeProvider
,
68 ::com::sun::star::ui::XUIElement
,
69 ::com::sun::star::ui::XUIElementSettings
,
70 ::com::sun::star::beans::XMultiPropertySet
,
71 ::com::sun::star::beans::XFastPropertySet
,
72 ::com::sun::star::beans::XPropertySet
,
73 ::com::sun::star::lang::XInitialization
,
74 ::com::sun::star::lang::XComponent
,
75 ::com::sun::star::util::XUpdatable
,
76 ::com::sun::star::ui::XUIConfigurationListener
,
77 ::com::sun::star::container::XNameAccess
80 MenuBarWrapper::MenuBarWrapper(
81 const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>& rxContext
83 : UIConfigElementWrapperBase( UIElementType::MENUBAR
),
84 m_bRefreshPopupControllerCache( true ),
85 m_xContext( rxContext
)
89 MenuBarWrapper::~MenuBarWrapper()
93 void SAL_CALL
MenuBarWrapper::dispose() throw (::com::sun::star::uno::RuntimeException
, std::exception
)
95 Reference
< XComponent
> xThis( static_cast< OWeakObject
* >(this), UNO_QUERY
);
97 com::sun::star::lang::EventObject
aEvent( xThis
);
98 m_aListenerContainer
.disposeAndClear( aEvent
);
102 m_xMenuBarManager
->dispose();
103 m_xMenuBarManager
.clear();
104 m_xConfigSource
.clear();
105 m_xConfigData
.clear();
112 void SAL_CALL
MenuBarWrapper::initialize( const Sequence
< Any
>& aArguments
) throw ( Exception
, RuntimeException
, std::exception
)
117 throw DisposedException();
119 if ( !m_bInitialized
)
121 OUString aModuleIdentifier
;
122 UIConfigElementWrapperBase::initialize( aArguments
);
124 Reference
< XFrame
> xFrame( m_xWeakFrame
);
125 if ( xFrame
.is() && m_xConfigSource
.is() )
127 // Create VCL menubar which will be filled with settings data
128 MenuBar
* pVCLMenuBar
= 0;
129 VCLXMenuBar
* pAwtMenuBar
= 0;
131 SolarMutexGuard aSolarMutexGuard
;
132 pVCLMenuBar
= new MenuBar();
135 Reference
< XModuleManager2
> xModuleManager
= ModuleManager::create( m_xContext
);
139 aModuleIdentifier
= xModuleManager
->identify( xFrame
);
141 catch( const Exception
& )
145 Reference
< XURLTransformer
> xTrans
;
148 xTrans
.set( URLTransformer::create(m_xContext
) );
149 m_xConfigData
= m_xConfigSource
->getSettings( m_aResourceURL
, sal_False
);
150 if ( m_xConfigData
.is() )
152 // Fill menubar with container contents
154 MenuBarManager::FillMenuWithConfiguration( nId
, pVCLMenuBar
, aModuleIdentifier
, m_xConfigData
, xTrans
);
157 catch ( const NoSuchElementException
& )
161 bool bMenuOnly( false );
162 for ( sal_Int32 n
= 0; n
< aArguments
.getLength(); n
++ )
164 PropertyValue aPropValue
;
165 if ( aArguments
[n
] >>= aPropValue
)
167 if ( aPropValue
.Name
== "MenuOnly" )
168 aPropValue
.Value
>>= bMenuOnly
;
174 // Initialize menubar manager with our vcl menu bar. There are some situations where we only want to get the menu without any
175 // interaction which is done by the menu bar manager. This must be requested by a special property called "MenuOnly". Be careful
176 // a menu bar created with this property is not fully supported. It must be attached to a real menu bar manager to have full
177 // support. This feature is currently used for "Inplace editing"!
178 Reference
< XDispatchProvider
> xDispatchProvider
;
180 MenuBarManager
* pMenuBarManager
= new MenuBarManager( m_xContext
,
189 m_xMenuBarManager
= Reference
< XComponent
>( static_cast< OWeakObject
*>( pMenuBarManager
), UNO_QUERY
);
192 // Initialize toolkit menu bar implementation to have awt::XMenuBar for data exchange.
193 // Don't use this toolkit menu bar or one of its functions. It is only used as a data container!
194 pAwtMenuBar
= new VCLXMenuBar( pVCLMenuBar
);
195 m_xMenuBar
= Reference
< XMenuBar
>( static_cast< OWeakObject
*>( pAwtMenuBar
), UNO_QUERY
);
200 // XUIElementSettings
201 void SAL_CALL
MenuBarWrapper::updateSettings() throw ( RuntimeException
, std::exception
)
206 throw DisposedException();
208 if ( m_xMenuBarManager
.is() )
210 if ( m_xConfigSource
.is() && m_bPersistent
)
214 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
216 m_xConfigData
= m_xConfigSource
->getSettings( m_aResourceURL
, sal_False
);
217 if ( m_xConfigData
.is() )
218 pMenuBarManager
->SetItemContainer( m_xConfigData
);
220 catch ( const NoSuchElementException
& )
224 else if ( !m_bPersistent
)
226 // Transient menubar: do nothing
230 void MenuBarWrapper::impl_fillNewData()
232 // Transient menubar => Fill menubar with new data
233 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
235 if ( pMenuBarManager
)
236 pMenuBarManager
->SetItemContainer( m_xConfigData
);
239 void MenuBarWrapper::fillPopupControllerCache()
241 if ( m_bRefreshPopupControllerCache
)
243 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
244 if ( pMenuBarManager
)
245 pMenuBarManager
->GetPopupController( m_aPopupControllerCache
);
246 if ( !m_aPopupControllerCache
.empty() )
247 m_bRefreshPopupControllerCache
= false;
252 Type SAL_CALL
MenuBarWrapper::getElementType()
253 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
255 return cppu::UnoType
<XDispatchProvider
>::get();
258 sal_Bool SAL_CALL
MenuBarWrapper::hasElements()
259 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
264 throw DisposedException();
266 fillPopupControllerCache();
267 return ( !m_aPopupControllerCache
.empty() );
271 Any SAL_CALL
MenuBarWrapper::getByName(
272 const OUString
& aName
)
273 throw ( container::NoSuchElementException
,
274 lang::WrappedTargetException
,
275 uno::RuntimeException
, std::exception
)
280 throw DisposedException();
282 fillPopupControllerCache();
284 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.find( aName
);
285 if ( pIter
!= m_aPopupControllerCache
.end() )
287 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider
;
288 xDispatchProvider
= pIter
->second
.m_xDispatchProvider
;
289 return uno::makeAny( xDispatchProvider
);
292 throw container::NoSuchElementException();
295 Sequence
< OUString
> SAL_CALL
MenuBarWrapper::getElementNames()
296 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
301 throw DisposedException();
303 fillPopupControllerCache();
305 Sequence
< OUString
> aSeq( m_aPopupControllerCache
.size() );
308 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.begin();
309 while ( pIter
!= m_aPopupControllerCache
.end() )
311 aSeq
[i
++] = pIter
->first
;
318 sal_Bool SAL_CALL
MenuBarWrapper::hasByName(
319 const OUString
& aName
)
320 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
325 throw DisposedException();
327 fillPopupControllerCache();
329 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.find( aName
);
330 if ( pIter
!= m_aPopupControllerCache
.end() )
337 Reference
< XInterface
> SAL_CALL
MenuBarWrapper::getRealInterface() throw ( RuntimeException
, std::exception
)
340 throw DisposedException();
342 return Reference
< XInterface
>( m_xMenuBarManager
, UNO_QUERY
);
345 } // namespace framework
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */